From dc675d12d9f61dd35df77266824572b1ac8a5671 Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Wed, 13 Dec 2023 22:04:15 -0500 Subject: [PATCH] Added support for MQTT Adapter Devices and Assets --- .../Module.cs | 46 ++++++++++++++++++- .../README-Nuget.md | 2 + .../README.md | 2 + 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/Module.cs b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/Module.cs index 765e7e54..e25d9a6f 100644 --- a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/Module.cs +++ b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/Module.cs @@ -60,7 +60,10 @@ private async Task Worker() try { // Declare new MQTT Client Options with Tcp Server - var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(_configuration.Server, _configuration.Port); + var clientOptionsBuilder = new MqttClientOptionsBuilder(); + + // Set TCP Settings + clientOptionsBuilder.WithTcpServer(_configuration.Server, _configuration.Port); // Set Client ID if (!string.IsNullOrEmpty(_configuration.ClientId)) @@ -155,9 +158,13 @@ public override bool AddObservations(IEnumerable observations if (formatResult.Success) { var message = new MqttApplicationMessage(); - message.Topic = $"{_configuration.Topic}/{_configuration.DeviceKey}/observations"; + message.Retain = true; + message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS; + message.Topic = $"{_configuration.Topic}/observations"; message.PayloadSegment = formatResult.Content; _mqttClient.PublishAsync(message); + + Log(MTConnectLogLevel.Debug, $"MQTT Observations Message Published to {message.Topic}"); } } @@ -166,11 +173,46 @@ public override bool AddObservations(IEnumerable observations public override bool AddAssets(IEnumerable assets) { + if (_mqttClient != null && !assets.IsNullOrEmpty()) + { + var formatResult = InputFormatter.Format(_configuration.DocumentFormat, assets); + if (formatResult.Success) + { + var message = new MqttApplicationMessage(); + message.Retain = true; + message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS; + message.Topic = $"{_configuration.Topic}/assets"; + message.PayloadSegment = formatResult.Content; + _mqttClient.PublishAsync(message); + + Log(MTConnectLogLevel.Debug, $"MQTT Assets Message Published to {message.Topic}"); + } + } + return true; } public override bool AddDevices(IEnumerable devices) { + if (_mqttClient != null && !devices.IsNullOrEmpty()) + { + foreach (var device in devices) + { + var formatResult = InputFormatter.Format(_configuration.DocumentFormat, device); + if (formatResult.Success) + { + var message = new MqttApplicationMessage(); + message.Retain = true; + message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS; + message.Topic = $"{_configuration.Topic}/device"; + message.PayloadSegment = formatResult.Content; + _mqttClient.PublishAsync(message); + + Log(MTConnectLogLevel.Debug, $"MQTT Device Message Published to {message.Topic}"); + } + } + } + return true; } diff --git a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README-Nuget.md b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README-Nuget.md index c0e55985..d4993b21 100644 --- a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README-Nuget.md +++ b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README-Nuget.md @@ -3,6 +3,8 @@ # MTConnect MQTT Adapter Module This Adapter Module sends input data to an MQTT Broker that can be read by an MTConnect Agent +>This Module is still under development. Please feel free to leave feedback or create Issues on GitHub. + ## Configuration ```yaml - mqtt: diff --git a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README.md b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README.md index 0cf2b41f..cb6c726d 100644 --- a/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README.md +++ b/adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README.md @@ -3,6 +3,8 @@ # MTConnect MQTT Adapter Module This Adapter Module sends input data to an MQTT Broker that can be read by an MTConnect Agent +>This Module is still under development. Please feel free to leave feedback or create Issues on GitHub. + ## Nuget