public async Task SetDeviceStatus(Client client, Device device) { if (device.TypeOfDevice == "Light") { if (ConnectSingleton.IsConnected == false) { await _mqttClientService.ConnectAsync(client.Id); await _mqttClientService.SubscribeAsync(device.Topic); ConnectSingleton.getInstance(true); } var messagePayload = new MqttApplicationMessageBuilder() .WithTopic(device.Topic) .WithPayload(System.Text.Encoding.UTF8.GetBytes("status")) .WithExactlyOnceQoS() .WithRetainFlag() .Build(); var newDevice = await _mqttClientService.ReceivePayload(messagePayload, device.ToMqtt()); await Devices.ReplaceOneAsync(new BsonDocument("_id", new ObjectId(device.Id)), newDevice.To()); } }
public static ConnectSingleton getInstance(bool IsConnected) { if (instance == null) { instance = new ConnectSingleton(IsConnected); } return(instance); }
// обновление документа public async Task UpdateFromUIAsync(Client client, Device device) { if (device.TypeOfDevice == "Light") { if (ConnectSingleton.IsConnected == false) { await _mqttClientService.ConnectAsync(client.Id); await _mqttClientService.SubscribeAsync(device.Topic); ConnectSingleton.getInstance(true); } var messagePayload = new MqttApplicationMessageBuilder() .WithTopic(device.Topic) .WithPayload(device.Payload) .WithExactlyOnceQoS() .WithRetainFlag() .Build(); await _mqttClientService.PublishAsync(messagePayload); } await Devices.ReplaceOneAsync(new BsonDocument("_id", new ObjectId(device.Id)), device); }