Example #1
0
 public static Device To(this DeviceMqtt @this)
 {
     return(new Device()
     {
         Id = @this.Id,
         Name = @this.Name,
         Topic = @this.Topic,
         TypeOfDevice = @this.TypeOfDevice,
         Payload = @this.Payload,
         Status = @this.Status,
         ClientId = @this.ClientId,
     });
 }
Example #2
0
        static public void Initialize()
        {
            if (Current == null)
            {
                var indexDirectory = Path.GetFullPath(WebConfigurationManager.AppSettings ["IndexDirectory"]);

                var devicesDirectory = Path.GetFullPath(WebConfigurationManager.AppSettings ["DevicesDirectory"]);

                var deviceManager = new DeviceManager(indexDirectory, devicesDirectory);

                var mqttDeviceName = WebConfigurationManager.AppSettings ["MqttDeviceName"] + "-" + Guid.NewGuid().ToString();
                var mqttHost       = WebConfigurationManager.AppSettings ["MqttHost"];
                var mqttUsername   = WebConfigurationManager.AppSettings ["MqttUsername"];
                var mqttPassword   = WebConfigurationManager.AppSettings ["MqttPassword"];
                var mqttPort       = Convert.ToInt32(WebConfigurationManager.AppSettings ["MqttPort"]);

                Current = new DeviceMqtt(deviceManager, mqttDeviceName, mqttHost, mqttUsername, mqttPassword, mqttPort);
            }

            if (!Current.IsConnected && !Current.IsConnecting)
            {
                Connect();
            }
        }
 private void HandleMessageReceived(DeviceMqtt device, MqttApplicationMessage applicationMessage)
 {
     device.Status = Encoding.UTF8.GetString(applicationMessage.Payload);
 }
        public async Task <DeviceMqtt> ReceivePayload(MqttApplicationMessage message, DeviceMqtt device)
        {
            if (clientMqtt != null)
            {
                await clientMqtt.PublishAsync(message);

                clientMqtt.UseApplicationMessageReceivedHandler(e => device.Status = Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
            }
            return(device);
        }