Exemplo n.º 1
0
        private async void onBrightness_ValueChanged(object sender, MouseEventArgs e)
        {
            int          lightIDX    = lightIndices[dgvLights.CurrentCell.RowIndex];
            RESTRequests r           = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));
            string       singleLight = await r.GET("/api/" + APIKey + "/lights/" + (lightIDX));

            JObject  singleLightObj = JObject.Parse(singleLight);
            HueLight selectedLight  = JsonConvert.DeserializeObject <HueLight>(singleLightObj.ToString());

            if (tbBrightness.Value == tbBrightness.Minimum)
            {
                await r.PUT("/api/" + APIKey + "/lights/" + (lightIDX) + "/state", "{\"on\":false}", Encoding.UTF8, Resources.BODY_TYPE_JSON);

                return;
            }

            if (!selectedLight.state.on)
            {
                await r.PUT("/api/" + APIKey + "/lights/" + (lightIDX) + "/state", "{\"on\":true, \"bri\":" + tbBrightness.Value + "}", Encoding.UTF8, Resources.BODY_TYPE_JSON);
            }
            else
            {
                await r.PUT("/api/" + APIKey + "/lights/" + (lightIDX) + "/state", "{\"bri\":" + tbBrightness.Value + "}", Encoding.UTF8, Resources.BODY_TYPE_JSON);
            }
        }
