Browse Source

Add subscribe options builder.

release/3.x.x
Christian Kratky 5 years ago
parent
commit
ab3df2ba65
2 changed files with 88 additions and 1 deletions
  1. +1
    -1
      Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs
  2. +87
    -0
      Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs

+ 1
- 1
Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs View File

@@ -9,6 +9,6 @@ namespace MQTTnet.Client.Subscribing

public uint? SubscriptionIdentifier { get; set; }

public List<MqttUserProperty> UserProperties { get; set; } = new List<MqttUserProperty>();
public List<MqttUserProperty> UserProperties { get; set; }
}
}

+ 87
- 0
Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs View File

@@ -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<MqttUserProperty>();
}

_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> 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<TopicFilter>();
}

_subscribeOptions.TopicFilters.Add(topicFilter);

return this;
}

public MqttClientSubscribeOptions Build()
{
return _subscribeOptions;
}
}
}

Loading…
Cancel
Save