@@ -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,7 +82,7 @@ private MqttApplicationMessage CreateMessage(IDevice device)
8182 messageBuilder . WithTopic ( topic ) ;
8283 messageBuilder . WithPayload ( formatResult . Content ) ;
8384 messageBuilder . WithRetainFlag ( true ) ;
84- messageBuilder . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
85+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
8586 return messageBuilder . Build ( ) ;
8687 }
8788 }
@@ -168,7 +169,7 @@ private MqttApplicationMessage CreateMessage(IObservation observation)
168169 messageBuilder . WithTopic ( topic ) ;
169170 messageBuilder . WithPayload ( formatResult . Content ) ;
170171 messageBuilder . WithRetainFlag ( true ) ;
171- messageBuilder . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
172+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
172173 return messageBuilder . Build ( ) ;
173174 }
174175 }
@@ -200,7 +201,7 @@ private MqttApplicationMessage CreateMessage(IEnumerable<IObservation> observati
200201 messageBuilder . WithTopic ( topic ) ;
201202 messageBuilder . WithPayload ( formatResult . Content ) ;
202203 messageBuilder . WithRetainFlag ( true ) ;
203- messageBuilder . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
204+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
204205 return messageBuilder . Build ( ) ;
205206 }
206207 }
@@ -253,12 +254,19 @@ private MqttApplicationMessage CreateMessage(IAsset asset)
253254 messageBuilder . WithTopic ( topic ) ;
254255 messageBuilder . WithPayload ( formatResult . Content ) ;
255256 messageBuilder . WithRetainFlag ( true ) ;
256- messageBuilder . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
257+ messageBuilder . WithQualityOfServiceLevel ( GetQualityOfService ( _configuration . QoS ) ) ;
257258 return messageBuilder . Build ( ) ;
258259 }
259260 }
260261
261262 return null ;
262263 }
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+ }
263271 }
264272}
0 commit comments