SharpHue
SharpHue copied to clipboard
Lights do not have to be sequential
// Lights are sequential, break if we don't have a light with the specified index.
This assumptions is incorrect. It is possible to add and remove hue lights. If you are lucky enough to have removed your first light like me, the program does not function.
EG Add Light 1 Add Light 2 Add Light 3 Remove Light 1
"/lights" now returns listOfLightJObject {{ "2": { "state": { "on": true, "bri": 254, "hue": 43520, "sat": 254, "effect": "none", "xy": [ 0.2073, 0.1196 ], "ct": 500, "alert": "none", "colormode": "hs", "reachable": true },
}, "3": { "state": { "on": true, "bri": 128, "hue": 21760, "sat": 254, "effect": "none", "xy": [ 0.4489, 0.4886 ], ... } } }}
Running through the for loop, (JObject)lights[i] is looking through the supplied JSON to find the property of 1 which does not exist. The light parsing stops here because of the break that was inserted.
Object lights = JsonClient.RequestSecure(HttpMethod.Get, "/lights") as JObject;
for (int i = 1; i <= Configuration.MAX_LIGHTS; i++)
{
if (lights[i.ToString()] != null)
{
Light l = ((JObject)lights[i.ToString()]).ToObject<Light>();
l.ID = i;
l.RefreshState();
Lights.Add(l);
}
else
{
// Lights are sequential, break if we don't have a light with the specified index.
break;
}
}
I suggest iterating through the properties of the returned JSON to properly parse the lights.