You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

52 lines
1.4 KiB

  1. using MQTTnet.Exceptions;
  2. using MQTTnet.Protocol;
  3. namespace MQTTnet
  4. {
  5. public class TopicFilterBuilder
  6. {
  7. private MqttQualityOfServiceLevel _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  8. private string _topic;
  9. public TopicFilterBuilder WithTopic(string topic)
  10. {
  11. _topic = topic;
  12. return this;
  13. }
  14. public TopicFilterBuilder WithQualityOfServiceLevel(MqttQualityOfServiceLevel qualityOfServiceLevel)
  15. {
  16. _qualityOfServiceLevel = qualityOfServiceLevel;
  17. return this;
  18. }
  19. public TopicFilterBuilder WithAtLeastOnceQoS()
  20. {
  21. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce;
  22. return this;
  23. }
  24. public TopicFilterBuilder WithAtMostOnceQoS()
  25. {
  26. _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
  27. return this;
  28. }
  29. public TopicFilterBuilder WithExactlyOnceQoS()
  30. {
  31. _qualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce;
  32. return this;
  33. }
  34. public TopicFilter Build()
  35. {
  36. if (string.IsNullOrEmpty(_topic))
  37. {
  38. throw new MqttProtocolViolationException("Topic is not set.");
  39. }
  40. return new TopicFilter(_topic, _qualityOfServiceLevel);
  41. }
  42. }
  43. }