Skip to content

Commit

Permalink
Update v6.2.2
Browse files Browse the repository at this point in the history
- Added Category, Sequence,  and InstanceId to MQTT Relay and MQTT Broker messages using the Entity topic structure.
- Updated Condition Entity MQTT messages to be an array in order to contain multiple fault states for a single DataItem
  • Loading branch information
PatrickRitchie committed Apr 5, 2024
1 parent 806f197 commit fdc340b
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 216 deletions.
30 changes: 15 additions & 15 deletions agent/MTConnect.NET-Agent/agent.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ modules:
# topicPrefix: MTConnect/Document
# topicStructure: Document

- mqtt-broker: # - Add MQTT Broker module (Entity Structure)
port: 1883
currentInterval: 10000
sampleInterval: 500
documentFormat: JSON
topicPrefix: MTConnect/Entity
topicStructure: Entity
# - mqtt-broker: # - Add MQTT Broker module (Entity Structure)
# port: 1883
# currentInterval: 10000
# sampleInterval: 500
# documentFormat: JSON
# topicPrefix: MTConnect/Entity
# topicStructure: Entity

# - mqtt-broker: # - Add MQTT Broker module (TLS)
# port: 8883
Expand All @@ -90,14 +90,14 @@ modules:
# topicPrefix: MTConnect/Document
# topicStructure: Document

# - mqtt-relay: # - Add MQTT Relay module (Entity Structure)
# server: localhost
# port: 1883
# currentInterval: 10000
# sampleInterval: 500
# documentFormat: JSON
# topicPrefix: MTConnect/Entity
# topicStructure: Entity
- mqtt-relay: # - Add MQTT Relay module (Entity Structure)
server: localhost
port: 1883
currentInterval: 10000
sampleInterval: 500
documentFormat: JSON
topicPrefix: MTConnect/Entity
topicStructure: Entity

# - mqtt-relay: # - Add MQTT Relay module (TLS)
# server: localhost
Expand Down
64 changes: 59 additions & 5 deletions agent/Modules/MTConnect.NET-AgentModule-MqttBroker/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using MTConnect.Devices;
using MTConnect.Formatters;
using MTConnect.Logging;
using MTConnect.Observations;
using MTConnect.Observations.Output;
using MTConnect.Streams.Output;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -308,11 +310,51 @@ private async Task PublishCurrentObservations()
if (_entityServer != null)
{
var observations = Agent.GetCurrentObservations();
if (!observations.IsNullOrEmpty())
await PublishObservations(observations);
}
}

