You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicationMessagePublisherExtensions.cs 2.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }