Ejemplo n.º 1
0
 private void SendStateUpdate(string stateTopic, MqttIcueDeviceState state)
 {
     if (Client.IsConnected)
     {
         Client.PublishAsync(new MqttApplicationMessageBuilder()
                             .WithTopic(stateTopic)
                             .WithPayload(Encoding.UTF8.GetBytes(state.ToJson()))
                             .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce)
                             .WithRetainFlag(true)
                             .Build());
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the MqttMsgPublishReceived event of the client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MqttMsgPublishEventArgs"/> instance containing the event data.</param>
        private void client_MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e)
        {
            string jsonMessage = Encoding.UTF8.GetString(e.ApplicationMessage.Payload, 0, e.ApplicationMessage.Payload.Length);

            Console.WriteLine(e.ApplicationMessage.Topic);
            if (e.ApplicationMessage.Topic == MqttIcueDeviceList.TOPIC_ALL_DEVICE_STATE)
            {
                SendStateUpdate(MqttIcueDeviceList.TOPIC_ALL_DEVICE_STATE, MqttIcueDeviceList.GetAllDeviceAverageState());
                return;
            }

            if (e.ApplicationMessage.Topic == TOPIC_CONTROL_SWITCH_STATE)
            {
                SendControlSwitchUpdate();
                return;
            }

            MqttIcueDevice mqttIcueDevice = MqttIcueDeviceList.GetDeviceByStateTopic(e.ApplicationMessage.Topic);

            if (mqttIcueDevice != null)
            {
                SendStateUpdate(mqttIcueDevice);
                return;
            }
            bool isSetAllDevices = e.ApplicationMessage.Topic == MqttIcueDeviceList.TOPIC_ALL_DEVICE_SET;

            mqttIcueDevice = MqttIcueDeviceList.GetDeviceBySetTopic(e.ApplicationMessage.Topic);
            if (mqttIcueDevice != null || isSetAllDevices)
            {
                MqttIcueDeviceState state = JsonConvert.DeserializeObject <MqttIcueDeviceState>(jsonMessage);
                if (state.Color == null)
                {
                    if (state.State.Equals("ON"))
                    {
                        SetState(mqttIcueDevice, isSetAllDevices);
                        CorsairError error = IcueSdk.CorsairGetLastError();
                        if (error != CorsairError.Success)
                        {
                            Logger.LogError("SDK error setting device to ON", new Exception(error.ToString()));
                        }
                    }
                    else
                    {
                        mqttIcueDevice.SetOffState();
                        SetState(mqttIcueDevice, isSetAllDevices, 0, 0, 0);
                        CorsairError error = IcueSdk.CorsairGetLastError();
                        if (error != CorsairError.Success)
                        {
                            Logger.LogError("SDK error setting device to OFF", new Exception(error.ToString()));
                        }
                    }
                    return;
                }
                else
                {
                    SetState(mqttIcueDevice, isSetAllDevices, state.Color.R, state.Color.G, state.Color.B);
                    CorsairError error = IcueSdk.CorsairGetLastError();
                    if (error != CorsairError.Success)
                    {
                        Logger.LogError("SDK error setting device color", new Exception(error.ToString()));
                    }
                }
                return;
            }

            if (e.ApplicationMessage.Topic == TOPIC_CONTROL_SWITCH_SET)
            {
                if (jsonMessage.Equals("ON"))
                {
                    HasControl = true;
                    IcueSdk.SetLayerPriority(130);
                }
                else
                {
                    HasControl = false;
                    IcueSdk.SetLayerPriority(126);
                }
                SendControlSwitchUpdate();
                return;
            }
        }