Skip to content

Commit

Permalink
Updates to MQTT
Browse files Browse the repository at this point in the history
- Added MQTT Protocol support that includes Agent information, a connection heartbeat, and multiple Observation Intervals
- Added QoS configuration parameter for MQTT Client and Relay Agent configurations
- Started adding documentation
  • Loading branch information
PatrickRitchie committed Mar 20, 2023
1 parent 0738d59 commit 0b5239f
Show file tree
Hide file tree
Showing 31 changed files with 1,196 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ server: localhost
# The port number of the MQTT broker to publish messages to
port: 1883

# The Intervals to publish Observations at (in milliseconds)
observationIntervals:
- 0
- 100
- 1000


# - Agent Configuration -

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ server: localhost
# The port number of the MQTT broker to publish messages to
port: 1883

# The Intervals to publish Observations at (in milliseconds)
observationIntervals:
- 0
- 100
- 1000


# - Agent Configuration -

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MTConnect.Clients.Mqtt;
using MTConnect.Clients;
using MTConnect.Configurations;
using MTConnect.Observations;

Expand All @@ -15,6 +15,8 @@
var configuration = new MTConnectMqttClientConfiguration();
configuration.Server = "localhost";
configuration.Port = 1883;
//configuration.Interval = 100;
configuration.DeviceUuid = "OKUMA.Lathe.123456";

var client = new MTConnectMqttClient(configuration, topics: topics);
client.DeviceReceived += (s, o) =>
Expand All @@ -27,7 +29,7 @@
};
client.AssetReceived += (s, o) =>
{
Console.WriteLine("Asset Received");
Console.WriteLine($"Asset Received : {o.AssetId} : {o.Type}");
};

await client.StartAsync();
Expand Down
55 changes: 55 additions & 0 deletions docs/MQTT-Protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# MQTT Protocol for MTConnect

## Overview
This is a protocol for accessing MTConnect data using MQTT that mimics the functionality of the MTConnect HTTP REST protocol.
The main difference between the HTTP and MQTT protocols is that the MQTT protocol deals with the individual MTConnect entities directly (ex. Device, Observation, Asset).

#### All Devices
![All_Devices](../img/mtconnect-mqtt-protocol-all-01.png)


#### By Device
![All_Devices](../img/mtconnect-mqtt-protocol-by-device-01.png)


### Multiple Configurable Observation Intervals
This protocol enables multiple intervals that Observations can be received.
This interval essentially implements a window that a maximum of one Observation per DataItem will be published.
This can be used by client applications such as Dashboards. An interval of "0" will indicate that all observation changes are published.

#### Observation Topics
The interval (in milliseconds) is specified in the brackets suffix for an Observations topic following the pattern below:

##### Observation Interval 1000ms
```
MTConnect/Devices/ff8fc5c5-206f-4d94-96a8-a03c70b682b8/Observations[1000]/#
```

##### Observation Interval 100ms
```
MTConnect/Devices/ff8fc5c5-206f-4d94-96a8-a03c70b682b8/Observations[100]/#
```

##### Observation Interval 0 ("Realtime")
```
MTConnect/Devices/ff8fc5c5-206f-4d94-96a8-a03c70b682b8/Observations/#
```


### Agent Connection Heartbeat
A connection heartbeat that represents the connection between the MTConnect Agent and the MQTT Broker can be read from the topic below:
```
MTConnect/Agents/[AGENT_UUID]/HeartbeatTimestamp
```

The payload is a simple 64 bit Integer representing the last timestamp (in Unix milliseconds) that the Agent sent a heartbeat.

Using this timestamp in combination with the HeartbeatInterval property found in the **MTConnect/Agents/[AGENT_UUID]/Information** topic,
the connection status of the MTConnect Agent can be determined.
Typically waiting for 3 failed heartbeats allows for temporary connection interruptions and follows a similar pattern of other MTConnect related heartbeat protocols.


### Entity level Agent InstanceId support
Each MTConnect entity contains an InstanceId property which can be compared to the **InstanceId** property found in the **MTConnect/Agents/[AGENT_UUID]/Information**.
If the entity's InstanceId property differs, then it can be assumed that the Agent was restarted, reconfigured, etc. and the protocol should be restarted.
This is similar to the MTConnect HTTP protocol.
Binary file added img/mtconnect-mqtt-protocol-all-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mtconnect-mqtt-protocol-by-device-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

