MqttClientService .cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MQTTnet;
  2. using MQTTnet.Client;
  3. using Microsoft.Extensions.Options;
  4. using Microsoft.Extensions.Logging;
  5. using System.Text;
  6. using MQTTnet.Server;
  7. namespace Fuel.Application.MqttService
  8. {
  9. public class MqttClientService : IMqttClientService, IDisposable
  10. {
  11. private IMqttClient _mqttClient;
  12. private MqttOptions _options;
  13. private ILogger<MqttClientService> _logger;
  14. private bool _disposed = false;
  15. public MqttClientService(
  16. IOptions<MqttOptions> options,
  17. ILogger<MqttClientService> logger)
  18. {
  19. _options = options.Value;
  20. _logger = logger;
  21. var factory = new MqttFactory();
  22. _mqttClient = factory.CreateMqttClient();
  23. // 配置事件处理
  24. _mqttClient.ConnectedAsync += HandleConnectedAsync;
  25. _mqttClient.DisconnectedAsync += HandleDisconnectedAsync;
  26. _mqttClient.ApplicationMessageReceivedAsync += HandleMessageReceivedAsync;
  27. }
  28. private async Task HandleConnectedAsync(MqttClientConnectedEventArgs e)
  29. {
  30. _logger.LogInformation("MQTT connected");
  31. await Task.CompletedTask;
  32. }
  33. private async Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs e)
  34. {
  35. _logger.LogWarning("MQTT disconnected. Attempting to reconnect...");
  36. await Task.Delay(TimeSpan.FromSeconds(5));
  37. if (!_disposed)
  38. {
  39. await ConnectAsync(); // 自动重连
  40. }
  41. }
  42. private async Task HandleMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e)
  43. {
  44. var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
  45. _logger.LogInformation($"Received message: {payload} [Topic: {e.ApplicationMessage.Topic}]");
  46. await Task.CompletedTask;
  47. }
  48. public async Task ConnectAsync()
  49. {
  50. if (_mqttClient.IsConnected)
  51. {
  52. return;
  53. }
  54. var options = new MqttClientOptionsBuilder()
  55. .WithTcpServer(_options.Server, _options.Port)
  56. .WithClientId(_options.ClientId)
  57. .WithCredentials("HSClient", "HS202503")
  58. .WithCleanSession()
  59. .Build();
  60. try
  61. {
  62. await _mqttClient.ConnectAsync(options);
  63. }
  64. catch (ObjectDisposedException ex)
  65. {
  66. _logger.LogError($"Error connecting to MQTT broker: {ex.Message}");
  67. // 处理异常或重新创建客户端实例
  68. }
  69. }
  70. public async Task PublishAsync(string topic, string payload)
  71. {
  72. if (!_mqttClient.IsConnected)
  73. {
  74. await ConnectAsync();
  75. }
  76. var message = new MqttApplicationMessageBuilder()
  77. .WithTopic(topic)
  78. .WithPayload(payload)
  79. .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce)
  80. .Build();
  81. try
  82. {
  83. await _mqttClient.PublishAsync(message);
  84. _logger.LogError($"PublishAsync,topic:{topic},payload:{payload},推送成功");
  85. }
  86. catch (ObjectDisposedException ex)
  87. {
  88. _logger.LogError($"Error publishing message: {ex.Message}");
  89. // 处理异常或重新创建客户端实例
  90. }
  91. }
  92. public async Task SubscribeAsync(string topic)
  93. {
  94. if (!_mqttClient.IsConnected)
  95. {
  96. await ConnectAsync();
  97. }
  98. var topicFilter = new MqttTopicFilterBuilder()
  99. .WithTopic(topic)
  100. .Build();
  101. try
  102. {
  103. await _mqttClient.SubscribeAsync(topicFilter);
  104. }
  105. catch (Exception ex)
  106. {
  107. _logger.LogError($"Error subscribing to topic {topic}: {ex.Message}");
  108. // 处理异常
  109. }
  110. }
  111. public async Task DisconnectAsync()
  112. {
  113. if (_mqttClient.IsConnected)
  114. {
  115. await _mqttClient.DisconnectAsync();
  116. }
  117. }
  118. protected virtual void Dispose(bool disposing)
  119. {
  120. if (!_disposed)
  121. {
  122. if (disposing)
  123. {
  124. _mqttClient?.Dispose();
  125. }
  126. _disposed = true;
  127. }
  128. }
  129. public void Dispose()
  130. {
  131. Dispose(true);
  132. GC.SuppressFinalize(this);
  133. }
  134. }
  135. }