@@ -23,11 +23,12 @@ public class MTConnectMqttEntityServer
2323 public string TopicPrefix => _configuration . TopicPrefix ;
2424
2525
26- public MTConnectMqttEntityServer ( string topicPrefix = null , string documentFormat = DocumentFormat . JSON )
26+ public MTConnectMqttEntityServer ( string topicPrefix = null , string documentFormat = DocumentFormat . JSON , int qos = 0 )
2727 {
2828 var configuration = new MTConnectMqttEntityServerConfiguration ( ) ;
2929 configuration . TopicPrefix = topicPrefix ;
3030 configuration . DocumentFormat = documentFormat ;
31+ configuration . QoS = qos ;
3132 _configuration = configuration ;
3233 }
3334
@@ -81,6 +82,7 @@ private MqttApplicationMessage CreateMessage(IDevice device)
8182 messageBuilder . WithTopic ( topic ) ;
8283 messageBuilder . WithPayload ( formatResult . Content ) ;
8384 messageBuilder . WithRetainFlag ( true ) ;
85+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
8486 return messageBuilder . Build ( ) ;
8587 }
8688 }
@@ -167,6 +169,7 @@ private MqttApplicationMessage CreateMessage(IObservation observation)
167169 messageBuilder . WithTopic ( topic ) ;
168170 messageBuilder . WithPayload ( formatResult . Content ) ;
169171 messageBuilder . WithRetainFlag ( true ) ;
172+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
170173 return messageBuilder . Build ( ) ;
171174 }
172175 }
@@ -198,6 +201,7 @@ private MqttApplicationMessage CreateMessage(IEnumerable<IObservation> observati
198201 messageBuilder . WithTopic ( topic ) ;
199202 messageBuilder . WithPayload ( formatResult . Content ) ;
200203 messageBuilder . WithRetainFlag ( true ) ;
204+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
201205 return messageBuilder . Build ( ) ;
202206 }
203207 }
@@ -250,11 +254,19 @@ private MqttApplicationMessage CreateMessage(IAsset asset)
250254 messageBuilder . WithTopic ( topic ) ;
251255 messageBuilder . WithPayload ( formatResult . Content ) ;
252256 messageBuilder . WithRetainFlag ( true ) ;
257+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
253258 return messageBuilder . Build ( ) ;
254259 }
255260 }
256261
257262 return null ;
258263 }
264+
265+ private static MQTTnet . Protocol . MqttQualityOfServiceLevel GetQualityOfService ( int qos )
266+ {
267+ if ( qos == 1 ) return MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ;
268+ else if ( qos == 2 ) return MQTTnet . Protocol . MqttQualityOfServiceLevel . ExactlyOnce ;
269+ else return MQTTnet . Protocol . MqttQualityOfServiceLevel . AtMostOnce ;
270+ }
259271 }
260272}
0 commit comments