[assembly: AssemblyVersion("5.3.0")]
[assembly: AssemblyFileVersion("5.3.0")]
[assembly: AssemblyVersion("5.4.0")]
[assembly: AssemblyFileVersion("5.4.0")]
[assembly: AssemblyCompany("TrakHound Inc.")]
[assembly: AssemblyCopyright("Copyright (c) 2023 TrakHound Inc., All Rights Reserved.")]
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace MTConnect.Configurations
{
public interface IMqttAgentApplicationConfiguration : IAgentApplicationConfiguration, IMTConnectMqttClientConfiguration
public interface IMqttAgentApplicationConfiguration : IAgentApplicationConfiguration
{
string Server { get; set; }

int Port { get; set; }

string Username { get; set; }

string Password { get; set; }

string CertificateAuthority { get; set; }

string PemCertificate { get; set; }

string PemPrivateKey { get; set; }

bool UseTls { get; set; }

bool AllowUntrustedCertificates { get; set; }

string TopicPrefix { get; set; }

bool RetainMessages { get; set; }

MTConnectMqttFormat MqttFormat { get; set; }

IEnumerable<int> ObservationIntervals { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

namespace MTConnect.Configurations
{
public interface IMqttRelayAgentApplicationConfiguration : IMqttAgentApplicationConfiguration
{
string ClientId { get; set; }

int QoS { get; set; }

int RetryInterval { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

namespace MTConnect.Configurations
{
/// <summary>
/// Configuration for an MTConnect SHDR > MQTT Agent
/// </summary>
public interface IShdrMqttRelayAgentApplicationConfiguration : IShdrAgentApplicationConfiguration, IMqttRelayAgentApplicationConfiguration
{

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Serialization;
using YamlDotNet.Serialization;

Expand All @@ -23,9 +24,6 @@ public class MqttAgentApplicationConfiguration : AgentApplicationConfiguration,
[JsonPropertyName("password")]
public string Password { get; set; }

[JsonPropertyName("clientId")]
public string ClientId { get; set; }

[JsonPropertyName("certificateAuthority")]
public string CertificateAuthority { get; set; }

Expand All @@ -41,9 +39,6 @@ public class MqttAgentApplicationConfiguration : AgentApplicationConfiguration,
[JsonPropertyName("useTls")]
public bool UseTls { get; set; }

[JsonPropertyName("retryInterval")]
public int RetryInterval { get; set; }

[JsonPropertyName("retainMessages")]
public bool RetainMessages { get; set; }

Expand All @@ -54,14 +49,17 @@ public class MqttAgentApplicationConfiguration : AgentApplicationConfiguration,
[YamlMember(Alias = "mqttTopicPrefix")]
public string TopicPrefix { get; set; }

[JsonPropertyName("observationIntervals")]
public IEnumerable<int> ObservationIntervals { get; set; }


public MqttAgentApplicationConfiguration()
{
Server = "localhost";
Port = 1883;
RetryInterval = 5000;
RetainMessages = true;
MqttFormat = MTConnectMqttFormat.Hierarchy;
ObservationIntervals = new List<int> { 0, 1000 };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Text.Json.Serialization;

namespace MTConnect.Configurations
{
public class MqttRelayAgentApplicationConfiguration : MqttAgentApplicationConfiguration, IMqttRelayAgentApplicationConfiguration
{
[JsonPropertyName("clientId")]
public string ClientId { get; set; }

[JsonPropertyName("qos")]
public int QoS { get; set; }

[JsonPropertyName("retryInterval")]
public int RetryInterval { get; set; }


public MqttRelayAgentApplicationConfiguration()
{
Server = "localhost";
Port = 1883;
QoS = 1;
RetryInterval = 5000;
RetainMessages = true;
MqttFormat = MTConnectMqttFormat.Hierarchy;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MTConnect.Configurations
{
/// <summary>
/// Configuration for an MTConnect Mqtt Gateway Agent Application
/// </summary>
public class MqttRelayAgentGatewayApplicationConfiguration : MqttRelayAgentApplicationConfiguration
{
/// <summary>
/// List of MTConnect Http Clients to read from
/// </summary>
[JsonPropertyName("clients")]
public List<HttpClientConfiguration> Clients { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2023 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Serialization;
using YamlDotNet.Serialization;

Expand All @@ -23,9 +24,6 @@ public class ShdrMqttAgentApplicationConfiguration : ShdrAgentApplicationConfigu
[JsonPropertyName("password")]
public string Password { get; set; }

[JsonPropertyName("clientId")]
public string ClientId { get; set; }

[JsonPropertyName("certificateAuthority")]
public string CertificateAuthority { get; set; }

Expand All @@ -41,9 +39,6 @@ public class ShdrMqttAgentApplicationConfiguration : ShdrAgentApplicationConfigu
[JsonPropertyName("useTls")]
public bool UseTls { get; set; }

[JsonPropertyName("retryInterval")]
public int RetryInterval { get; set; }

[JsonPropertyName("retainMessages")]
public bool RetainMessages { get; set; }

Expand All @@ -54,14 +49,17 @@ public class ShdrMqttAgentApplicationConfiguration : ShdrAgentApplicationConfigu
[YamlMember(Alias = "mqttTopicPrefix")]
public string TopicPrefix { get; set; }

[JsonPropertyName("observationIntervals")]
public IEnumerable<int> ObservationIntervals { get; set; }


public ShdrMqttAgentApplicationConfiguration()
{
Server = "localhost";
Port = 1883;
RetryInterval = 5000;
RetainMessages = true;
MqttFormat = MTConnectMqttFormat.Hierarchy;
ObservationIntervals = new List<int> { 0, 1000 };
}
}
}
Loading

0 comments on commit 0b5239f

Please sign in to comment.