Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SparkplugNet.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public sealed class SparkplugMessageGeneratorTestVersion30
public void TestStateMessageNamespaceBOnline()
{
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", true);
var payloadVersionB = JsonSerializer.Deserialize<StateMessage>(message.Payload);
var payloadVersionB = JsonSerializer.Deserialize<StateMessage>(message.Payload.ToArray());

Assert.AreEqual("spBv1.0/STATE/scada1", message.Topic);
Assert.IsNotNull(payloadVersionB);
Expand All @@ -56,7 +56,7 @@ public void TestStateMessageNamespaceBOnline()
public void TestStateMessageNamespaceBOffline()
{
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", false);
var payloadVersionB = JsonSerializer.Deserialize<StateMessage>(message.Payload);
var payloadVersionB = JsonSerializer.Deserialize<StateMessage>(message.Payload.ToArray());

Assert.AreEqual("spBv1.0/STATE/scada1", message.Topic);
Assert.IsNotNull(payloadVersionB);
Expand Down
4 changes: 2 additions & 2 deletions src/SparkplugNet/Core/Application/SparkplugApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/SparkplugNet/Core/Node/SparkplugNodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.");
}
}

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/SparkplugNet/Core/PayloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ internal static class PayloadHelper
return Serializer.Deserialize<T>(stream);
}

/// <summary>
/// Deserializes the data to a proto payload.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="data">The data.</param>
/// <returns>The <see cref="T:T?"/> value as deserialized object.</returns>
internal static T? Deserialize<T>(ReadOnlySequence<byte> data) where T : class
{
if (data.IsEmpty)
{
return null;
}

return Serializer.Deserialize<T>(data);
}

// Todo: Adjust to callback via action / Func?
/// <summary>
/// The write bytes delegate.
Expand Down
2 changes: 1 addition & 1 deletion src/SparkplugNet/Core/SparkplugBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/SparkplugNet/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/SparkplugNet/SparkplugNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MQTTnet" Version="4.3.7.1207" />
<PackageReference Include="protobuf-net" Version="3.2.45" />
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
<PackageReference Include="protobuf-net" Version="3.2.56" />
</ItemGroup>

<ItemGroup>
Expand Down