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

MTConnectMqttEntityServer: set correct qos for CreateMessage variants #77

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}