示例#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);
            }
        }
示例#2
0
        private async void btnAllOff_Click(object sender, EventArgs e)
        {
            RESTRequests r = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));
            int          i = 0;

            foreach (HueLight hl in allLights)
            {
                string result = await r.PUT("/api/" + APIKey + "/lights/" + lightIndices[i] + "/state", "{\"on\":false}", Encoding.UTF8, Resources.BODY_TYPE_JSON);

                i++;
            }
        }
示例#3
0
        private async void onCellText_Changed(object sender, DataGridViewCellEventArgs e)
        {
            int          rowIdx       = e.RowIndex + 1;
            string       newLightName = dgvLights.Rows[e.RowIndex].Cells["name"].Value.ToString();
            RESTRequests r            = new RESTRequests(theHueBridge.bridgeURLBase.Replace("http://", "").Replace(@":80/", ""));
            string       result       = await r.PUT("/api/" + APIKey + "/lights/" + rowIdx, "{\"name\":\"" + newLightName + "\"}", Encoding.UTF8, Resources.BODY_TYPE_JSON);

            if (result == null || result.Contains("error") || result == "")
            {
                MessageBox.Show(Resources.ERROR03, "Name-change Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            populateBridgeLights();
        }
示例#4
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;
            }
        }
示例#5
0
        /// <summary>
        /// One time operation. Pings a bridge to acquire the API Key
        /// Once found, the API Key is stored locally for future use.
        /// </summary>
        /// <param name="sender">UI element that this event originates from</param>
        /// <param name="e">Event parameters</param>
        private async void btnConnect_Click(object sender, EventArgs e)
        {
            if (ourIP == 0)
            {
                MessageBox.Show(Resources.ERROR07, "IP address error", MessageBoxButtons.OK);
                btnSearch.Enabled        = true;
                btnBridgeDetails.Enabled = false;
                return;
            }

            string       ipAddress = "192.168.0." + ourIP.ToString();
            RESTRequests r         = new RESTRequests(ipAddress);
            string       result    = await r.POST("/api", HueConstants.BODY_POST_CONNECT, Encoding.UTF8, Resources.BODY_TYPE_JSON);

            if (result == null || result.Contains("error") || result == "")
            {
                MessageBox.Show(Resources.ERROR02, "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            List <BridgeConnectionSuccess> s = jss.Deserialize <List <BridgeConnectionSuccess> >(result);

            if (s == null || s[0].success == null || String.IsNullOrEmpty(s[0].success.username))
            {
                MessageBox.Show(Resources.ERROR02, "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            APIKey = s[0].success.username;
            storeKey();
            storeSubnet(ourIP);

            logger.Info("API KEY DONE");

            btnConnect.Enabled = false;

            gbConnect.Visible = false;
        }
示例#6
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);
            }
        }
示例#7
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;
        }