Exemplo n.º 1
0
        protected static void PublishedMsgReceived(object sender, MqttMsgPublishEventArgs e, LightpackApiClient lightpackApiClient, string stateTopic, byte qosLevel)
        {
            var msg     = Encoding.UTF8.GetString(e.Message).ToUpper();
            var success = msg switch
            {
                "ON" => lightpackApiClient.SetStatus(Status.On),
                "OFF" => lightpackApiClient.SetStatus(Status.Off),
                _ => false
            };

            // No need to publish state if we couldn't change it
            if (!success)
            {
                return;
            }

            var mqttClient = (MqttClient)sender;

            mqttClient.Publish(stateTopic, Encoding.UTF8.GetBytes(msg), qosLevel, false);
        }
    }
Exemplo n.º 2
0
 public LightpackMqttClient(LightpackApiClient lightpackApiClient, IOptions <MqttConfiguration> mqttConfig)
 {
     _lightpackApiClient = lightpackApiClient;
     _mqttConfig         = mqttConfig.Value;
     _mqttClient         = new MqttClient(mqttConfig.Value.Host);
 }