From 3e26bf20a7d8dd4a42040ff255e12783ed02fbff Mon Sep 17 00:00:00 2001 From: "victor.irizar" Date: Tue, 15 Oct 2024 13:08:07 +0200 Subject: [PATCH 1/2] MTConnectMqttEntityServer: set correct qos for CreateMessage variants --- libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs b/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs index 88b1ef42..9752b9e3 100644 --- a/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs +++ b/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs @@ -81,6 +81,7 @@ private MqttApplicationMessage CreateMessage(IDevice device) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); + messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); return messageBuilder.Build(); } } @@ -167,6 +168,7 @@ private MqttApplicationMessage CreateMessage(IObservation observation) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); + messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); return messageBuilder.Build(); } } @@ -198,6 +200,7 @@ private MqttApplicationMessage CreateMessage(IEnumerable observati messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); + messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); return messageBuilder.Build(); } } @@ -250,6 +253,7 @@ private MqttApplicationMessage CreateMessage(IAsset asset) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); + messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); return messageBuilder.Build(); } } From 0f156263da8de305f82f1bd6f1baa63f7529f55d Mon Sep 17 00:00:00 2001 From: Patrick Ritchie Date: Tue, 15 Oct 2024 22:24:50 -0400 Subject: [PATCH 2/2] Updated to use QoS read from configuration file --- .../Module.cs | 2 +- .../Module.cs | 2 +- .../IMTConnectMqttEntityServerConfiguration.cs | 5 +++++ .../MTConnectMqttEntityServerConfiguration.cs | 5 ++++- .../MTConnectMqttEntityServer.cs | 18 +++++++++++++----- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/agent/Modules/MTConnect.NET-AgentModule-MqttBroker/Module.cs b/agent/Modules/MTConnect.NET-AgentModule-MqttBroker/Module.cs index dbef75be..864dff49 100644 --- a/agent/Modules/MTConnect.NET-AgentModule-MqttBroker/Module.cs +++ b/agent/Modules/MTConnect.NET-AgentModule-MqttBroker/Module.cs @@ -56,7 +56,7 @@ public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base case MqttTopicStructure.Entity: - _entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat); + _entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat, _configuration.QoS); Agent.DeviceAdded += AgentDeviceAdded; Agent.ObservationAdded += AgentObservationAdded; Agent.AssetAdded += AgentAssetAdded; diff --git a/agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs b/agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs index d0720f99..68cf49a0 100644 --- a/agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs +++ b/agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs @@ -57,7 +57,7 @@ public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base case MqttTopicStructure.Entity: - _entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat); + _entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat, _configuration.QoS); Agent.DeviceAdded += AgentDeviceAdded; Agent.ObservationAdded += AgentObservationAdded; Agent.AssetAdded += AgentAssetAdded; diff --git a/libraries/MTConnect.NET-MQTT/Configurations/IMTConnectMqttEntityServerConfiguration.cs b/libraries/MTConnect.NET-MQTT/Configurations/IMTConnectMqttEntityServerConfiguration.cs index 54f97f19..9ddfed87 100644 --- a/libraries/MTConnect.NET-MQTT/Configurations/IMTConnectMqttEntityServerConfiguration.cs +++ b/libraries/MTConnect.NET-MQTT/Configurations/IMTConnectMqttEntityServerConfiguration.cs @@ -14,5 +14,10 @@ public interface IMTConnectMqttEntityServerConfiguration /// /// string DocumentFormat { get; } + + /// + /// + /// + int QoS { get; set; } } } diff --git a/libraries/MTConnect.NET-MQTT/Configurations/MTConnectMqttEntityServerConfiguration.cs b/libraries/MTConnect.NET-MQTT/Configurations/MTConnectMqttEntityServerConfiguration.cs index ee333e77..a5cf9db4 100644 --- a/libraries/MTConnect.NET-MQTT/Configurations/MTConnectMqttEntityServerConfiguration.cs +++ b/libraries/MTConnect.NET-MQTT/Configurations/MTConnectMqttEntityServerConfiguration.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 TrakHound Inc., All Rights Reserved. +// Copyright (c) 2024 TrakHound Inc., All Rights Reserved. // TrakHound Inc. licenses this file to you under the MIT license. namespace MTConnect.Configurations @@ -9,11 +9,14 @@ public class MTConnectMqttEntityServerConfiguration : IMTConnectMqttEntityServer public string DocumentFormat { get; set; } + public int QoS { get; set; } + public MTConnectMqttEntityServerConfiguration() { TopicPrefix = "MTConnect"; DocumentFormat = "JSON"; + QoS = 0; } } } diff --git a/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs b/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs index 9752b9e3..1f764016 100644 --- a/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs +++ b/libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs @@ -23,11 +23,12 @@ public class MTConnectMqttEntityServer public string TopicPrefix => _configuration.TopicPrefix; - public MTConnectMqttEntityServer(string topicPrefix = null, string documentFormat = DocumentFormat.JSON) + public MTConnectMqttEntityServer(string topicPrefix = null, string documentFormat = DocumentFormat.JSON, int qos = 0) { var configuration = new MTConnectMqttEntityServerConfiguration(); configuration.TopicPrefix = topicPrefix; configuration.DocumentFormat = documentFormat; + configuration.QoS = qos; _configuration = configuration; } @@ -81,7 +82,7 @@ private MqttApplicationMessage CreateMessage(IDevice device) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); - messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS)); return messageBuilder.Build(); } } @@ -168,7 +169,7 @@ private MqttApplicationMessage CreateMessage(IObservation observation) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); - messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS)); return messageBuilder.Build(); } } @@ -200,7 +201,7 @@ private MqttApplicationMessage CreateMessage(IEnumerable observati messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); - messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS)); return messageBuilder.Build(); } } @@ -253,12 +254,19 @@ private MqttApplicationMessage CreateMessage(IAsset asset) messageBuilder.WithTopic(topic); messageBuilder.WithPayload(formatResult.Content); messageBuilder.WithRetainFlag(true); - messageBuilder.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS)); return messageBuilder.Build(); } } return null; } + + private static MQTTnet.Protocol.MqttQualityOfServiceLevel GetQualityOfService(int qos) + { + if (qos == 1) return MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce; + else if (qos == 2) return MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce; + else return MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce; + } } } \ No newline at end of file