From 7b134b39deff31343d2da4ceedb3eb1e69553aed Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:11 +0200 Subject: [PATCH 1/6] Update MQTTnet package to version 4.3.6.1152 This commit upgrades the MQTTnet package from version 3.0.9 to 4.3.6.1152 in both the main API project and the test project. This update is intended to leverage new features and improvements in the latest version of MQTTnet. --- .../agrirouter-sdk-dotnet-standard-api.csproj | 2 +- .../agrirouter-sdk-dotnet-standard-test.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/agrirouter-sdk-dotnet-standard-api/agrirouter-sdk-dotnet-standard-api.csproj b/agrirouter-sdk-dotnet-standard-api/agrirouter-sdk-dotnet-standard-api.csproj index 12a6f40..5e1b67a 100644 --- a/agrirouter-sdk-dotnet-standard-api/agrirouter-sdk-dotnet-standard-api.csproj +++ b/agrirouter-sdk-dotnet-standard-api/agrirouter-sdk-dotnet-standard-api.csproj @@ -18,7 +18,7 @@ - + diff --git a/agrirouter-sdk-dotnet-standard-test/agrirouter-sdk-dotnet-standard-test.csproj b/agrirouter-sdk-dotnet-standard-test/agrirouter-sdk-dotnet-standard-test.csproj index 0c476e7..2494af5 100644 --- a/agrirouter-sdk-dotnet-standard-test/agrirouter-sdk-dotnet-standard-test.csproj +++ b/agrirouter-sdk-dotnet-standard-test/agrirouter-sdk-dotnet-standard-test.csproj @@ -25,7 +25,7 @@ - + From 50fe3c08f480dc458b28b9cf8029b6d1efbd86b4 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:25 +0200 Subject: [PATCH 2/6] Update MQTT message handling in CapabilitiesServiceTest Refactor MQTT message handling to use the ApplicationMessageReceivedAsync event instead of UseApplicationMessageReceivedHandler. This aligns with updated MQTTnet library practices and ensures better async event handling. Removed the unnecessary MQTTnet.Client import. --- .../Service/Messaging/Mqtt/CapabilitiesServiceTest.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/CapabilitiesServiceTest.cs b/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/CapabilitiesServiceTest.cs index bd53222..e7fc5e4 100644 --- a/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/CapabilitiesServiceTest.cs +++ b/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/CapabilitiesServiceTest.cs @@ -12,7 +12,6 @@ using Agrirouter.Test.Data; using Agrirouter.Test.Helper; using MQTTnet; -using MQTTnet.Client; using Xunit; using Agrirouter.Api.Dto.Messaging; using Newtonsoft.Json; @@ -52,15 +51,17 @@ public async void var messageReceived = false; var counter = 0; - mqttClient.UseApplicationMessageReceivedHandler(e => + mqttClient.ApplicationMessageReceivedAsync += async e => { messageReceived = true; - MessageResponse msg = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); + MessageResponse msg = + JsonConvert.DeserializeObject( + Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); var decodedMessage = DecodeMessageService.Decode(msg.Command.Message); Assert.Equal(201, decodedMessage.ResponseEnvelope.ResponseCode); - }); + }; while (!messageReceived && counter < 5) { From c1fe67796654e636c55071d330f5ddf53b77b754 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:32 +0200 Subject: [PATCH 3/6] Update MQTTnet namespace import. Switched from MQTTnet.Client.Publishing to MQTTnet.Client to resolve namespace discrepancies and ensure compatibility with updated library versions. --- .../Exception/CouldNotSendMqttMessageException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agrirouter-sdk-dotnet-standard-api/Exception/CouldNotSendMqttMessageException.cs b/agrirouter-sdk-dotnet-standard-api/Exception/CouldNotSendMqttMessageException.cs index 3d03698..1673aa5 100644 --- a/agrirouter-sdk-dotnet-standard-api/Exception/CouldNotSendMqttMessageException.cs +++ b/agrirouter-sdk-dotnet-standard-api/Exception/CouldNotSendMqttMessageException.cs @@ -1,5 +1,5 @@ using System; -using MQTTnet.Client.Publishing; +using MQTTnet.Client; namespace Agrirouter.Api.Exception { From f59799ddc7975920d51803e4bdac5efce4a71e75 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:38 +0200 Subject: [PATCH 4/6] Update MQTT connection timeout method Replaced deprecated `.WithCommunicationTimeout()` method with `.WithTimeout()` for setting MQTT communication timeout. Removed unused `MQTTnet.Client.Options` import statement for cleanup. --- .../Helper/MqttConnectionHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/agrirouter-sdk-dotnet-standard-test/Helper/MqttConnectionHelper.cs b/agrirouter-sdk-dotnet-standard-test/Helper/MqttConnectionHelper.cs index 04d4f4b..e2177f0 100644 --- a/agrirouter-sdk-dotnet-standard-test/Helper/MqttConnectionHelper.cs +++ b/agrirouter-sdk-dotnet-standard-test/Helper/MqttConnectionHelper.cs @@ -6,7 +6,6 @@ using Agrirouter.Api.Dto.Onboard; using Agrirouter.Impl.Service.Common; using MQTTnet.Client; -using MQTTnet.Client.Options; using Xunit; namespace Agrirouter.Test.Helper @@ -47,7 +46,7 @@ public static async Task ConnectMqttClient(IMqttClient mqttClient, OnboardRespon .WithTcpServer(onboardResponse.ConnectionCriteria.Host, int.Parse(onboardResponse.ConnectionCriteria.Port)) .WithTls(tlsParameters) - .WithCommunicationTimeout(TimeSpan.FromSeconds(20)) + .WithTimeout(TimeSpan.FromSeconds(20)) .Build(); await mqttClient.ConnectAsync(options); From 7a089647f3bcbf2771467767dc7acc44b67e1b6e Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:44 +0200 Subject: [PATCH 5/6] Refactor MQTT message publishing for clarity and consistency Refactor the MQTT message publishing process by switching to the appropriate MQTTnet.Protocol namespace and improving code readability. Utilize the `MqttQualityOfServiceLevel.ExactlyOnce` for enhanced clarity and ensure proper formatting and spacing throughout. --- .../Service/Common/MqttMessagingService.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/agrirouter-sdk-dotnet-standard-impl/Service/Common/MqttMessagingService.cs b/agrirouter-sdk-dotnet-standard-impl/Service/Common/MqttMessagingService.cs index 2796506..02bdc68 100644 --- a/agrirouter-sdk-dotnet-standard-impl/Service/Common/MqttMessagingService.cs +++ b/agrirouter-sdk-dotnet-standard-impl/Service/Common/MqttMessagingService.cs @@ -9,7 +9,7 @@ using Agrirouter.Api.Service.Parameters; using MQTTnet; using MQTTnet.Client; -using MQTTnet.Client.Publishing; +using MQTTnet.Protocol; using Newtonsoft.Json; namespace Agrirouter.Impl.Service.Common @@ -25,7 +25,7 @@ public IMqttClient MqttClient { get { return _mqttClient; } } - + /// /// Constructor. /// @@ -61,11 +61,13 @@ public async Task SendAsync(MessagingParameters messagingParame var mqttMessage = BuildMqttApplicationMessage(messagingParameters); var response = await _mqttClient.PublishAsync(mqttMessage, CancellationToken.None); - if (response.ReasonCode != MqttClientPublishReasonCode.Success) { + if (response.ReasonCode != MqttClientPublishReasonCode.Success) + { throw new CouldNotSendMqttMessageException(response.ReasonCode, response.ReasonString); } - return new MessagingResultBuilder().WithApplicationMessageId(messagingParameters.ApplicationMessageId).Build(); + return new MessagingResultBuilder().WithApplicationMessageId(messagingParameters.ApplicationMessageId) + .Build(); } private static MqttApplicationMessage BuildMqttApplicationMessage(MessagingParameters messagingParameters) @@ -78,8 +80,8 @@ private static MqttApplicationMessage BuildMqttApplicationMessage(MessagingParam }; foreach (var message in messagingParameters.EncodedMessages.Select(encodedMessage => - new Api.Dto.Messaging.Inner.Message - {Content = encodedMessage, Timestamp = UtcDataService.NowAsUnixTimestamp()})) + new Api.Dto.Messaging.Inner.Message + { Content = encodedMessage, Timestamp = UtcDataService.NowAsUnixTimestamp() })) messageRequest.Messages.Add(message); var messagePayload = JsonConvert.SerializeObject(messageRequest); @@ -87,7 +89,7 @@ private static MqttApplicationMessage BuildMqttApplicationMessage(MessagingParam var mqttMessage = new MqttApplicationMessageBuilder() .WithTopic(messagingParameters.OnboardResponse.ConnectionCriteria.Measures) .WithPayload(messagePayload) - .WithExactlyOnceQoS() + .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce) .WithRetainFlag() .Build(); return mqttMessage; From 7e2b3e04ad00535ed053a92559b6d4b44e46f4ab Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 20 Aug 2024 21:02:50 +0200 Subject: [PATCH 6/6] Switch to async handler for MQTT message receiving Updated the application message handling in PingServiceTest to use the asynchronous event handler. This ensures better responsiveness and non-blocking execution while verifying response codes in the unit tests. --- .../Service/Messaging/Mqtt/PingServiceTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/PingServiceTest.cs b/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/PingServiceTest.cs index 3f6d2b3..0b6ae0a 100644 --- a/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/PingServiceTest.cs +++ b/agrirouter-sdk-dotnet-standard-test/Service/Messaging/Mqtt/PingServiceTest.cs @@ -38,7 +38,7 @@ public async void var messageReceived = false; var counter = 0; - mqttClient.UseApplicationMessageReceivedHandler(e => + mqttClient.ApplicationMessageReceivedAsync += async e => { messageReceived = true; @@ -47,7 +47,7 @@ public async void var decodedMessage = DecodeMessageService.Decode(msg.Command.Message); Assert.Equal(200, decodedMessage.ResponseEnvelope.ResponseCode); - }); + }; while (!messageReceived && counter < 5) { @@ -90,7 +90,7 @@ public async void var messageReceived = false; var counter = 0; - mqttClient.UseApplicationMessageReceivedHandler(e => + mqttClient.ApplicationMessageReceivedAsync += async e => { messageReceived = true; @@ -100,7 +100,7 @@ public async void // your own application should remove the endpoint from your endpoint list/registry now! Assert.Equal(400, decodedMessage.ResponseEnvelope.ResponseCode); - }); + }; while (!messageReceived && counter < 5) {