public LightRequest SetLights(Light light, bool doFade = false) { var result = new LightRequest { Light = light }; var client = clientFactory.CreateClient(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; var todoItemJson = new StringContent(JsonSerializer.Serialize(light, options), Encoding.UTF8, "application/json"); string url; if (doFade) { url = $"https://localhost:{portNumber}/api/light?doFade=true"; } else { url = $"https://localhost:{portNumber}/api/light"; } using var response = client.PostAsync(url, todoItemJson); //httpResponse.EnsureSuccessStatusCode(); result.Response = response.Result.Content.ToString(); return(result); }
private void LightColor_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs <Color?> e) { if (LightColor.SelectedColor.HasValue) { Light light = new Light(System.Drawing.Color.FromArgb(LightColor.SelectedColor.Value.R, LightColor.SelectedColor.Value.G, LightColor.SelectedColor.Value.B)); LightRequest result = lightsService.SetLights(light, checkbox_doFade); } }
public static void UpdateMqttLight(string entityId, string state, int red, int green, int blue) { Color col = Color.FromArgb(red, green, blue); if (col == null) { col = Color.Black; } try { if (entityId.Contains("light.")) { entityId = entityId.Replace("light.", ""); } string configpayload = $@" {{ ""~"": ""homeassistant/light/{entityId}"", ""name"": ""{entityId}"", ""unique_id"": ""{entityId}"", ""cmd_t"": ""~/set"", ""stat_t"": ""~/state"", ""schema"": ""json"", ""brightness"": true ""rgb"": true }}"; CreateMqttDevice("light", entityId, configpayload); Console.WriteLine($"Sending light info to MQTT", "info"); LightRequest lightState = new LightRequest(); Light light = new Light(col); lightState.Light = light; lightState.LightState = state.ToUpper(); var lightJson = JsonConvert.SerializeObject(lightState); Console.WriteLine($"{lightJson}", "info"); //MqttClient.MqttMsgPublished += mqttClient_MqttMsgPublished; var topic = $"homeassistant/light/{entityId}/state"; Console.WriteLine($"Following topic is used for updating {entityId}: {topic} to state: {lightJson}", "info"); ushort msgId = MqttClient.Publish(topic, // topic Encoding.UTF8.GetBytes(lightJson), // message body MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level true); // retained } catch (Exception e) { Console.WriteLine($"{e}", "error"); Console.WriteLine($"{e.Message}", "error"); Console.WriteLine($"{e.StackTrace}", "error"); Console.WriteLine($"{e.InnerException}", "error"); } }
public LightRequest SetLights(Light light, bool doFade) { LightRequest result = new LightRequest() { Light = light }; try { if (!serialPort.IsOpen) { OpenPort(); } var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; Light oldLight = JsonSerializer.Deserialize <Light>(File.ReadAllText(FileUtil.GetFilePath("files/light.json")), options); if (doFade && oldLight != light && result.LightState == "on") { result.Fade = doFade; int discreteUnits = 25; Color fadeTo = Light.GetColor(light); Color baseClr = Light.GetColor(oldLight); float correctionFactor = 0.0f; float corFactorStep = 1.0f / discreteUnits; for (int i = 0; i < discreteUnits; i++) { correctionFactor += corFactorStep; float red = (fadeTo.R - baseClr.R) * correctionFactor + baseClr.R; float green = (fadeTo.G - baseClr.G) * correctionFactor + baseClr.G; float blue = (fadeTo.B - baseClr.B) * correctionFactor + baseClr.B; int redInt = (int)red; int greenInt = (int)green; int blueInt = (int)blue; ColorCommand(redInt.ToString().PadLeft(3, '0'), blueInt.ToString().PadLeft(3, '0'), greenInt.ToString().PadLeft(3, '0')); Thread.Sleep(500); } result.Response = serialPort.ReadExisting(); Console.WriteLine(result.Response); result.Succes = true; } else { ColorCommand(light.Red.PadLeft(3, '0'), light.Blue.PadLeft(3, '0'), light.Green.PadLeft(3, '0')); result.Response = serialPort.ReadLine(); Console.WriteLine(result.Response); result.Succes = true; } return(result); } catch (Exception ex) { result.Response = ex.Message; result.Succes = false; return(result); } }