private async Task PublishObservations(IEnumerable<IObservationOutput> observations)
{
if (!observations.IsNullOrEmpty())
{
var dataItemIds = observations.Select(o => o.DataItemId).Distinct();
foreach (var dataItemId in dataItemIds)
{
foreach (var observation in observations)
var dataItemObservations = observations.Where(o => o.DataItemId == dataItemId);
var dataItemObservation = dataItemObservations.FirstOrDefault();

if (dataItemObservation.Category == DataItemCategory.CONDITION)
{
// Conditions have multiple observations
var multipleObservations = new List<IObservation>();
foreach (var observation in dataItemObservations)
{
var x = new Observation();
x.DeviceUuid = observation.DeviceUuid;
x.DataItemId = observation.DataItemId;
x.DataItem = observation.DataItem;
x.Name = observation.Name;
x.Category = observation.Category;
x.Type = observation.Type;
x.SubType = observation.SubType;
x.Representation = observation.Representation;
x.CompositionId = observation.CompositionId;
x.InstanceId = observation.InstanceId;
x.Sequence = observation.Sequence;
x.Timestamp = observation.Timestamp;
x.AddValues(observation.Values);

multipleObservations.Add(x);
}

await _entityServer.PublishObservations(_mqttServer, multipleObservations);
}
else
{
var x = new Observations.Observation();
var observation = dataItemObservations.FirstOrDefault();

var x = new Observation();
x.DeviceUuid = observation.DeviceUuid;
x.DataItemId = observation.DataItemId;
x.DataItem = observation.DataItem;
Expand All @@ -333,6 +375,7 @@ private async Task PublishCurrentObservations()
}
}


private async Task PublishAssets()
{
if (_entityServer != null)
Expand All @@ -354,9 +397,20 @@ private async void AgentDeviceAdded(object sender, IDevice device)
if (_entityServer != null) await _entityServer.PublishDevice(_mqttServer, device);
}

private async void AgentObservationAdded(object sender, Observations.IObservation observation)
private async void AgentObservationAdded(object sender, IObservation observation)
{
if (_entityServer != null) await _entityServer.PublishObservation(_mqttServer, observation);
if (_entityServer != null)
{
if (observation.Category == DataItemCategory.CONDITION)
{
var conditionObservations = Agent.GetCurrentObservations(observation.DeviceUuid, observation.DataItemId);
await PublishObservations(conditionObservations);
}
else
{
await _entityServer.PublishObservation(_mqttServer, observation);
}
}
}

private async void AgentAssetAdded(object sender, IAsset asset)
Expand Down
64 changes: 59 additions & 5 deletions agent/Modules/MTConnect.NET-AgentModule-MqttRelay/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
using MTConnect.Devices;
using MTConnect.Formatters;
using MTConnect.Logging;
using MTConnect.Observations;
using MTConnect.Observations.Events;
using MTConnect.Observations.Output;
using MTConnect.Streams.Output;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -452,11 +455,51 @@ private async Task PublishCurrentObservations()
if (_entityServer != null)
{
var observations = Agent.GetCurrentObservations();
if (!observations.IsNullOrEmpty())
await PublishObservations(observations);
}
}

private async Task PublishObservations(IEnumerable<IObservationOutput> observations)
{
if (!observations.IsNullOrEmpty())
{
var dataItemIds = observations.Select(o => o.DataItemId).Distinct();
foreach (var dataItemId in dataItemIds)
{
foreach (var observation in observations)
var dataItemObservations = observations.Where(o => o.DataItemId == dataItemId);
var dataItemObservation = dataItemObservations.FirstOrDefault();

if (dataItemObservation.Category == DataItemCategory.CONDITION)
{
var x = new Observations.Observation();
// Conditions have multiple observations
var multipleObservations = new List<IObservation>();
foreach (var observation in dataItemObservations)
{
var x = new Observation();
x.DeviceUuid = observation.DeviceUuid;
x.DataItemId = observation.DataItemId;
x.DataItem = observation.DataItem;
x.Name = observation.Name;
x.Category = observation.Category;
x.Type = observation.Type;
x.SubType = observation.SubType;
x.Representation = observation.Representation;
x.CompositionId = observation.CompositionId;
x.InstanceId = observation.InstanceId;
x.Sequence = observation.Sequence;
x.Timestamp = observation.Timestamp;
x.AddValues(observation.Values);

multipleObservations.Add(x);
}

await _entityServer.PublishObservations(_mqttClient, multipleObservations);
}
else
{
var observation = dataItemObservations.FirstOrDefault();

var x = new Observation();
x.DeviceUuid = observation.DeviceUuid;
x.DataItemId = observation.DataItemId;
x.DataItem = observation.DataItem;
Expand Down Expand Up @@ -498,9 +541,20 @@ private async void AgentDeviceAdded(object sender, IDevice device)
if (_entityServer != null) await _entityServer.PublishDevice(_mqttClient, device);
}

private async void AgentObservationAdded(object sender, Observations.IObservation observation)
private async void AgentObservationAdded(object sender, IObservation observation)
{
if (_entityServer != null) await _entityServer.PublishObservation(_mqttClient, observation);
if (_entityServer != null)
{
if (observation.Category == DataItemCategory.CONDITION)
{
var conditionObservations = Agent.GetCurrentObservations(observation.DeviceUuid, observation.DataItemId);
await PublishObservations(conditionObservations);
}
else
{
await _entityServer.PublishObservation(_mqttClient, observation);
}
}
}

private async void AgentAssetAdded(object sender, IAsset asset)
Expand Down
4 changes: 2 additions & 2 deletions build/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

[assembly: AssemblyVersion("6.2.1")]
[assembly: AssemblyFileVersion("6.2.1")]
[assembly: AssemblyVersion("6.2.2")]
[assembly: AssemblyFileVersion("6.2.2")]
[assembly: AssemblyCompany("TrakHound Inc.")]
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]
Loading

0 comments on commit fdc340b

Please sign in to comment.