Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #106 Metrics only updated when dataReceived handler is attached and other metrics are removed #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions src/SparkplugNet/VersionB/SparkplugApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ await this.FireDeviceBirthReceived(topic.GroupIdentifier, topic.EdgeNodeIdentifi
/// <param name="metricStatus">The metric status.</param>
/// <exception cref="InvalidOperationException">Thrown if the edge node identifier is invalid.</exception>
/// <exception cref="InvalidCastException">Thrown if the metric cast is invalid.</exception>
private IEnumerable<Metric> ProcessPayload(SparkplugMessageTopic topic, List<Metric> metrics, SparkplugMetricStatus metricStatus)
private List<Metric> ProcessPayload(SparkplugMessageTopic topic, List<Metric> metrics, SparkplugMetricStatus metricStatus)
{
var metricState = new MetricState<Metric>
{
Expand All @@ -224,13 +224,34 @@ private IEnumerable<Metric> ProcessPayload(SparkplugMessageTopic topic, List<Met
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<Metric>
{
MetricStatus = metricStatus
};
this.NodeStates.AddOrUpdate(
topic.EdgeNodeIdentifier,
metricState,
(_, previousMetricState) =>
{
metricState = previousMetricState;
metricState.MetricStatus = metricStatus;
return metricState;
}
);
}

var result = new List<Metric>();
foreach (var payloadMetric in metrics)
{
if (payloadMetric is not Metric convertedMetric)
Expand All @@ -243,7 +264,9 @@ private IEnumerable<Metric> ProcessPayload(SparkplugMessageTopic topic, List<Met
metricState.Metrics[payloadMetric.Name] = convertedMetric;
}

yield return convertedMetric;
result.Add(convertedMetric);
}

return result;
}
}