From 4f6a082379e3bdb5fa4e9a1cd84e1973b25d483e Mon Sep 17 00:00:00 2001 From: Niels Geens Date: Tue, 13 Aug 2024 10:50:26 +0200 Subject: [PATCH 1/2] Fix metricState only updated when dataReceived handler is attached --- src/SparkplugNet/VersionB/SparkplugApplication.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/SparkplugNet/VersionB/SparkplugApplication.cs b/src/SparkplugNet/VersionB/SparkplugApplication.cs index 76f896b..c295386 100644 --- a/src/SparkplugNet/VersionB/SparkplugApplication.cs +++ b/src/SparkplugNet/VersionB/SparkplugApplication.cs @@ -210,7 +210,7 @@ await this.FireDeviceBirthReceived(topic.GroupIdentifier, topic.EdgeNodeIdentifi /// The metric status. /// Thrown if the edge node identifier is invalid. /// Thrown if the metric cast is invalid. - private IEnumerable ProcessPayload(SparkplugMessageTopic topic, List metrics, SparkplugMetricStatus metricStatus) + private List ProcessPayload(SparkplugMessageTopic topic, List metrics, SparkplugMetricStatus metricStatus) { var metricState = new MetricState { @@ -231,6 +231,7 @@ private IEnumerable ProcessPayload(SparkplugMessageTopic topic, List(); foreach (var payloadMetric in metrics) { if (payloadMetric is not Metric convertedMetric) @@ -243,7 +244,9 @@ private IEnumerable ProcessPayload(SparkplugMessageTopic topic, List Date: Tue, 13 Aug 2024 10:51:25 +0200 Subject: [PATCH 2/2] Fix metricState reinitialized on partial update --- .../VersionB/SparkplugApplication.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/SparkplugNet/VersionB/SparkplugApplication.cs b/src/SparkplugNet/VersionB/SparkplugApplication.cs index c295386..c755cfe 100644 --- a/src/SparkplugNet/VersionB/SparkplugApplication.cs +++ b/src/SparkplugNet/VersionB/SparkplugApplication.cs @@ -224,11 +224,31 @@ private List ProcessPayload(SparkplugMessageTopic topic, List me throw new InvalidOperationException($"The edge node identifier is invalid for device {topic.DeviceIdentifier}."); } - this.DeviceStates[$"{topic.EdgeNodeIdentifier}/{topic.DeviceIdentifier}"] = metricState; + this.DeviceStates.AddOrUpdate( + $"{topic.EdgeNodeIdentifier}/{topic.DeviceIdentifier}", + metricState, + (_, previousMetricState) => { + metricState = previousMetricState; + metricState.MetricStatus = metricStatus; + return metricState; + }); } else { - this.NodeStates[topic.EdgeNodeIdentifier] = metricState; + metricState = new MetricState + { + MetricStatus = metricStatus + }; + this.NodeStates.AddOrUpdate( + topic.EdgeNodeIdentifier, + metricState, + (_, previousMetricState) => + { + metricState = previousMetricState; + metricState.MetricStatus = metricStatus; + return metricState; + } + ); } var result = new List();