Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ManagedMqttClientExtensions.cs 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Client.Connecting;
  6. using MQTTnet.Client.Disconnecting;
  7. using MQTTnet.Client.Publishing;
  8. using MQTTnet.Client.Receiving;
  9. using MQTTnet.Protocol;
  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 IManagedMqttClient UseApplicationMessageReceivedHandler(this IManagedMqttClient client, Func<MqttApplicationMessageReceivedEventArgs, Task> handler)
  63. {
  64. if (client == null) throw new ArgumentNullException(nameof(client));
  65. if (handler == null)
  66. {
  67. return client.UseApplicationMessageReceivedHandler((IMqttApplicationMessageReceivedHandler)null);
  68. }
  69. return client.UseApplicationMessageReceivedHandler(new MqttApplicationMessageReceivedHandlerDelegate(handler));
  70. }
  71. public static IManagedMqttClient UseApplicationMessageReceivedHandler(this IManagedMqttClient client, Action<MqttApplicationMessageReceivedEventArgs> handler)
  72. {
  73. if (client == null) throw new ArgumentNullException(nameof(client));
  74. if (handler == null)
  75. {
  76. return client.UseApplicationMessageReceivedHandler((IMqttApplicationMessageReceivedHandler)null);
  77. }
  78. return client.UseApplicationMessageReceivedHandler(new MqttApplicationMessageReceivedHandlerDelegate(handler));
  79. }
  80. public static IManagedMqttClient UseApplicationMessageReceivedHandler(this IManagedMqttClient client, IMqttApplicationMessageReceivedHandler handler)
  81. {
  82. if (client == null) throw new ArgumentNullException(nameof(client));
  83. client.ApplicationMessageReceivedHandler = handler;
  84. return client;
  85. }
  86. public static Task SubscribeAsync(this IManagedMqttClient client, params TopicFilter[] topicFilters)
  87. {
  88. if (client == null) throw new ArgumentNullException(nameof(client));
  89. return client.SubscribeAsync(topicFilters);
  90. }
  91. public static Task SubscribeAsync(this IManagedMqttClient client, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
  92. {
  93. if (client == null) throw new ArgumentNullException(nameof(client));
  94. if (topic == null) throw new ArgumentNullException(nameof(topic));
  95. return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  96. }
  97. public static Task SubscribeAsync(this IManagedMqttClient client, string topic)
  98. {
  99. if (client == null) throw new ArgumentNullException(nameof(client));
  100. if (topic == null) throw new ArgumentNullException(nameof(topic));
  101. return client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
  102. }
  103. public static Task UnsubscribeAsync(this IManagedMqttClient client, params string[] topicFilters)
  104. {
  105. if (client == null) throw new ArgumentNullException(nameof(client));
  106. return client.UnsubscribeAsync(topicFilters);
  107. }
  108. public static async Task PublishAsync(this IManagedMqttClient client, IEnumerable<MqttApplicationMessage> applicationMessages)
  109. {
  110. if (client == null) throw new ArgumentNullException(nameof(client));
  111. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  112. foreach (var applicationMessage in applicationMessages)
  113. {
  114. await client.PublishAsync(applicationMessage).ConfigureAwait(false);
  115. }
  116. }
  117. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, MqttApplicationMessage applicationMessage)
  118. {
  119. if (client == null) throw new ArgumentNullException(nameof(client));
  120. if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
  121. return client.PublishAsync(applicationMessage, CancellationToken.None);
  122. }
  123. public static async Task PublishAsync(this IManagedMqttClient client, params MqttApplicationMessage[] applicationMessages)
  124. {
  125. if (client == null) throw new ArgumentNullException(nameof(client));
  126. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  127. foreach (var applicationMessage in applicationMessages)
  128. {
  129. await client.PublishAsync(applicationMessage, CancellationToken.None).ConfigureAwait(false);
  130. }
  131. }
  132. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic)
  133. {
  134. if (client == null) throw new ArgumentNullException(nameof(client));
  135. if (topic == null) throw new ArgumentNullException(nameof(topic));
  136. return client.PublishAsync(builder => builder
  137. .WithTopic(topic));
  138. }
  139. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload)
  140. {
  141. if (client == null) throw new ArgumentNullException(nameof(client));
  142. if (topic == null) throw new ArgumentNullException(nameof(topic));
  143. return client.PublishAsync(builder => builder
  144. .WithTopic(topic)
  145. .WithPayload(payload));
  146. }
  147. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  148. {
  149. if (client == null) throw new ArgumentNullException(nameof(client));
  150. if (topic == null) throw new ArgumentNullException(nameof(topic));
  151. return client.PublishAsync(builder => builder
  152. .WithTopic(topic)
  153. .WithPayload(payload)
  154. .WithQualityOfServiceLevel(qualityOfServiceLevel));
  155. }
  156. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain)
  157. {
  158. if (client == null) throw new ArgumentNullException(nameof(client));
  159. if (topic == null) throw new ArgumentNullException(nameof(topic));
  160. return client.PublishAsync(builder => builder
  161. .WithTopic(topic)
  162. .WithPayload(payload)
  163. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  164. .WithRetainFlag(retain));
  165. }
  166. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder, CancellationToken cancellationToken)
  167. {
  168. if (client == null) throw new ArgumentNullException(nameof(client));
  169. var message = builder(new MqttApplicationMessageBuilder()).Build();
  170. return client.PublishAsync(message, cancellationToken);
  171. }
  172. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
  173. {
  174. if (client == null) throw new ArgumentNullException(nameof(client));
  175. var message = builder(new MqttApplicationMessageBuilder()).Build();
  176. return client.PublishAsync(message, CancellationToken.None);
  177. }
  178. }
  179. }