Exemplo n.º 2
0
        private async void btnColorChange_Click(object sender, EventArgs e)
        {
            colorPicker.Color = pnlCurrentColor.BackColor;
            if (colorPicker.ShowDialog() == DialogResult.OK)
            {
                Color c = colorPicker.Color;

                pnlCurrentColor.BackColor = c;
                double r = c.R;
                double g = c.G;
                double b = c.B;

                double X, Y;
                ColorConverter.colorToXY(r, g, b, out X, out Y);
                logger.Info("Setting color ==> " + X + "," + Y);

                int          lightIDX    = lightIndices[dgvLights.CurrentCell.RowIndex];
                RESTRequests rest        = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));
                string       singleLight = await rest.GET("/api/" + APIKey + "/lights/" + (lightIDX));

                JObject  singleLightObj = JObject.Parse(singleLight);
                HueLight selectedLight  = JsonConvert.DeserializeObject <HueLight>(singleLightObj.ToString());

                rest = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));

                if (!selectedLight.state.on)
                {
                    await rest.PUT("/api/" + APIKey + "/lights/" + (lightIDX) + "/state", "{\"on\":true, \"xy\":[" + X + "," + Y + "]}", Encoding.UTF8, Resources.BODY_TYPE_JSON);
                }
                else
                {
                    await rest.PUT("/api/" + APIKey + "/lights/" + (lightIDX) + "/state", "{\"xy\":[" + X + "," + Y + "]}", Encoding.UTF8, Resources.BODY_TYPE_JSON);
                }

                tbBrightness.Value = selectedLight.state.bri;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Finds all lights paired with the bridge and
        /// populates the UI list
        /// </summary>
        private async void populateBridgeLights()
        {
            string       endpoint = theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", "");
            RESTRequests r        = new RESTRequests(endpoint);
            string       result   = await r.GET("/api/" + APIKey + "/lights");

            if (result == null || result.Contains("error") || result == "" || result == "{}")
            {
                MessageBox.Show(Resources.ERROR03, "Lights not paired", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (result == Resources.LAN_DOWN)
            {
                MessageBox.Show(Resources.ERROR05, "Network Down!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Parsing this result proved to be trickier than expected
            // Looked it up here (https://stackoverflow.com/questions/41092239/json-deserialization-not-deserializing)
            // and followed the directions by Mohammed Abulssa and Mark Schultheiss to get the right mapping between
            // JSON and C# data structure
            // Further... when the light indices were changed in the bridge, due to user manually deleting and re-adding
            // lights (1,2,3,4,5 became 5,6,7,8,9) looked up here - https://stackoverflow.com/questions/21002297/getting-the-name-key-of-a-jtoken-with-json-net
            // to get the right indices for *future* REST calls. Storing these in lightIndices
            JObject        lightObj = JObject.Parse(result);
            IList <JToken> idxObj   = lightObj.Children().ToList();
            int            j        = 0;

            foreach (var pair in lightObj)
            {
                Int32.TryParse(pair.Key, out lightIndices[j]);
                j++;
            }
            IList <JToken> allPaired = idxObj.Children().ToList(); //MAGIC!! :)

            allLights = new HueLight[allPaired.Count];

            int i = 0;

            foreach (JToken jt in allPaired)
            {
                allLights[i] = JsonConvert.DeserializeObject <HueLight>(jt.ToString());
                i++;
            }

            while (allLights == null)
            {
                ;
            }
            if (allLights.Length > 1)
            {
                dgvLights.DataSource = allLights;
                btnAllOn.Enabled     = true;
                btnAllOff.Enabled    = true;
                dgvLights.Columns["state"].Visible           =
                    dgvLights.Columns["type"].Visible        =
                        dgvLights.Columns["modelid"].Visible =
                            dgvLights.Columns["manufacturername"].Visible  =
                                dgvLights.Columns["uniqueid"].Visible      =
                                    dgvLights.Columns["swversion"].Visible = false;

                dgvLights.ClearSelection();
            }
            else
            {
                MessageBox.Show(Resources.ERROR03, "Lights not paired", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 4
0
        private async void onLightSelectionChanged(object sender, EventArgs e)
        {
            if (allLights == null || allLights[0].modelid == null || dgvLights.CurrentCell == null)
            {
                return;
            }

            int lightIDX = lightIndices[dgvLights.CurrentCell.RowIndex];

            if (lightIDX >= 0)
            {
                logger.Info("Hue Light index = " + lightIDX);
            }
            else
            {
                return;
            }

            //Get current status of light
            RESTRequests r           = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));
            string       singleLight = await r.GET("/api/" + APIKey + "/lights/" + (lightIDX));

            JObject singleLightObj = JObject.Parse(singleLight);
            //IList<JToken> pairedLight = singleLightObj.Children().ToList(); //MAGIC!! :)
            HueLight selectedLight = JsonConvert.DeserializeObject <HueLight>(singleLightObj.ToString());

            String ld = String.Format(Resources.LIGHT_DETAILS, selectedLight.name, (selectedLight.modelid != null &&
                                                                                    selectedLight.modelid.Contains("LCT")) ? "Hue Lamp" : "Hue Lightstrip", (selectedLight.state.on) ? "ON" : "OFF");

            if (allLights[dgvLights.CurrentCell.RowIndex].modelid.Contains("LCT"))
            {
                btnColorChange.BackgroundImage = Resources.bulb;
            }
            else
            {
                btnColorChange.BackgroundImage = Resources.lightstrip;
            }

            btnColorChange.Visible = true;

            //Populate details
            lblLD_Name.Text         = "Name:  " + selectedLight.name;
            lblLD_Type.Text         = "Type:  " + selectedLight.type;
            lblLD_Manufacturer.Text = "Manufacturer:  " + selectedLight.manufacturername;
            lblLD_Model.Text        = "Model:  " + selectedLight.modelid;
            tbBrightness.Value      = (selectedLight.state.on)?selectedLight.state.bri:tbBrightness.Minimum;

            int brt = tbBrightness.Value;

            lblBrightnessValue.Text = (brt == 1) ? Resources.TEXT_OFF : (brt == 254) ? Resources.TEXT_MAX : brt.ToString();

            byte[] rgbValue = ColorConverter.xyBriToRgb(selectedLight.state.xy[0],
                                                        selectedLight.state.xy[1],
                                                        brt);

            Color currentColor = new Color();

            currentColor = Color.FromArgb(255, rgbValue[0], rgbValue[1], rgbValue[2]);
            pnlCurrentColor.BackColor = currentColor;

            pnlLightCtrl.Visible = true;
        }