Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

57 righe
2.3 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Protocol;
  4. namespace MQTTnet
  5. {
  6. public static class ApplicationMessagePublisherExtensions
  7. {
  8. public static Task PublishAsync(this IApplicationMessagePublisher publisher, params MqttApplicationMessage[] applicationMessages)
  9. {
  10. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  11. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  12. return publisher.PublishAsync(applicationMessages);
  13. }
  14. public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic)
  15. {
  16. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  17. if (topic == null) throw new ArgumentNullException(nameof(topic));
  18. return publisher.PublishAsync(builder => builder
  19. .WithTopic(topic)
  20. );
  21. }
  22. public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload)
  23. {
  24. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  25. if (topic == null) throw new ArgumentNullException(nameof(topic));
  26. return publisher.PublishAsync(builder => builder
  27. .WithTopic(topic)
  28. .WithPayload(payload)
  29. );
  30. }
  31. public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  32. {
  33. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  34. if (topic == null) throw new ArgumentNullException(nameof(topic));
  35. return publisher.PublishAsync(builder => builder
  36. .WithTopic(topic)
  37. .WithPayload(payload)
  38. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  39. );
  40. }
  41. public static Task PublishAsync(this IApplicationMessagePublisher publisher, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
  42. {
  43. var message = builder(new MqttApplicationMessageBuilder()).Build();
  44. return publisher.PublishAsync(message);
  45. }
  46. }
  47. }