diff --git a/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs b/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs index 49cf551..dc4be2f 100644 --- a/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs +++ b/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs @@ -9,6 +9,6 @@ namespace MQTTnet.Client.Subscribing public uint? SubscriptionIdentifier { get; set; } - public List UserProperties { get; set; } = new List(); + public List UserProperties { get; set; } } } diff --git a/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs b/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs new file mode 100644 index 0000000..c33f931 --- /dev/null +++ b/Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using MQTTnet.Packets; +using MQTTnet.Protocol; + +namespace MQTTnet.Client.Subscribing +{ + public class MqttClientSubscribeOptionsBuilder + { + private readonly MqttClientSubscribeOptions _subscribeOptions = new MqttClientSubscribeOptions(); + + public MqttClientSubscribeOptionsBuilder WithUserProperty(string name, string value) + { + if (name == null) throw new ArgumentNullException(nameof(name)); + if (value == null) throw new ArgumentNullException(nameof(value)); + + if (_subscribeOptions.UserProperties == null) + { + _subscribeOptions.UserProperties = new List(); + } + + _subscribeOptions.UserProperties.Add(new MqttUserProperty(name, value)); + + return this; + } + + public MqttClientSubscribeOptionsBuilder WithSubscriptionIdentifier(uint? subscriptionIdentifier) + { + _subscribeOptions.SubscriptionIdentifier = subscriptionIdentifier; + + return this; + } + + public MqttClientSubscribeOptionsBuilder WithTopicFilter( + string topic, + MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, + bool? noLocal = null, + bool? retainAsPublished = null, + MqttRetainHandling? retainHandling = null) + { + return WithTopicFilter(new TopicFilter + { + Topic = topic, + QualityOfServiceLevel = qualityOfServiceLevel, + NoLocal = noLocal, + RetainAsPublished = retainAsPublished, + RetainHandling = retainHandling + }); + } + + public MqttClientSubscribeOptionsBuilder WithTopicFilter(Action topicFilterBuilder) + { + if (topicFilterBuilder == null) throw new ArgumentNullException(nameof(topicFilterBuilder)); + + var internalTopicFilterBuilder = new TopicFilterBuilder(); + topicFilterBuilder(internalTopicFilterBuilder); + + return WithTopicFilter(internalTopicFilterBuilder); + } + + public MqttClientSubscribeOptionsBuilder WithTopicFilter(TopicFilterBuilder topicFilterBuilder) + { + if (topicFilterBuilder == null) throw new ArgumentNullException(nameof(topicFilterBuilder)); + + return WithTopicFilter(topicFilterBuilder.Build()); + } + + public MqttClientSubscribeOptionsBuilder WithTopicFilter(TopicFilter topicFilter) + { + if (topicFilter == null) throw new ArgumentNullException(nameof(topicFilter)); + + if (_subscribeOptions.TopicFilters == null) + { + _subscribeOptions.TopicFilters = new List(); + } + + _subscribeOptions.TopicFilters.Add(topicFilter); + + return this; + } + + public MqttClientSubscribeOptions Build() + { + return _subscribeOptions; + } + } +}