mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 08:33:01 +08:00
75 lines
2.3 KiB
C#
75 lines
2.3 KiB
C#
using System;
|
|
using MQTTnet.Client;
|
|
|
|
namespace MQTTnet.ManagedClient
|
|
{
|
|
public class ManagedMqttClientOptionsBuilder
|
|
{
|
|
private readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions();
|
|
private MqttClientOptionsBuilder _clientOptionsBuilder;
|
|
|
|
public ManagedMqttClientOptionsBuilder WithAutoReconnectDelay(TimeSpan value)
|
|
{
|
|
_options.AutoReconnectDelay = value;
|
|
return this;
|
|
}
|
|
|
|
public ManagedMqttClientOptionsBuilder WithStorage(IManagedMqttClientStorage value)
|
|
{
|
|
_options.Storage = value;
|
|
return this;
|
|
}
|
|
|
|
public ManagedMqttClientOptionsBuilder WithClientOptions(IMqttClientOptions value)
|
|
{
|
|
if (_clientOptionsBuilder != null)
|
|
{
|
|
throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
|
|
}
|
|
|
|
_options.ClientOptions = value ?? throw new ArgumentNullException(nameof(value));
|
|
|
|
return this;
|
|
}
|
|
|
|
public ManagedMqttClientOptionsBuilder WithClientOptions(MqttClientOptionsBuilder builder)
|
|
{
|
|
if (_options.ClientOptions != null)
|
|
{
|
|
throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
|
|
}
|
|
|
|
_clientOptionsBuilder = builder;
|
|
return this;
|
|
}
|
|
|
|
public ManagedMqttClientOptionsBuilder WithClientOptions(Action<MqttClientOptionsBuilder> options)
|
|
{
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
|
|
if (_clientOptionsBuilder == null)
|
|
{
|
|
_clientOptionsBuilder = new MqttClientOptionsBuilder();
|
|
}
|
|
|
|
options(_clientOptionsBuilder);
|
|
return this;
|
|
}
|
|
|
|
public ManagedMqttClientOptions Build()
|
|
{
|
|
if (_clientOptionsBuilder != null)
|
|
{
|
|
_options.ClientOptions = _clientOptionsBuilder.Build();
|
|
}
|
|
|
|
if (_options.ClientOptions == null)
|
|
{
|
|
throw new InvalidOperationException("The ClientOptions cannot be null.");
|
|
}
|
|
|
|
return _options;
|
|
}
|
|
}
|
|
}
|