選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

204 行
8.5 KiB

  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 (handler == null)
  17. {
  18. client.ConnectedHandler = null;
  19. return client;
  20. }
  21. client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler);
  22. return client;
  23. }
  24. public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Action<MqttClientConnectedEventArgs> handler)
  25. {
  26. if (handler == null)
  27. {
  28. client.ConnectedHandler = null;
  29. return client;
  30. }
  31. client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler);
  32. return client;
  33. }
  34. public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Func<MqttClientDisconnectedEventArgs, Task> handler)
  35. {
  36. if (handler == null)
  37. {
  38. client.DisconnectedHandler = null;
  39. return client;
  40. }
  41. client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler);
  42. return client;
  43. }
  44. public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Action<MqttClientDisconnectedEventArgs> handler)
  45. {
  46. if (handler == null)
  47. {
  48. client.DisconnectedHandler = null;
  49. return client;
  50. }
  51. client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler);
  52. return client;
  53. }
  54. public static IManagedMqttClient UseApplicationMessageReceivedHandler(this IManagedMqttClient client, Func<MqttApplicationMessageReceivedEventArgs, Task> handler)
  55. {
  56. if (handler == null)
  57. {
  58. client.ApplicationMessageReceivedHandler = null;
  59. return client;
  60. }
  61. client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(handler);
  62. return client;
  63. }
  64. public static IManagedMqttClient UseApplicationMessageReceivedHandler(this IManagedMqttClient client, Action<MqttApplicationMessageReceivedEventArgs> handler)
  65. {
  66. if (handler == null)
  67. {
  68. client.ApplicationMessageReceivedHandler = null;
  69. return client;
  70. }
  71. client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(handler);
  72. return client;
  73. }
  74. public static Task SubscribeAsync(this IManagedMqttClient managedClient, params TopicFilter[] topicFilters)
  75. {
  76. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  77. return managedClient.SubscribeAsync(topicFilters);
  78. }
  79. public static Task SubscribeAsync(this IManagedMqttClient managedClient, string topic, MqttQualityOfServiceLevel qualityOfServiceLevel)
  80. {
  81. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  82. if (topic == null) throw new ArgumentNullException(nameof(topic));
  83. return managedClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  84. }
  85. public static Task SubscribeAsync(this IManagedMqttClient managedClient, string topic)
  86. {
  87. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  88. if (topic == null) throw new ArgumentNullException(nameof(topic));
  89. return managedClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
  90. }
  91. public static Task UnsubscribeAsync(this IManagedMqttClient managedClient, params string[] topicFilters)
  92. {
  93. if (managedClient == null) throw new ArgumentNullException(nameof(managedClient));
  94. return managedClient.UnsubscribeAsync(topicFilters);
  95. }
  96. public static async Task PublishAsync(this IManagedMqttClient client, IEnumerable<MqttApplicationMessage> applicationMessages)
  97. {
  98. if (client == null) throw new ArgumentNullException(nameof(client));
  99. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  100. foreach (var applicationMessage in applicationMessages)
  101. {
  102. await client.PublishAsync(applicationMessage).ConfigureAwait(false);
  103. }
  104. }
  105. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, MqttApplicationMessage applicationMessage)
  106. {
  107. if (client == null) throw new ArgumentNullException(nameof(client));
  108. if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
  109. return client.PublishAsync(applicationMessage, CancellationToken.None);
  110. }
  111. public static async Task PublishAsync(this IManagedMqttClient client, params MqttApplicationMessage[] applicationMessages)
  112. {
  113. if (client == null) throw new ArgumentNullException(nameof(client));
  114. if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
  115. foreach (var applicationMessage in applicationMessages)
  116. {
  117. await client.PublishAsync(applicationMessage, CancellationToken.None).ConfigureAwait(false);
  118. }
  119. }
  120. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic)
  121. {
  122. if (client == null) throw new ArgumentNullException(nameof(client));
  123. if (topic == null) throw new ArgumentNullException(nameof(topic));
  124. return client.PublishAsync(builder => builder
  125. .WithTopic(topic));
  126. }
  127. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload)
  128. {
  129. if (client == null) throw new ArgumentNullException(nameof(client));
  130. if (topic == null) throw new ArgumentNullException(nameof(topic));
  131. return client.PublishAsync(builder => builder
  132. .WithTopic(topic)
  133. .WithPayload(payload));
  134. }
  135. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  136. {
  137. if (client == null) throw new ArgumentNullException(nameof(client));
  138. if (topic == null) throw new ArgumentNullException(nameof(topic));
  139. return client.PublishAsync(builder => builder
  140. .WithTopic(topic)
  141. .WithPayload(payload)
  142. .WithQualityOfServiceLevel(qualityOfServiceLevel));
  143. }
  144. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain)
  145. {
  146. if (client == null) throw new ArgumentNullException(nameof(client));
  147. if (topic == null) throw new ArgumentNullException(nameof(topic));
  148. return client.PublishAsync(builder => builder
  149. .WithTopic(topic)
  150. .WithPayload(payload)
  151. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  152. .WithRetainFlag(retain));
  153. }
  154. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder, CancellationToken cancellationToken)
  155. {
  156. var message = builder(new MqttApplicationMessageBuilder()).Build();
  157. return client.PublishAsync(message, cancellationToken);
  158. }
  159. public static Task<MqttClientPublishResult> PublishAsync(this IManagedMqttClient client, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
  160. {
  161. var message = builder(new MqttApplicationMessageBuilder()).Build();
  162. return client.PublishAsync(message, CancellationToken.None);
  163. }
  164. }
  165. }