Ejemplo n.º 1
0
        public static int ChangeScene(string scene, bool hardCoded)
        {
            //Turn on the lights if any of them are disabled
            foreach (string light in Storage.groupData.lights)
            {
                if (JsonParser.Read(Storage.latestData, new string[] { "lights", light, "state", "on" }) == false)
                {
                    ToggleLight(light, true);
                }
            }

            dynamic tileColor;
            int     tileColorValue;

            if (hardCoded)
            {
                foreach (string light in Storage.groupData.lights)
                {
                    Uri dataUri = new Uri(AddressBuild.LightState(light));
                    PutData(dataUri, JsonParser.Serialize(JsonParser.Read(Storage.defaultScenes, new string[] { scene, "state", "1" })));
                }
                tileColorValue = (int)JsonParser.Read(Storage.defaultScenes, new string[] { scene, "tilecolor" });
                tileColor      = Storage.tileColors(tileColorValue);
            }
            else
            {
                if (JsonParser.Read(Storage.sceneData, new string[] { scene }) != null)
                {
                    int i     = 1;
                    int limit = 0;

                    foreach (dynamic light in JsonParser.Read(Storage.sceneData, new string[] { scene, "state" }))
                    {
                        limit++;
                    }

                    foreach (string light in Storage.groupData.lights)
                    {
                        Uri dataUri = new Uri(AddressBuild.LightState(light));
                        PutData(dataUri, JsonParser.Serialize(JsonParser.Read(Storage.sceneData, new string[] { scene, "state", i.ToString() })));
                        if (JsonParser.Read(Storage.sceneData, new string[] { scene, "state", i.ToString(), "bri" }) <= 2)
                        {
                            ToggleLight(light, false);
                        }

                        if (i < limit)
                        {
                            i++;
                        }
                    }

                    tileColorValue = (int)JsonParser.Read(Storage.sceneData, new string[] { scene, "tilecolor" });
                    tileColor      = Storage.tileColors(tileColorValue);
                }
                else
                {
                    //Error handling for missing scene data
                    tileColorValue = 0;
                    tileColor      = Storage.tileColors(tileColorValue);
                }
            }

            //Change accent to match tile background
            ChangeAccent(tileColorValue);

            //Store scene name
            Storage.currentScene = scene;

            return(0);
        }