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);
            }
        }
        /// <summary>
        /// Stops the MQTT client and disposes of the  IcueSDK releasing the icue connection
        /// </summary>
        internal async System.Threading.Tasks.Task StopAsync()
        {
            stopping = true;
            if (Client != null && Client.IsConnected)
            {
                await Client.DisconnectAsync();

                Client.Dispose();
            }
            if (IcueSdk != null)
            {
                IcueSdk.Dispose();
            }
        }
 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);
     }
 }
        /// <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;
            }
        }