[#3] How to read Sensors | Philips Hue - C# Programming

in #programming7 years ago

How to read Sensors

<hr /> <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmXaVtwo4XuqV72hESzSUzXM3irWc5Bs8atCXaF8gWiUYs/analytics-3088958_1280.jpg" alt="analytics-3088958_1280.jpg" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmXaVtwo4XuqV72hESzSUzXM3irWc5Bs8atCXaF8gWiUYs/analytics-3088958_1280.jpg 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmXaVtwo4XuqV72hESzSUzXM3irWc5Bs8atCXaF8gWiUYs/analytics-3088958_1280.jpg 2x" /> <center><em><span><a href="https://pixabay.com/en/analytics-information-innovation-3088958/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://pixabay.com/en/analytics-information-innovation-3088958/<br /> <p dir="auto"><h3>Previous Post<p> <hr /> <p dir="auto">In the last tutorial, I showed how to control the lights.<br /> You can find it here: <a href="https://steemit.com/programming/@geggi632/2-how-to-control-hue-lights-or-philips-hue-c-programming" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">[#2] How to control Hue Lights | Philips Hue - C# Programming <p dir="auto"><h3>Sensors<p> <hr /> <p dir="auto">You will find a lot more than lights in the Philips Hue System, called sensors. This tutorial is limited to the Hue Motion Sensor and Hue Dimmer Switch because I don't own a Hue Tap Switch. I will focus on the motion sensor which has a built-in temperature sensor. I will show how to get the sensor data, parse it into an object and show the temperature. <p dir="auto">Lets create the sensor template:<br /> <code>private const string SensorUrlTemplate = "http://{0}/api/{1}/{2}"; <p dir="auto">Now use the GetRequestToBridge from the last Tutorial:<br /> <code>string result = GetRequestToBridge(string.Format(SensorUrlTemplate, BridgeIP, Usercode, "sensors")); <p dir="auto"><span>As result, you will get all sensors from your hue system formatted as JSON. Every sensor has different properties so it's a little bit hard to parse this JSON. I created a helper class for parsing (<a href="http://json2csharp.com/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://json2csharp.com/) and modified it: <pre><code>using System; using System.Collections.Generic; using Newtonsoft.Json; using HueTestForms.Enumerations; namespace HueTestForms.Helpers { public partial class SensorHelper { public string ID { get; internal set; } public SensorTypes SensorType { get; set; } [JsonProperty("state")] public SensorHelperHelperState State { get; set; } [JsonProperty("config")] public SensorHelperHelperConfig Config { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("type")] public SensorTypes Type { get; set; } [JsonProperty("modelid")] public string Modelid { get; set; } [JsonProperty("manufacturername")] public string Manufacturername { get; set; } [JsonProperty("swversion")] public string Swversion { get; set; } [JsonProperty("uniqueid")] public string Uniqueid { get; set; } [JsonProperty("recycle")] public bool Recycle { get; set; } [JsonProperty("swupdate")] public SensorHelperHelperSwupdate Swupdate { get; set; } } public partial class SensorHelperHelperConfig { [JsonProperty("on")] public bool On { get; set; } [JsonProperty("configured")] public bool Configured { get; set; } [JsonProperty("sunriseoffset")] public long Sunriseoffset { get; set; } [JsonProperty("sunsetoffset")] public long Sunsetoffset { get; set; } [JsonProperty("reachable")] public bool Reachable { get; set; } [JsonProperty("battery")] public long Battery { get; set; } [JsonProperty("pending")] public List<object> Pending { get; set; } [JsonProperty("flag")] public bool Flag { get; set; } [JsonProperty("alert")] public string Alert { get; set; } [JsonProperty("ledindication")] public bool Ledindication { get; set; } [JsonProperty("usertest")] public bool Usertest { get; set; } [JsonProperty("sensitivity")] public long? Sensitivity { get; set; } [JsonProperty("sensitivitymax")] public long? Sensitivitymax { get; set; } [JsonProperty("tholddark")] public long? Tholddark { get; set; } [JsonProperty("tholdoffset")] public long? Tholdoffset { get; set; } } public partial class SensorHelperHelperState { [JsonProperty("daylight")] public object Daylight { get; set; } [JsonProperty("lastupdated")] public string Lastupdated { get; set; } [JsonProperty("status")] public long Status { get; set; } [JsonProperty("buttonevent")] public long Buttonevent { get; set; } [JsonProperty("temperature")] public double Temperature { get; set; } [JsonProperty("presence")] public bool Presence { get; set; } [JsonProperty("lightlevel")] public long Lightlevel { get; set; } [JsonProperty("dark")] public bool Dark { get; set; } } public partial class SensorHelperHelperSwupdate { [JsonProperty("state")] public string State { get; set; } [JsonProperty("lastinstall")] public object Lastinstall { get; set; } } public static class SensorHelperHelperSerialize { public static string ToJson(this SensorHelper self) => JsonConvert.SerializeObject(self, SensorHelperHelperConverter.Settings); } public class SensorHelperHelperConverter { public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore, DateParseHandling = DateParseHandling.None, }; } } <p dir="auto">As I mentioned in the beginning, I don't own a Hue tap switch so maybe the parsing will fail if you own one.<br /> For the Type I added an enum: <pre><code>public enum SensorTypes { Daylight, CLIPGenericFlag, ZLLSwitch, CLIPGenericStatus, ZLLTemperature, ZLLPresence, ZLLLightLevel, } <p dir="auto">For parsing: <pre><code>public partial class SensorHelper { public static List<SensorHelper> FromJson(string json) { var dict = JsonConvert.DeserializeObject<Dictionary<string, SensorHelper>>(json, SensorHelperHelperConverter.Settings); var result = new List<SensorHelper>(); foreach (var item in dict) { item.Value.ID = item.Key; result.Add(item.Value); } return result; } } <p dir="auto">I deserialize it to a dictonary>string, Sensorhelper> to keep the ID as Key. Afterwards I create a List of SensorHelper and add the ID to every object. I do this to have the ID in the object and don't have to handle with a dictonary. <p dir="auto">Now it's easy to get the Temperature: <pre><code>SensorList.Where(s => s.State.Temperature != 0).First().State.Temperature; <p dir="auto">The temperature is stored as int so you have to add the comma! <p dir="auto">If you have any questions do not hesitate to contact me per comment 😃 <hr /> <p dir="auto"><b><center>Have a nice day<br /> Geggi632<br /> <img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmXk1FUtoX2AfvWHy2R3WmTHWJDw2wo9DyKG9msXeinSuk/_DSC4507-2.jpg" alt="_DSC4507-2.jpg" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmXk1FUtoX2AfvWHy2R3WmTHWJDw2wo9DyKG9msXeinSuk/_DSC4507-2.jpg 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmXk1FUtoX2AfvWHy2R3WmTHWJDw2wo9DyKG9msXeinSuk/_DSC4507-2.jpg 2x" /> <hr />