MqttClientService .cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  85. catch (ObjectDisposedException ex)
  86. {
  87. _logger.LogError($"Error publishing message: {ex.Message}");
  88. // 处理异常或重新创建客户端实例
  89. }
  90. }
  91. public async Task SubscribeAsync(string topic)
  92. {
  93. if (!_mqttClient.IsConnected)
  94. {
  95. await ConnectAsync();
  96. }
  97. var topicFilter = new MqttTopicFilterBuilder()
  98. .WithTopic(topic)
  99. .Build();
  100. try
  101. {
  102. await _mqttClient.SubscribeAsync(topicFilter);
  103. }
  104. catch (Exception ex)
  105. {
  106. _logger.LogError($"Error subscribing to topic {topic}: {ex.Message}");
  107. // 处理异常
  108. }
  109. }
  110. public async Task DisconnectAsync()
  111. {
  112. if (_mqttClient.IsConnected)
  113. {
  114. await _mqttClient.DisconnectAsync();
  115. }
  116. }
  117. protected virtual void Dispose(bool disposing)
  118. {
  119. if (!_disposed)
  120. {
  121. if (disposing)
  122. {
  123. _mqttClient?.Dispose();
  124. }
  125. _disposed = true;
  126. }
  127. }
  128. public void Dispose()
  129. {
  130. Dispose(true);
  131. GC.SuppressFinalize(this);
  132. }
  133. }
  134. }