Example #1
0
        async private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            switch (effectMode)
            {
            case "test":
                TestPut();
                break;

            case "ambient":
                AmbientPut();
                break;

            case "daylight":
                DaylightPut();
                break;
            }

            try {
                HttpClient    client  = new HttpClient();
                StringContent content = new StringContent(JsonParser.Serialize(bufferData), Encoding.UTF8);
                if (effectsOn)
                {
                    await client.PutAsync(lightArray[lastLight], content);
                }
            } catch (Exception) { return; } finally {
                lastLight++;
                if (lastLight >= lightArray.Length)
                {
                    lastLight = 0;
                }
            }
        }
Example #2
0
        async public static Task PairAttempt(string bridgeIP, string bridgeUser)
        {
            HttpClient client  = new HttpClient();
            string     address = AddressBuild.APIRootSetup(bridgeIP);

            try {
                dynamic dynContent = new ExpandoObject();
                string  devicetype = String.Format("Albedo#{0}.{1}", Environment.MachineName, DateTime.Now.ToString());
                if (devicetype.Length > 40)
                {
                    devicetype = devicetype.Substring(0, 40);
                }
                dynContent.devicetype = devicetype;
                //dynContent.username = bridgeUser; //OBSOLETE. Username is now assigned by the bridge.
                StringContent content = new StringContent(JsonParser.Serialize(dynContent));

                Task <HttpResponseMessage> postData = client.PostAsync(address, content);
                HttpResponseMessage        response = await postData;
                string responseString = await response.Content.ReadAsStringAsync();

                //Ugly hack; to be removed once parser handles arrays properly
                responseString = responseString.Substring(1, responseString.Length - 2);

                Storage.tempData = JsonParser.Deserialize(responseString);
                return;
            } catch (Exception) {
                dynamic failData = new ExpandoObject();
                Storage.tempData = failData;
                return;
            }
        }
Example #3
0
        private static void ChangeValue(string light, int value, string valueName)
        {
            dynamic jsonValue = new ExpandoObject();
            var     jsonRef   = (IDictionary <string, object>)jsonValue;

            jsonRef[valueName] = value;

            Uri dataUri = new Uri(AddressBuild.LightState(light));

            TimePut(dataUri, JsonParser.Serialize(jsonValue));
            JsonParser.Modify(Storage.latestData, new string[] { "lights", light, "state", valueName }, (int)value);
        }
Example #4
0
        public static void EffectsOff()
        {
            if (effectTimer.Enabled)
            {
                effectMode           = "";
                effectTimer.Elapsed -= OnTimedEvent;
                effectTimer.Enabled  = false;
                foreach (string light in Storage.groupData.lights)                   //Restore last scene data from backup

                {
                    JsonParser.Create(Storage.latestData, new string[] { "lights", light, "state" }, JsonParser.Read(Storage.sceneBackup, new string[] { light, "state" }));
                    PutEvents.CustomPut(light, JsonParser.Serialize(JsonParser.Read(Storage.sceneBackup, new string[] { light, "state" })), true);
                }
                effectsOn = false;
                Platform.NotifyCheck("", false);
            }
        }
Example #5
0
        public static int ToggleLight(string light, bool isOn)
        {
            dynamic jsonToggle = new ExpandoObject();

            if (isOn == true)
            {
                jsonToggle.on = true;
            }
            else
            {
                jsonToggle.on = false;
            }

            Uri dataUri = new Uri(AddressBuild.LightState(light));

            JsonParser.Modify(Storage.latestData, new string[] { "lights", light, "state", "on" }, isOn);
            PutData(dataUri, JsonParser.Serialize(jsonToggle));
            return(0);
        }
Example #6
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 (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);
        }