Exemplo n.º 1
0
        public static void UpdateMqttLight(string entityId, string state, int red, int green, int blue)
        {
            Color col = Color.FromArgb(red, green, blue);

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

                LogHelper.Log($"Sending light info to MQTT", "info");
                var lightState = new JsonTypes.LightState();
                JsonTypes.LightColor lightColor = new JsonTypes.LightColor();
                if (col == null)
                {
                    col = Color.Black;
                }

                lightColor.r          = red;
                lightColor.g          = green;
                lightColor.b          = blue;
                lightState.color      = lightColor;
                lightState.brightness = (red + green + blue) / 3;
                lightState.state      = state.ToUpper();
                var lightJson = JsonConvert.SerializeObject(lightState);

                LogHelper.Log($"{lightJson}", "info");


                MqttClient.MqttMsgPublished += mqttClient_MqttMsgPublished;
                var topic = $"homeassistant/light/{entityId}/state";
                LogHelper.Log($"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)
            {
                LogHelper.Log($"{e}", "error");
                LogHelper.Log($"{e.Message}", "error");
                LogHelper.Log($"{e.StackTrace}", "error");
                LogHelper.Log($"{e.InnerException}", "error");
            }
        }
Exemplo n.º 2
0
        private static void mqttClient_recievedMessage(object sender, MqttMsgPublishEventArgs e)
        {
            // Handle message received
            var message = System.Text.Encoding.Default.GetString(e.Message);

            LogHelper.Log($"Message received: {message} in topic {e.Topic}", "info");

            try
            {
                if (Service1.manualMode)
                {
                    if (e.Topic.Contains("axolotllight"))
                    {
                        JsonTypes.LightState lightState = new JsonTypes.LightState();
                        Color sentColor = Color.Snow;
                        lightState = JsonConvert.DeserializeObject <JsonTypes.LightState>(message);
                        if (lightState.state == "ON")
                        {
                            if (lightState.color != null)
                            {
                                sentColor = Color.FromArgb(lightState.color.r, lightState.color.g, lightState.color.b);
                            }
                        }
                        else
                        {
                            sentColor = Color.Black;
                        }
                        Service1.ColorFade(Service1.oldColor, sentColor, true, 3);
                        UpdateMqttLight("axolotllight", lightState.state, sentColor.R, sentColor.G, sentColor.B);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log($"{ex}", "error");
                LogHelper.Log($"{ex.Message}", "error");
                LogHelper.Log($"{ex.StackTrace}", "error");
                LogHelper.Log($"{ex.InnerException}", "error");
            }
        }