From 67003f8cbc18aef12a5bc799cab7a7a4188c432b Mon Sep 17 00:00:00 2001 From: Thomas Hepp Date: Tue, 5 Aug 2025 11:25:11 +0200 Subject: [PATCH] Upgraded `MQTTnet` from version `4.3.7.1207` to `5.0.1.1416` and `protobuf-net` from version `3.2.45` to `3.2.56 --- src/SparkplugNet.Tests/GlobalUsings.cs | 1 + .../SparkplugMessageGeneratorTestVersion30.cs | 4 ++-- .../Core/Application/SparkplugApplicationBase.cs | 4 ++-- src/SparkplugNet/Core/Node/SparkplugNodeBase.cs | 8 ++++---- src/SparkplugNet/Core/PayloadHelper.cs | 16 ++++++++++++++++ src/SparkplugNet/Core/SparkplugBase.cs | 2 +- src/SparkplugNet/GlobalUsings.cs | 3 +-- src/SparkplugNet/SparkplugNet.csproj | 4 ++-- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/SparkplugNet.Tests/GlobalUsings.cs b/src/SparkplugNet.Tests/GlobalUsings.cs index 08dc9a0..7721064 100644 --- a/src/SparkplugNet.Tests/GlobalUsings.cs +++ b/src/SparkplugNet.Tests/GlobalUsings.cs @@ -1,5 +1,6 @@ #pragma warning disable IDE0065 // Die using-Anweisung wurde falsch platziert. global using System.Text.Json; +global using System.Buffers; global using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/src/SparkplugNet.Tests/Messages/SparkplugMessageGeneratorTestVersion30.cs b/src/SparkplugNet.Tests/Messages/SparkplugMessageGeneratorTestVersion30.cs index 15ac569..d7a2ceb 100644 --- a/src/SparkplugNet.Tests/Messages/SparkplugMessageGeneratorTestVersion30.cs +++ b/src/SparkplugNet.Tests/Messages/SparkplugMessageGeneratorTestVersion30.cs @@ -40,7 +40,7 @@ public sealed class SparkplugMessageGeneratorTestVersion30 public void TestStateMessageNamespaceBOnline() { var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", true); - var payloadVersionB = JsonSerializer.Deserialize(message.Payload); + var payloadVersionB = JsonSerializer.Deserialize(message.Payload.ToArray()); Assert.AreEqual("spBv1.0/STATE/scada1", message.Topic); Assert.IsNotNull(payloadVersionB); @@ -56,7 +56,7 @@ public void TestStateMessageNamespaceBOnline() public void TestStateMessageNamespaceBOffline() { var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", false); - var payloadVersionB = JsonSerializer.Deserialize(message.Payload); + var payloadVersionB = JsonSerializer.Deserialize(message.Payload.ToArray()); Assert.AreEqual("spBv1.0/STATE/scada1", message.Topic); Assert.IsNotNull(payloadVersionB); diff --git a/src/SparkplugNet/Core/Application/SparkplugApplicationBase.cs b/src/SparkplugNet/Core/Application/SparkplugApplicationBase.cs index c140274..d864c41 100644 --- a/src/SparkplugNet/Core/Application/SparkplugApplicationBase.cs +++ b/src/SparkplugNet/Core/Application/SparkplugApplicationBase.cs @@ -293,7 +293,7 @@ private Task OnApplicationMessageReceived(MqttApplicationMessageReceivedEventArg if (SparkplugMessageTopic.TryParse(topic, out var topicParsed)) { - var data = args.ApplicationMessage.PayloadSegment.Array ?? []; + var data = args.ApplicationMessage.Payload.ToArray(); return this.OnMessageReceived(topicParsed!, data); } else if (topic.Contains(SparkplugMessageType.StateMessage.GetDescription())) @@ -381,7 +381,7 @@ private async Task ConnectInternal() builder.WithWillContentType(willMessage.ContentType); builder.WithWillCorrelationData(willMessage.CorrelationData); builder.WithWillDelayInterval(willMessage.MessageExpiryInterval); - builder.WithWillPayload(willMessage.PayloadSegment); + builder.WithWillPayload(willMessage.Payload.ToArray()); builder.WithWillPayloadFormatIndicator(willMessage.PayloadFormatIndicator); builder.WithWillQualityOfServiceLevel(willMessage.QualityOfServiceLevel); builder.WithWillResponseTopic(willMessage.ResponseTopic); diff --git a/src/SparkplugNet/Core/Node/SparkplugNodeBase.cs b/src/SparkplugNet/Core/Node/SparkplugNodeBase.cs index 27df22d..4f0bd60 100644 --- a/src/SparkplugNet/Core/Node/SparkplugNodeBase.cs +++ b/src/SparkplugNet/Core/Node/SparkplugNodeBase.cs @@ -234,17 +234,17 @@ private async Task OnApplicationMessageReceived(MqttApplicationMessageReceivedEv if (SparkplugMessageTopic.TryParse(topic, out var messageTopic)) { - var data = args.ApplicationMessage.PayloadSegment.Array ?? []; + var data = args.ApplicationMessage.Payload.ToArray(); await this.OnMessageReceived(messageTopic!, data); } else if (topic.Contains(SparkplugMessageType.StateMessage.GetDescription())) { // Handle the STATE message before anything else as they're UTF-8 encoded. - await this.FireStatusMessageReceived(Encoding.UTF8.GetString(args.ApplicationMessage.PayloadSegment)); + await this.FireStatusMessageReceived(args.ApplicationMessage.ConvertPayloadToString()); } else { - throw new InvalidOperationException($"Received message on unkown topic {topic}: {args.ApplicationMessage.PayloadSegment:X2}."); + throw new InvalidOperationException($"Received message on unkown topic {topic}: {args.ApplicationMessage.Payload.ToArray():X2}."); } } @@ -318,7 +318,7 @@ private async Task ConnectInternal() builder.WithWillContentType(willMessage.ContentType); builder.WithWillCorrelationData(willMessage.CorrelationData); builder.WithWillDelayInterval(willMessage.MessageExpiryInterval); - builder.WithWillPayload(willMessage.PayloadSegment); + builder.WithWillPayload(willMessage.Payload.ToArray()); builder.WithWillPayloadFormatIndicator(willMessage.PayloadFormatIndicator); builder.WithWillQualityOfServiceLevel(willMessage.QualityOfServiceLevel); builder.WithWillResponseTopic(willMessage.ResponseTopic); diff --git a/src/SparkplugNet/Core/PayloadHelper.cs b/src/SparkplugNet/Core/PayloadHelper.cs index cb00e6f..56dd262 100644 --- a/src/SparkplugNet/Core/PayloadHelper.cs +++ b/src/SparkplugNet/Core/PayloadHelper.cs @@ -50,6 +50,22 @@ internal static class PayloadHelper return Serializer.Deserialize(stream); } + /// + /// Deserializes the data to a proto payload. + /// + /// The type. + /// The data. + /// The value as deserialized object. + internal static T? Deserialize(ReadOnlySequence data) where T : class + { + if (data.IsEmpty) + { + return null; + } + + return Serializer.Deserialize(data); + } + // Todo: Adjust to callback via action / Func? /// /// The write bytes delegate. diff --git a/src/SparkplugNet/Core/SparkplugBase.cs b/src/SparkplugNet/Core/SparkplugBase.cs index 7e0b7da..319e097 100644 --- a/src/SparkplugNet/Core/SparkplugBase.cs +++ b/src/SparkplugNet/Core/SparkplugBase.cs @@ -65,7 +65,7 @@ public SparkplugBase(KnownMetricStorage knownMetricsStorage, SparkplugSpecificat this.NameSpace = SparkplugNamespace.VersionB; } - this.client = new MqttFactory().CreateMqttClient(); + this.client = new MqttClientFactory().CreateMqttClient(); this.messageGenerator = new SparkplugMessageGenerator(specificationVersion); } diff --git a/src/SparkplugNet/GlobalUsings.cs b/src/SparkplugNet/GlobalUsings.cs index 1732fb1..8aa0428 100644 --- a/src/SparkplugNet/GlobalUsings.cs +++ b/src/SparkplugNet/GlobalUsings.cs @@ -1,4 +1,5 @@ #pragma warning disable IDE0065 // Die using-Anweisung wurde falsch platziert. +global using System.Buffers; global using System.Buffers.Binary; global using System.Collections.Concurrent; global using System.ComponentModel; @@ -13,7 +14,6 @@ global using Microsoft.Extensions.Logging; global using MQTTnet; -global using MQTTnet.Client; global using MQTTnet.Formatter; global using MQTTnet.Internal; global using MQTTnet.Protocol; @@ -34,7 +34,6 @@ global using VersionAData = SparkplugNet.VersionA.Data; global using VersionAProtoBuf = SparkplugNet.VersionA.ProtoBuf; -global using VersionBData = SparkplugNet.VersionB.Data; global using VersionBProtoBuf = SparkplugNet.VersionB.ProtoBuf; global using VersionADataTypeEnum = SparkplugNet.VersionA.Data.DataType; diff --git a/src/SparkplugNet/SparkplugNet.csproj b/src/SparkplugNet/SparkplugNet.csproj index db86500..765e34c 100644 --- a/src/SparkplugNet/SparkplugNet.csproj +++ b/src/SparkplugNet/SparkplugNet.csproj @@ -40,8 +40,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - + +