Explorar el Código

Add new extension methods.

release/3.x.x
Christian hace 6 años
padre
commit
447b2aa2c6
Se han modificado 4 ficheros con 89 adiciones y 6 borrados
  1. +28
    -3
      Frameworks/MQTTnet.NetStandard/ApplicationMessagePublisherExtensions.cs
  2. +8
    -0
      Frameworks/MQTTnet.NetStandard/Client/MqttClientExtensions.cs
  3. +28
    -3
      Frameworks/MQTTnet.NetStandard/ManagedClient/ManagedMqttClientOptionsBuilder.cs
  4. +25
    -0
      Frameworks/MQTTnet.NetStandard/MqttApplicationMessageExtensions.cs

+ 28
- 3
Frameworks/MQTTnet.NetStandard/ApplicationMessagePublisherExtensions.cs Ver fichero

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

+ 8
- 0
Frameworks/MQTTnet.NetStandard/Client/MqttClientExtensions.cs Ver fichero

@@ -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));


+ 28
- 3
Frameworks/MQTTnet.NetStandard/ManagedClient/ManagedMqttClientOptionsBuilder.cs Ver fichero

@@ -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.");


+ 25
- 0
Frameworks/MQTTnet.NetStandard/MqttApplicationMessageExtensions.cs Ver fichero

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

Cargando…
Cancelar
Guardar