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

Agent published as Device on mqtt relay #86

Open
virizar opened this issue Nov 28, 2024 · 0 comments
Open

Agent published as Device on mqtt relay #86

virizar opened this issue Nov 28, 2024 · 0 comments

Comments

@virizar
Copy link
Contributor

virizar commented Nov 28, 2024

Hi @PatrickRitchie

This issue has been noticed on v6.5 using Ubuntu

Here is the configuration of our mqtt-relay

- mqtt-relay:
    allowUntrustedCertificates: true
    clientId: simulation
    currentInterval: 10000
    documentFormat: JSON
    port: 8883
    qos: 1
    sampleInterval: 500
    server: 192.168.161.164
    tls:
      omitCAValidation: true
      pem:
        certificateAuthority: GroupCACertificate.pem
        certificatePath:certificate.pem
        privateKeyPath: privateKey.pem
      verifyClientCertificate: false
    topicPrefix: MTConnect
    topicStructure: Entity

We are noticing on our brokers that, when the agent connects, it is adding itself as a Device on the data stream of the mqtt relay. Below is a screenshot of us monitoring one of our brokers when an agent is connected. We see that the GUID of the agent is added to the MTConnect/Devices prefix

image

We were expecting the agent to publish on MTConnect/Agents prefix since we know that this is the way the v5 agent is handling this.

Also the README's regarding this on the master branch also suggest that the agents are expected to be publishing on the MTConnect/Agents prefix

## MQTT Topics
### Agents
Each MTConnect Agent upon connection to the MQTT Broker publishes to the 2 topics listed below:
#### Information
Topic Format:
```
MTConnect/Agents/[AGENT_UUID]/Information
```
Example 1:
```
MTConnect/Agents/d7e169c5-14bb-48a3-bf9f-521152df2c84/Information
```
The Information topic contains information about the MTConnect Agent that would typically be contained in the Header node in an MTConnect XML Response using HTTP.
In addition to the standard MTConnect information, the Information payload also contains the configured "heartbeatInterval", "observationIntervals", and a list of the Device UUIDs that are published by this Agent.
Example Payload:
```json
{
"uuid": "d7e169c5-14bb-48a3-bf9f-521152df2c84",
"instanceId": 1687791189,
"sender": "DESKTOP-HV74M4N",
"version": "5.4.0.0",
"deviceModelChangeTime": "2023-06-26T14:53:11.1293232Z",
"heartbeatInterval": 1000,
"observationIntervals": [
0,
1000
],
"devices": [
"d7e169c5-14bb-48a3-bf9f-521152df2c84",
"5fd88408-7811-3c6b-5400-11f4026b6890",
"OKUMA.Lathe.123456"
]
}
```

I have been inspecting the code and I can see that the entity server CreateMessage method always refers to the topic <PREFIX>/Devices/* when creating messages for observations

private MqttApplicationMessage CreateMessage(IObservation observation)
{
if (observation != null)
{
var device = observation.DataItem?.Device;
if (device != null)
{
var topic = $"{_configuration.TopicPrefix}/Devices/{device.Uuid}/Observations/{observation.DataItemId}";
var formatOptions = new Dictionary<string, string>();
formatOptions.Add("categoryOutput", "true");
formatOptions.Add("instanceIdOutput", "true");
var formatResult = EntityFormatter.Format(_configuration.DocumentFormat, observation, formatOptions);
if (formatResult.Success)
{
if (formatResult.Content.Position > 0) formatResult.Content.Seek(0, SeekOrigin.Begin);
var messageBuilder = new MqttApplicationMessageBuilder();
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.Qos));
return messageBuilder.Build();
}
}
}
return null;
}
private MqttApplicationMessage CreateMessage(IEnumerable<IObservation> observations)
{
if (!observations.IsNullOrEmpty())
{
var observation = observations.FirstOrDefault();
var device = observation.DataItem?.Device;
if (device != null)
{
var topic = $"{_configuration.TopicPrefix}/Devices/{device.Uuid}/Observations/{observation.DataItemId}";
var formatOptions = new Dictionary<string, string>();
formatOptions.Add("categoryOutput", "true");
formatOptions.Add("instanceIdOutput", "true");
var formatResult = EntityFormatter.Format(_configuration.DocumentFormat, observations, formatOptions);
if (formatResult.Success)
{
if (formatResult.Content.Position > 0) formatResult.Content.Seek(0, SeekOrigin.Begin);
var messageBuilder = new MqttApplicationMessageBuilder();
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.Qos));
return messageBuilder.Build();
}
}
}
return null;
}

There is a CreateMessage overload that takes a Device as an argument and it would make sense that the message is created for a device

I failed to understand where the agent is being added as a device in the code, but I imagine there is a need to have CreateMessage for agents and use that for the messages that the agent is publishing? maybe is there a way that, via configuration, we can avoid including the agent as part of the data stream?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant