Esempio n. 1
0
        internal void GetListOfMqttDevices()
        {
            Device[]     devices = IcueSdk.ListDevices();
            CorsairError error   = IcueSdk.CorsairGetLastError();

            if (error != CorsairError.Success)
            {
                Logger.LogError("SDK error getting list of devices", new Exception(error.ToString()));
            }
            Dictionary <string, int> modelCount = new Dictionary <string, int>();

            for (int i = 0; i < devices.Length; i++)
            {
                //handle multiple devices of same model
                int modelSuffix = 0;
                if (modelCount.ContainsKey(devices[i].CorsairDevice.Model))
                {
                    modelSuffix = modelCount[devices[i].CorsairDevice.Model] + 1;
                    modelCount[devices[i].CorsairDevice.Model] = modelSuffix;
                }
                else
                {
                    modelCount.Add(devices[i].CorsairDevice.Model, modelSuffix);
                }
                MqttIcueDeviceList.AddIcueDevice(devices[i], modelSuffix);
            }
        }
Esempio n. 2
0
 private void SetState(MqttIcueDevice mqttIcueDevice, bool isAllDevices, int R, int G, int B)
 {
     if (isAllDevices)
     {
         MqttIcueDeviceList.SetAllDeviceState(IcueSdk, R, G, B);
         foreach (MqttIcueDevice icueDevice in MqttIcueDeviceList.GetDevices())
         {
             SendStateUpdate(icueDevice);
         }
         SendStateUpdate(MqttIcueDeviceList.TOPIC_ALL_DEVICE_STATE, MqttIcueDeviceList.GetAllDeviceAverageState());
     }
     else
     {
         IcueSdk.SetDeviceColor(mqttIcueDevice.IcueDevice, R, G, B);
         SendStateUpdate(mqttIcueDevice);
     }
 }
Esempio n. 3
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;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Publishes the icue devices to the MQTT broker and sets up the control topics.
        /// </summary>
        internal void PublishDevices()
        {
            if (Client.IsConnected)
            {
                if (IcueSdk == null)
                {
                    IcueSdk = new Sdk(false);
                }
                if (MqttIcueDeviceList.GetDevices().Length == 0)
                {
                    GetListOfMqttDevices();
                }
                for (int i = 0; i < MqttIcueDeviceList.GetDevices().Length; i++)
                {
                    MqttIcueDevice mqttIcueDevice = MqttIcueDeviceList.GetDevices()[i];
                    if (mqttIcueDevice != null)
                    {
                        Logger.LogInformation(String.Format("Publishing device {0}", mqttIcueDevice.IcueDevice.CorsairDevice.Model));
                        MqttClientSubscribeOptions subscriptions = new MqttClientSubscribeOptions();
                        List <TopicFilter>         topicFilters  = new List <TopicFilter>();
                        topicFilters.Add(new TopicFilter()
                        {
                            Topic = mqttIcueDevice.CommandTopic,
                            QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                        });
                        subscriptions.TopicFilters = topicFilters;
                        Client.SubscribeAsync(subscriptions);
                        MqttApplicationMessage publishMessage = new MqttApplicationMessage()
                        {
                            Payload = Encoding.UTF8.GetBytes(mqttIcueDevice.Discovery.ToJson()),
                            Topic   = mqttIcueDevice.DiscoveryTopic,
                            Retain  = true,
                            QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                        };
                        Client.PublishAsync(publishMessage).ContinueWith(e => { SendStateUpdate(mqttIcueDevice); });
                    }
                }
                if (MqttIcueDeviceList.GetDevices().Length > 0)
                {
                    //publish the all device entity
                    MqttClientSubscribeOptions subscriptions = new MqttClientSubscribeOptions();
                    List <TopicFilter>         topicFilters  = new List <TopicFilter>();
                    topicFilters.Add(new TopicFilter()
                    {
                        Topic = MqttIcueDeviceList.TOPIC_ALL_DEVICE_SET,
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    });
                    subscriptions.TopicFilters = topicFilters;
                    Client.SubscribeAsync(subscriptions);
                    MqttApplicationMessage publishMessage = new MqttApplicationMessage()
                    {
                        Payload = Encoding.UTF8.GetBytes(MqttIcueDeviceList.GetAllDeviceDiscovery().ToJson()),
                        Topic   = MqttIcueDeviceList.TOPIC_ALL_DEVICE_CONFIG,
                        Retain  = true,
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    };
                    Client.PublishAsync(publishMessage).ContinueWith(e =>
                    {
                        SendStateUpdate(MqttIcueDeviceList.TOPIC_ALL_DEVICE_STATE, MqttIcueDeviceList.GetAllDeviceAverageState());
                    });


                    //publish the icue control switch
                    subscriptions = new MqttClientSubscribeOptions();
                    topicFilters  = new List <TopicFilter>();
                    topicFilters.Add(new TopicFilter()
                    {
                        Topic = TOPIC_CONTROL_SWITCH_SET,
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                    });
                    subscriptions.TopicFilters = topicFilters;
                    Client.SubscribeAsync(subscriptions);
                    publishMessage = new MqttApplicationMessage()
                    {
                        Payload = Encoding.UTF8.GetBytes(new MqttIcueControlSwitchDiscovery(TOPIC_CONTROL_SWITCH_STATE, TOPIC_CONTROL_SWITCH_SET).ToJson()),
                        Topic   = TOPIC_CONTROL_SWITCH_CONFIG,
                        Retain  = false,
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                    };
                    Client.PublishAsync(publishMessage).ContinueWith(e =>
                    {
                        SendControlSwitchUpdate();
                    });
                }
            }
            else
            {
                Logger.LogInformation("MQTT broker connection lost.");
            }
        }