Пример #1
0
        /// <summary>
        /// Posts a API command for a single light to the Hue Bridge.  Used to change the State of a light.
        /// </summary>
        /// <param name="light"></param>
        /// <param name="command"></param>
        private void SendApiCommand(Light light, string command)
        {
            try
            {
                HttpResponseMessage response = new HttpResponseMessage();

                // Create a Http Call for Access Token
                HttpClient client = new HttpClient();
                client.BaseAddress = _bridgeApiBase;

                client.PutAsync(_bridgeApiBase + "/lights/" + light.Id.ToString() + "/state", new StringContent(command)).ContinueWith(
                    (getTask) =>
                    {
                        if (getTask.IsCanceled) { return; }
                        if (getTask.IsFaulted) { throw getTask.Exception; }
                        response = getTask.Result;

                        response.EnsureSuccessStatusCode();
                    }).Wait();

                string result = response.Content.ReadAsStringAsync().Result.ToString();

            }
            catch (Exception ex)
            {
                throw new Exception("Error sending command.  Check inner exception for details.", ex);
            }
        }
Пример #2
0
        public void SetEffect(Light light, Effect effect)
        {
            LightCommand newState = new LightCommand();
            newState.Effect = effect;

            string command = JsonConvert.SerializeObject(newState, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            SendApiCommand(light, command);
        }
Пример #3
0
        public void TurnOn(Light light)
        {
            LightCommand newState = new LightCommand();
            newState.On = true;

            string command = JsonConvert.SerializeObject(newState, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

            SendApiCommand(light, command);
        }
Пример #4
0
        public void SetColor(Light light, int red, int green, int blue)
        {
            int hueValue = GetHueFromRGB(red, green, blue);

            string command = JsonConvert.SerializeObject(new { hue = hueValue});
            SendApiCommand(light, command);
        }