Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

233 linhas
10 KiB

  1. using MQTTnet.Client.Connecting;
  2. using MQTTnet.Client.Disconnecting;
  3. using MQTTnet.Client.Publishing;
  4. using MQTTnet.Client.Receiving;
  5. using MQTTnet.Protocol;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.Extensions.ManagedClient
  11. {
  12. public static class ManagedMqttClientExtensions
  13. {
  14. public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Func<MqttClientConnectedEventArgs, Task> handler)
  15. {
  16. if (client == null) throw new ArgumentNullException(nameof(client));
  17. if (handler == null)
  18. {
  19. return client.UseConnectedHandler((IMqttClientConnectedHandler)null);
  20. }
  21. return client.UseConnectedHandler(new MqttClientConnectedHandlerDelegate(handler));
  22. }
  23. public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Action<MqttClientConnectedEventArgs> handler)
  24. {
  25. if (client == null) throw new ArgumentNullException(nameof(client));
  26. if (handler == null)
  27. {
  28. return client.UseConnectedHandler((IMqttClientConnectedHandler)null);
  29. }
  30. return client.UseConnectedHandler(new MqttClientConnectedHandlerDelegate(handler));
  31. }
  32. public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, IMqttClientConnectedHandler handler)
  33. {
  34. if (client == null) throw new ArgumentNullException(nameof(client));
  35. client.ConnectedHandler = handler;
  36. return client;
  37. }
  38. public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Func<MqttClientDisconnectedEventArgs, Task> handler)
  39. {
  40. if (client == null) throw new ArgumentNullException(nameof(client));
  41. if (handler == null)
  42. {
  43. return client.UseDisconnectedHandler((IMqttClientDisconnectedHandler)null);
  44. }
  45. return client.UseDisconnectedHandler(new MqttClientDisconnectedHandlerDelegate(handler));
  46. }
  47. public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Action<MqttClientDisconnectedEventArgs> handler)
  48. {
  49. if (client == null) throw new ArgumentNullException(nameof(client));
  50. if (handler == null)
  51. {
  52. return client.UseDisconnectedHandler((IMqttClientDisconnectedHandler)null);
  53. }
  54. return client.UseDisconnectedHandler(new MqttClientDisconnectedHandlerDelegate(handler));
  55. }
  56. public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, IMqttClientDisconnectedHandler handler)
  57. {
  58. if (client == null) throw new ArgumentNullException(nameof(client));
  59. client.DisconnectedHandler = handler;
  60. return client;
  61. }
  62. public static TReceiver UseApplicationMessageReceivedHandler<TReceiver>(this TReceiver receiver, Func<MqttApplicationMessageReceivedEventArgs, Task> handler)
  63. where TReceiver : IApplicationMessageReceiver
  64. {
  65. if (receiver == null) throw new ArgumentNullException(nameof(receiver));
  66. if (handler == null)
  67. {
  68. return receiver.UseApplicationMessageReceivedHandler((IMqttApplicationMessageReceivedHandler)null);
  69. }
  70. return receiver.UseApplicationMessageReceivedHandler(new MqttApplicationMessageReceivedHandlerDelegate(handler));
  71. }
  72. public static TReceiver UseApplicationMessageReceivedHandler<TReceiver>(this TReceiver receiver, Action<MqttApplicationMessageReceivedEventArgs> handler)
  73. where TReceiver : IApplicationMessageReceiver
  74. {
  75. if (receiver == null) throw new ArgumentNullException(nameof(receiver));
  76. if (handler == null)
  77. {
  78. return receiver.UseApplicationMessageReceivedHandler((IMqttApplicationMessageReceivedHandler)null);
  79. }
  80. return receiver.UseApplicationMessageReceivedHandler(new MqttApplicationMessageReceivedHandlerDelegate(handler));
  81. }
  82. public static TReceiver UseApplicationMessageReceivedHandler<TReceiver>(this TReceiver receiver, IMqttApplicationMessageReceivedHandler handler)
  83. where TReceiver : IApplicationMessageReceiver
  84. {
  85. if (receiver == null) throw new ArgumentNullException(nameof(receiver));
  86. receiver.ApplicationMessageReceivedHandler = handler;
  87. return receiver;
  88. }
  89. public static Task SubscribeAsync(this IManagedMqttClient client, params MqttTopicFilter[] topicFilters)
  90. {
  91. if (client == null) throw new ArgumentNullException(nameof(client));
  92. return client.SubscribeAsync(topicFilters);
  93. }
  94. public static Task SubscribeAsync(this IManagedMqttClient client, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
  95. {
  96. if (client == null) throw new ArgumentNullException(nameof(client));
  97. if (topic == null) throw new ArgumentNullException(nameof(topic));
  98. return client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  99. }
  100. public static Task SubscribeAsync(this IManagedMqttClient client, string topic)
  101. {
  102. if (client == null) throw new ArgumentNullException(nameof(client));
  103. if (topic == null) throw new ArgumentNullException(nameof(topic));
  104. return client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topic).Build());
  105. }
  106. public static Task UnsubscribeAsync(this IManagedMqttClient client, params string[] topicFilters)
  107. {
  108. if (client == null) throw new ArgumentNullException(nameof(client));
  109. return client.UnsubscribeAsync(topicFilters);
  110. }
  111. public static async Task PublishAsync(this IApplicationMessagePublisher publisher, IEnumerable<MqttApplicationMessage> applicationMessages)
  112. {
  113. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  114. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  115. foreach (var applicationMessage in applicationMessages)
  116. {
  117. await publisher.PublishAsync(applicationMessage).ConfigureAwait(false);
  118. }
  119. }
  120. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, MqttApplicationMessage applicationMessage)
  121. {
  122. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  123. if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
  124. return publisher.PublishAsync(applicationMessage, CancellationToken.None);
  125. }
  126. public static async Task PublishAsync(this IApplicationMessagePublisher publisher, params MqttApplicationMessage[] applicationMessages)
  127. {
  128. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  129. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  130. foreach (var applicationMessage in applicationMessages)
  131. {
  132. await publisher.PublishAsync(applicationMessage, CancellationToken.None).ConfigureAwait(false);
  133. }
  134. }
  135. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic)
  136. {
  137. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  138. if (topic == null) throw new ArgumentNullException(nameof(topic));
  139. return publisher.PublishAsync(builder => builder
  140. .WithTopic(topic));
  141. }
  142. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload)
  143. {
  144. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  145. if (topic == null) throw new ArgumentNullException(nameof(topic));
  146. return publisher.PublishAsync(builder => builder
  147. .WithTopic(topic)
  148. .WithPayload(payload));
  149. }
  150. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  151. {
  152. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  153. if (topic == null) throw new ArgumentNullException(nameof(topic));
  154. return publisher.PublishAsync(builder => builder
  155. .WithTopic(topic)
  156. .WithPayload(payload)
  157. .WithQualityOfServiceLevel(qualityOfServiceLevel));
  158. }
  159. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain)
  160. {
  161. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  162. if (topic == null) throw new ArgumentNullException(nameof(topic));
  163. return publisher.PublishAsync(builder => builder
  164. .WithTopic(topic)
  165. .WithPayload(payload)
  166. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  167. .WithRetainFlag(retain));
  168. }
  169. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder, CancellationToken cancellationToken)
  170. {
  171. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  172. var message = builder(new MqttApplicationMessageBuilder()).Build();
  173. return publisher.PublishAsync(message, cancellationToken);
  174. }
  175. public static Task<MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
  176. {
  177. if (publisher == null) throw new ArgumentNullException(nameof(publisher));
  178. var message = builder(new MqttApplicationMessageBuilder()).Build();
  179. return publisher.PublishAsync(message, CancellationToken.None);
  180. }
  181. }
  182. }