25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

57 lines
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. }