MqttClientUnsubscribeOptionsBuilder.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using MQTTnet.Packets;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace MQTTnet.Client.Unsubscribing
  5. {
  6. public class MqttClientUnsubscribeOptionsBuilder
  7. {
  8. private readonly MqttClientUnsubscribeOptions _unsubscribeOptions = new MqttClientUnsubscribeOptions();
  9. public MqttClientUnsubscribeOptionsBuilder WithUserProperty(string name, string value)
  10. {
  11. if (name is null) throw new ArgumentNullException(nameof(name));
  12. if (value is null) throw new ArgumentNullException(nameof(value));
  13. return WithUserProperty(new MqttUserProperty(name, value));
  14. }
  15. public MqttClientUnsubscribeOptionsBuilder WithUserProperty(MqttUserProperty userProperty)
  16. {
  17. if (userProperty is null) throw new ArgumentNullException(nameof(userProperty));
  18. if (_unsubscribeOptions.UserProperties is null)
  19. {
  20. _unsubscribeOptions.UserProperties = new List<MqttUserProperty>();
  21. }
  22. _unsubscribeOptions.UserProperties.Add(userProperty);
  23. return this;
  24. }
  25. public MqttClientUnsubscribeOptionsBuilder WithTopicFilter(string topic)
  26. {
  27. if (topic is null) throw new ArgumentNullException(nameof(topic));
  28. if (_unsubscribeOptions.TopicFilters is null)
  29. {
  30. _unsubscribeOptions.TopicFilters = new List<string>();
  31. }
  32. _unsubscribeOptions.TopicFilters.Add(topic);
  33. return this;
  34. }
  35. public MqttClientUnsubscribeOptionsBuilder WithTopicFilter(MqttTopicFilter topicFilter)
  36. {
  37. if (topicFilter is null) throw new ArgumentNullException(nameof(topicFilter));
  38. return WithTopicFilter(topicFilter.Topic);
  39. }
  40. public MqttClientUnsubscribeOptions Build()
  41. {
  42. return _unsubscribeOptions;
  43. }
  44. }
  45. }