Skip to content

Commit

Permalink
Merge pull request #77 from virizar/set_correct_qos_mqtt_agent_create…
Browse files Browse the repository at this point in the history
…_message

MTConnectMqttEntityServer: set correct qos for CreateMessage variants
  • Loading branch information
PatrickRitchie authored Oct 16, 2024
2 parents eb4952c + 0f15626 commit 6f1feaa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base

case MqttTopicStructure.Entity:

_entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat);
_entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat, _configuration.QoS);
Agent.DeviceAdded += AgentDeviceAdded;
Agent.ObservationAdded += AgentObservationAdded;
Agent.AssetAdded += AgentAssetAdded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Module(IMTConnectAgentBroker mtconnectAgent, object configuration) : base

case MqttTopicStructure.Entity:

_entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat);
_entityServer = new MTConnectMqttEntityServer(_configuration.TopicPrefix, _configuration.DocumentFormat, _configuration.QoS);
Agent.DeviceAdded += AgentDeviceAdded;
Agent.ObservationAdded += AgentObservationAdded;
Agent.AssetAdded += AgentAssetAdded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public interface IMTConnectMqttEntityServerConfiguration
///
/// </summary>
string DocumentFormat { get; }

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

namespace MTConnect.Configurations
Expand All @@ -9,11 +9,14 @@ public class MTConnectMqttEntityServerConfiguration : IMTConnectMqttEntityServer

public string DocumentFormat { get; set; }

public int QoS { get; set; }


public MTConnectMqttEntityServerConfiguration()
{
TopicPrefix = "MTConnect";
DocumentFormat = "JSON";
QoS = 0;
}
}
}
14 changes: 13 additions & 1 deletion libraries/MTConnect.NET-MQTT/MTConnectMqttEntityServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public class MTConnectMqttEntityServer
public string TopicPrefix => _configuration.TopicPrefix;


public MTConnectMqttEntityServer(string topicPrefix = null, string documentFormat = DocumentFormat.JSON)
public MTConnectMqttEntityServer(string topicPrefix = null, string documentFormat = DocumentFormat.JSON, int qos = 0)
{
var configuration = new MTConnectMqttEntityServerConfiguration();
configuration.TopicPrefix = topicPrefix;
configuration.DocumentFormat = documentFormat;
configuration.QoS = qos;
_configuration = configuration;
}

Expand Down Expand Up @@ -81,6 +82,7 @@ private MqttApplicationMessage CreateMessage(IDevice device)
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS));
return messageBuilder.Build();
}
}
Expand Down Expand Up @@ -167,6 +169,7 @@ private MqttApplicationMessage CreateMessage(IObservation observation)
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS));
return messageBuilder.Build();
}
}
Expand Down Expand Up @@ -198,6 +201,7 @@ private MqttApplicationMessage CreateMessage(IEnumerable<IObservation> observati
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS));
return messageBuilder.Build();
}
}
Expand Down Expand Up @@ -250,11 +254,19 @@ private MqttApplicationMessage CreateMessage(IAsset asset)
messageBuilder.WithTopic(topic);
messageBuilder.WithPayload(formatResult.Content);
messageBuilder.WithRetainFlag(true);
messageBuilder.WithQualityOfServiceLevel(GetQualityOfService(_configuration.QoS));
return messageBuilder.Build();
}
}

return null;
}

private static MQTTnet.Protocol.MqttQualityOfServiceLevel GetQualityOfService(int qos)
{
if (qos == 1) return MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce;
else if (qos == 2) return MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce;
else return MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce;
}
}
}

0 comments on commit 6f1feaa

Please sign in to comment.