Skip to content

Commit

Permalink
Added support for MQTT Adapter Devices and Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 14, 2023
1 parent aeec28a commit dc675d1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
46 changes: 44 additions & 2 deletions adapter/Modules/MTConnect.NET-AdapterModule-MQTT/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ private async Task Worker()
try
{
// Declare new MQTT Client Options with Tcp Server
var clientOptionsBuilder = new MqttClientOptionsBuilder().WithTcpServer(_configuration.Server, _configuration.Port);
var clientOptionsBuilder = new MqttClientOptionsBuilder();

// Set TCP Settings
clientOptionsBuilder.WithTcpServer(_configuration.Server, _configuration.Port);

// Set Client ID
if (!string.IsNullOrEmpty(_configuration.ClientId))
Expand Down Expand Up @@ -155,9 +158,13 @@ public override bool AddObservations(IEnumerable<IObservationInput> observations
if (formatResult.Success)
{
var message = new MqttApplicationMessage();
message.Topic = $"{_configuration.Topic}/{_configuration.DeviceKey}/observations";
message.Retain = true;
message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS;
message.Topic = $"{_configuration.Topic}/observations";
message.PayloadSegment = formatResult.Content;
_mqttClient.PublishAsync(message);

Log(MTConnectLogLevel.Debug, $"MQTT Observations Message Published to {message.Topic}");
}
}

Expand All @@ -166,11 +173,46 @@ public override bool AddObservations(IEnumerable<IObservationInput> observations

public override bool AddAssets(IEnumerable<IAssetInput> assets)
{
if (_mqttClient != null && !assets.IsNullOrEmpty())
{
var formatResult = InputFormatter.Format(_configuration.DocumentFormat, assets);
if (formatResult.Success)
{
var message = new MqttApplicationMessage();
message.Retain = true;
message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS;
message.Topic = $"{_configuration.Topic}/assets";
message.PayloadSegment = formatResult.Content;
_mqttClient.PublishAsync(message);

Log(MTConnectLogLevel.Debug, $"MQTT Assets Message Published to {message.Topic}");
}
}

return true;
}

public override bool AddDevices(IEnumerable<IDeviceInput> devices)
{
if (_mqttClient != null && !devices.IsNullOrEmpty())
{
foreach (var device in devices)
{
var formatResult = InputFormatter.Format(_configuration.DocumentFormat, device);
if (formatResult.Success)
{
var message = new MqttApplicationMessage();
message.Retain = true;
message.QualityOfServiceLevel = (MQTTnet.Protocol.MqttQualityOfServiceLevel)_configuration.QoS;
message.Topic = $"{_configuration.Topic}/device";
message.PayloadSegment = formatResult.Content;
_mqttClient.PublishAsync(message);

Log(MTConnectLogLevel.Debug, $"MQTT Device Message Published to {message.Topic}");
}
}
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# MTConnect MQTT Adapter Module
This Adapter Module sends input data to an MQTT Broker that can be read by an MTConnect Agent

>This Module is still under development. Please feel free to leave feedback or create Issues on GitHub.
## Configuration
```yaml
- mqtt:
Expand Down
2 changes: 2 additions & 0 deletions adapter/Modules/MTConnect.NET-AdapterModule-MQTT/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# MTConnect MQTT Adapter Module
This Adapter Module sends input data to an MQTT Broker that can be read by an MTConnect Agent

>This Module is still under development. Please feel free to leave feedback or create Issues on GitHub.
## Nuget
<table>
<thead>
Expand Down

0 comments on commit dc675d1

Please sign in to comment.