// Mqtt message event handler private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { Console.WriteLine("DeviceActor: " + e.Topic); if (e.Topic == _topicUnregister) { _sender.Tell(new UnregisterDevice(), _self); _self.Tell(new UnregisterDevice(), _self); } if (e.Topic == _topicDisconnect) { _sender.Tell(new DisconnectDevice(), _self); _self.Tell(new DisconnectDevice(), _self); } if (e.Topic == _topicStateChanged) { MqttDeviceStateDto mqttDto = MqttDeviceStateDto.Deserialize(e.Message); _sender.Tell(new DeviceStateChanged(new DeviceStateDto(_deviceId, mqttDto.CurrentState, mqttDto.TimeStamp)), _self); } if (e.Topic == _topicSetDeviceStateChanged) { MqttDeviceStateDto mqttDto = MqttDeviceStateDto.Deserialize(e.Message); _sender.Tell(new SetDeviceStatusReceived(new DeviceStateDto(_deviceId, mqttDto.CurrentState, mqttDto.TimeStamp)), _self); } }
private void BtnOnOff_OnClick(object sender, RoutedEventArgs e) { _deviceState = !_deviceState; MqttDeviceStateDto deviceStateDto = new MqttDeviceStateDto(_deviceState); _client.Publish(DeviceTopic.DeviceStateChanged + "/" + _deviceId, deviceStateDto.Serialize(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); SetOnOffButtonLabel(); }
private void ClientOnMqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs mqttMsgPublishEventArgs) { if (mqttMsgPublishEventArgs.Topic == DeviceTopic.Unregistered + "/" + _deviceId) { if (_client != null && _client.IsConnected) { _client.Disconnect(); } btnConnect.Dispatcher.Invoke(() => { btnConnect.IsEnabled = true; }); btnDisconnect.Dispatcher.Invoke(() => { btnDisconnect.IsEnabled = false; }); btnOnOff.Dispatcher.Invoke(() => { btnOnOff.IsEnabled = false; }); } else if (mqttMsgPublishEventArgs.Topic == DeviceTopic.SetDeviceState + "/" + _deviceId) { _deviceState = MqttDeviceStateDto.Deserialize(mqttMsgPublishEventArgs.Message).CurrentState; btnOnOff.Dispatcher.Invoke(() => btnOnOff.Content = _deviceState ? "Switch to OFF" : "Switch to ON"); SetBackgroundColor(); _client.Publish(DeviceTopic.SetDeviceStateChanged + "/" + _deviceId, (new MqttDeviceStateDto(_deviceState)).Serialize(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); } }