Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights do not have to be sequential #4

Open
BobDaMann opened this issue Feb 20, 2016 · 1 comment
Open

Lights do not have to be sequential #4

BobDaMann opened this issue Feb 20, 2016 · 1 comment
Labels

Comments

@BobDaMann
Copy link

// 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.

BobDaMann added a commit to BobDaMann/SharpHue that referenced this issue Feb 21, 2016
…me in the JSON instead of guessing @ the Index number
@qJake qJake added the bug label Apr 27, 2016
@qJake
Copy link
Owner

qJake commented Feb 13, 2017

@BobDaMann Care to send a pull request for this? Your code looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants