Ejemplo n.º 1
0
        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());
            }
        }
Ejemplo n.º 2
0
 public static ConnectSingleton getInstance(bool IsConnected)
 {
     if (instance == null)
     {
         instance = new ConnectSingleton(IsConnected);
     }
     return(instance);
 }
Ejemplo n.º 3
0
        // обновление документа
        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);
        }