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); } }
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; } }
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; }