@@ -1,16 +1,41 @@ | |||
using System; | |||
using System.Threading.Tasks; | |||
using MQTTnet.Protocol; | |||
namespace MQTTnet | |||
{ | |||
public static class ApplicationMessagePublisherExtensions | |||
{ | |||
public static Task PublishAsync(this IApplicationMessagePublisher client, params MqttApplicationMessage[] applicationMessages) | |||
public static Task PublishAsync(this IApplicationMessagePublisher publisher, params MqttApplicationMessage[] applicationMessages) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
if (publisher == null) throw new ArgumentNullException(nameof(publisher)); | |||
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages)); | |||
return client.PublishAsync(applicationMessages); | |||
return publisher.PublishAsync(applicationMessages); | |||
} | |||
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic) | |||
{ | |||
if (publisher == null) throw new ArgumentNullException(nameof(publisher)); | |||
if (topic == null) throw new ArgumentNullException(nameof(topic)); | |||
return publisher.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).Build()); | |||
} | |||
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload) | |||
{ | |||
if (publisher == null) throw new ArgumentNullException(nameof(publisher)); | |||
if (topic == null) throw new ArgumentNullException(nameof(topic)); | |||
return publisher.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(payload).Build()); | |||
} | |||
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel) | |||
{ | |||
if (publisher == null) throw new ArgumentNullException(nameof(publisher)); | |||
if (topic == null) throw new ArgumentNullException(nameof(topic)); | |||
return publisher.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(payload).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()); | |||
} | |||
} | |||
} |
@@ -24,6 +24,14 @@ namespace MQTTnet.Client | |||
return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()); | |||
} | |||
public static Task<IList<MqttSubscribeResult>> SubscribeAsync(this IMqttClient client, string topic) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
if (topic == null) throw new ArgumentNullException(nameof(topic)); | |||
return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build()); | |||
} | |||
public static Task UnsubscribeAsync(this IMqttClient client, params string[] topicFilters) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
@@ -6,6 +6,7 @@ namespace MQTTnet.ManagedClient | |||
public class ManagedMqttClientOptionsBuilder | |||
{ | |||
private readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions(); | |||
private MqttClientOptionsBuilder _clientOptionsBuilder; | |||
public ManagedMqttClientOptionsBuilder WithAutoReconnectDelay(TimeSpan value) | |||
{ | |||
@@ -21,7 +22,24 @@ namespace MQTTnet.ManagedClient | |||
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; | |||
} | |||
@@ -29,15 +47,22 @@ namespace MQTTnet.ManagedClient | |||
{ | |||
if (options == null) throw new ArgumentNullException(nameof(options)); | |||
var builder = new MqttClientOptionsBuilder(); | |||
options(builder); | |||
_options.ClientOptions = builder.Build(); | |||
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."); | |||
@@ -0,0 +1,25 @@ | |||
using System; | |||
using System.Text; | |||
namespace MQTTnet | |||
{ | |||
public static class MqttApplicationMessageExtensions | |||
{ | |||
public static string ConvertPayloadToString(this MqttApplicationMessage applicationMessage) | |||
{ | |||
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage)); | |||
if (applicationMessage.Payload == null) | |||
{ | |||
return null; | |||
} | |||
if (applicationMessage.Payload.Length == 0) | |||
{ | |||
return string.Empty; | |||
} | |||
return Encoding.UTF8.GetString(applicationMessage.Payload, 0, applicationMessage.Payload.Length); | |||
} | |||
} | |||
} |