Beispiel #1
0
        public async Task <string> Handle(UpdateMqttDeviceCommand request, CancellationToken cancellationToken)
        {
            var mqttDevice = new MqttDevice()
            {
                Id = request.Id, IsAvailable = request.isAvailable, IsConfirmed = request.isConfirmed
            };

            _unitOfWork.MqttDevice.Update(mqttDevice);
            await _unitOfWork.SaveAsync();

            return(mqttDevice.Id);
        }
Beispiel #2
0
        private async Task LoadConfigurationAsync()
        {
            var deviceSection = configuration.GetSection("devices").GetChildren();

            foreach (var item in deviceSection)
            {
                try
                {
                    var node = item.GetChildren();

                    var device = new MqttDevice
                    {
                        // generic
                        ClientId = GetSettingsParam(node, "clientID"),

                        // mqtt
                        TopicName  = GetSettingsParam(node, "TopicName"),
                        TopicValue = GetSettingsParam(node, "TopicValue"),
                        TopicPath  = GetSettingsParam(node, "TopicPath"),

                        AlternateTopicName = GetSettingsParam(node, "AlternateTopicName"),
                        AlternateTopicPath = GetSettingsParam(node, "AlternateTopicPath"),

                        // vera/openluup
                        DeviceID = Convert.ToInt32(GetSettingsParam(node, "deviceID")),
                        Service  = GetSettingsParam(node, "Service"),
                        Variable = GetSettingsParam(node, "Variable"),
                        Value    = GetSettingsParam(node, "Value")
                    };

                    // TODO: enforce config check at startups
                    this.devices.Add(device);

                    Log.Verbose("[{where}] MQTT Client Configuration loaded: {a}", nameof(MQTTServer), device.ClientId);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "[{where}] {what}.LoadConfiguration MQTT Server...", nameof(MQTTServer), nameof(StartAsync));
                }
            }

            await Task.CompletedTask;
        }
Beispiel #3
0
        private async Task ProcessMessageAsync(MqttDevice device, MqttApplicationMessage applicationMessage)
        {
            string value;
            bool   matched;

            // topic path
            if (string.IsNullOrEmpty(device.TopicPath))
            {
                var messageValue = applicationMessage.ConvertPayloadToString();
                matched = messageValue.Equals(device.TopicValue, StringComparison.InvariantCultureIgnoreCase);
                value   = device.Value;

                Log.Verbose("[{where}] Processed message: got {messageValue} - expected {value} - matched: {matched}", nameof(MQTTServer), messageValue, value, matched);
            }
            // read the value from the payload, using the path
            else
            {
                var json       = applicationMessage.ConvertPayloadToString();
                var jsonObject = JObject.Parse(json);
                value = device.TopicPath == "." ? json : (string)jsonObject.SelectToken(device.TopicPath);

                if (!string.IsNullOrEmpty(device.AlternateTopicPath) && string.IsNullOrEmpty(value))
                {
                    value = device.AlternateTopicPath == "." ? json : (string)jsonObject.SelectToken(device.AlternateTopicPath);
                }

                matched = true;

                Log.Verbose("[{where}] Processed message: got {value}", nameof(MQTTServer), value);
            }

            if (matched)
            {
                Log.Information("[{where}] Updating #{deviceID} - {service} / {var} - {value}", nameof(MQTTServer), device.DeviceID, device.Service, device.Variable, value);
                await luupWrapper.UpdateVariablesAsync(device.DeviceID, device.Service, device.Variable, value);
            }
        }