Skip to content

Commit

Permalink
Added Sparkplug spec version to differentiate between state messages …
Browse files Browse the repository at this point in the history
…(Maybe later, more things need to be checked). Fixes #68 partially --> Issue 3 done.
  • Loading branch information
SeppPenner committed Feb 20, 2024
1 parent 428a360 commit 8ab456b
Show file tree
Hide file tree
Showing 16 changed files with 647 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SparkplugMessageGeneratorTest.cs" company="Hämmer Electronics">
// <copyright file="SparkplugMessageGeneratorTestVersion22.cs" company="Hämmer Electronics">
// The project is licensed under the MIT license.
// </copyright>
// <summary>
// A class to test the <see cref="SparkplugMessageGenerator"/> class.
// A class to test the <see cref="SparkplugMessageGenerator"/> class with specification version 2.2.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SparkplugNet.Tests;

/// <summary>
/// A class to test the <see cref="SparkplugMessageGenerator"/> class.
/// A class to test the <see cref="SparkplugMessageGenerator"/> class with specification version 2.2.
/// </summary>
[TestClass]
public class SparkplugMessageGeneratorTest
public class SparkplugMessageGeneratorTestVersion22
{
/// <summary>
/// The metrics for namespace A.
Expand Down Expand Up @@ -64,15 +64,16 @@ public class SparkplugMessageGeneratorTest
/// <summary>
/// The message generator.
/// </summary>
private readonly SparkplugMessageGenerator messageGenerator = new(new LoggerConfiguration().WriteTo.Console().CreateLogger());
private readonly SparkplugMessageGenerator messageGenerator = new(new LoggerConfiguration().WriteTo.Console().CreateLogger(),
SparkplugSpecificationVersion.Version22);

/// <summary>
/// Tests the Sparkplug message generator with a message with a version A namespace and a online state.
/// </summary>
[TestMethod]
public void TestStateMessageNamespaceAOnline()
{
var message = SparkplugMessageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionA, "scada1", true);
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionA, "scada1", true);

Assert.AreEqual("STATE/scada1", message.Topic);
Assert.AreEqual("ONLINE", message.ConvertPayloadToString());
Expand All @@ -84,7 +85,7 @@ public void TestStateMessageNamespaceAOnline()
[TestMethod]
public void TestStateMessageNamespaceAOffline()
{
var message = SparkplugMessageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionA, "scada1", false);
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionA, "scada1", false);

Assert.AreEqual("STATE/scada1", message.Topic);
Assert.AreEqual("OFFLINE", message.ConvertPayloadToString());
Expand All @@ -96,7 +97,7 @@ public void TestStateMessageNamespaceAOffline()
[TestMethod]
public void TestStateMessageNamespaceBOnline()
{
var message = SparkplugMessageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", true);
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", true);

Assert.AreEqual("STATE/scada1", message.Topic);
Assert.AreEqual("ONLINE", message.ConvertPayloadToString());
Expand All @@ -108,7 +109,7 @@ public void TestStateMessageNamespaceBOnline()
[TestMethod]
public void TestStateMessageNamespaceBOffline()
{
var message = SparkplugMessageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", false);
var message = this.messageGenerator.GetSparkplugStateMessage(SparkplugNamespace.VersionB, "scada1", false);

Assert.AreEqual("STATE/scada1", message.Topic);
Assert.AreEqual("OFFLINE", message.ConvertPayloadToString());
Expand Down Expand Up @@ -389,7 +390,7 @@ public void TestNodeDataMessageNamespaceB()
public void TestDeviceCommandMessageNamespaceA()
{
var dateTime = DateTimeOffset.Now;
var message = SparkplugMessageGenerator.GetSparkPlugDeviceCommandMessage(SparkplugNamespace.VersionA, "group1", "edge1", "device1", this.metricsA, 0, 1, dateTime);
var message = this.messageGenerator.GetSparkPlugDeviceCommandMessage(SparkplugNamespace.VersionA, "group1", "edge1", "device1", this.metricsA, 0, 1, dateTime);
var payloadVersionA = PayloadHelper.Deserialize<VersionAProtoBufPayload>(message.Payload);

Assert.AreEqual("spAv1.0/group1/DCMD/edge1/device1", message.Topic);
Expand All @@ -413,7 +414,7 @@ public void TestDeviceCommandMessageNamespaceA()
public void TestDeviceCommandMessageNamespaceB()
{
var dateTime = DateTimeOffset.UtcNow;
var message = SparkplugMessageGenerator.GetSparkPlugDeviceCommandMessage(SparkplugNamespace.VersionB, "group1", "edge1", "device1", this.metricsB, 0, 1, dateTime);
var message = this.messageGenerator.GetSparkPlugDeviceCommandMessage(SparkplugNamespace.VersionB, "group1", "edge1", "device1", this.metricsB, 0, 1, dateTime);
var payloadVersionB = PayloadHelper.Deserialize<VersionBProtoBufPayload>(message.Payload);

Assert.AreEqual("spBv1.0/group1/DCMD/edge1/device1", message.Topic);
Expand All @@ -437,7 +438,7 @@ public void TestDeviceCommandMessageNamespaceB()
public void TestNodeCommandMessageNamespaceA()
{
var dateTime = DateTimeOffset.Now;
var message = SparkplugMessageGenerator.GetSparkPlugNodeCommandMessage(SparkplugNamespace.VersionA, "group1", "edge1", this.metricsA, 0, 1, dateTime);
var message = this.messageGenerator.GetSparkPlugNodeCommandMessage(SparkplugNamespace.VersionA, "group1", "edge1", this.metricsA, 0, 1, dateTime);
var payloadVersionA = PayloadHelper.Deserialize<VersionAProtoBufPayload>(message.Payload);

Assert.AreEqual("spAv1.0/group1/NCMD/edge1", message.Topic);
Expand All @@ -461,7 +462,7 @@ public void TestNodeCommandMessageNamespaceA()
public void TestNodeCommandMessageNamespaceB()
{
var dateTime = DateTimeOffset.UtcNow;
var message = SparkplugMessageGenerator.GetSparkPlugNodeCommandMessage(SparkplugNamespace.VersionB, "group1", "edge1", this.metricsB, 0, 1, dateTime);
var message = this.messageGenerator.GetSparkPlugNodeCommandMessage(SparkplugNamespace.VersionB, "group1", "edge1", this.metricsB, 0, 1, dateTime);
var payloadVersionB = PayloadHelper.Deserialize<VersionBProtoBufPayload>(message.Payload);

Assert.AreEqual("spBv1.0/group1/NCMD/edge1", message.Topic);
Expand Down
Loading

0 comments on commit 8ab456b

Please sign in to comment.