您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MqttServerStorage.cs 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.Extensions.Logging;
  8. using MQTTnet.Server.Configuration;
  9. using Newtonsoft.Json;
  10. namespace MQTTnet.Server.Mqtt
  11. {
  12. public class MqttServerStorage : IMqttServerStorage
  13. {
  14. private readonly List<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
  15. private readonly MqttSettingsModel _mqttSettings;
  16. private readonly ILogger<MqttServerStorage> _logger;
  17. private string _path;
  18. private bool _messagesHaveChanged;
  19. public MqttServerStorage(MqttSettingsModel mqttSettings, ILogger<MqttServerStorage> logger)
  20. {
  21. _mqttSettings = mqttSettings ?? throw new ArgumentNullException(nameof(mqttSettings));
  22. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  23. }
  24. public void Configure()
  25. {
  26. if (_mqttSettings.RetainedApplicationMessages?.Persist != true ||
  27. string.IsNullOrEmpty(_mqttSettings.RetainedApplicationMessages.Path))
  28. {
  29. _logger.LogInformation("Persisting of retained application messages is disabled.");
  30. return;
  31. }
  32. _path = PathHelper.ExpandPath(_mqttSettings.RetainedApplicationMessages.Path);
  33. // The retained application messages are stored in a separate thread.
  34. // This is mandatory because writing them to a slow storage (like RaspberryPi SD card)
  35. // will slow down the whole message processing speed.
  36. Task.Run(SaveRetainedMessagesInternalAsync, CancellationToken.None);
  37. }
  38. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  39. {
  40. lock (_messages)
  41. {
  42. _messages.Clear();
  43. _messages.AddRange(messages);
  44. _messagesHaveChanged = true;
  45. }
  46. return Task.CompletedTask;
  47. }
  48. private async Task SaveRetainedMessagesInternalAsync()
  49. {
  50. while (true)
  51. {
  52. try
  53. {
  54. await Task.Delay(TimeSpan.FromSeconds(_mqttSettings.RetainedApplicationMessages.WriteInterval)).ConfigureAwait(false);
  55. List<MqttApplicationMessage> messages;
  56. lock (_messages)
  57. {
  58. if (!_messagesHaveChanged)
  59. {
  60. continue;
  61. }
  62. messages = new List<MqttApplicationMessage>(_messages);
  63. _messagesHaveChanged = false;
  64. }
  65. var json = JsonConvert.SerializeObject(messages);
  66. await File.WriteAllTextAsync(_path, json, Encoding.UTF8).ConfigureAwait(false);
  67. _logger.LogInformation($"{messages.Count} retained MQTT messages written.");
  68. }
  69. catch (Exception exception)
  70. {
  71. _logger.LogError(exception, "Error while writing retained MQTT messages.");
  72. }
  73. }
  74. }
  75. public async Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  76. {
  77. if (_mqttSettings.RetainedApplicationMessages?.Persist != true)
  78. {
  79. return null;
  80. }
  81. if (!File.Exists(_path))
  82. {
  83. return null;
  84. }
  85. try
  86. {
  87. var json = await File.ReadAllTextAsync(_path).ConfigureAwait(false);
  88. var applicationMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  89. _logger.LogInformation($"{applicationMessages.Count} retained MQTT messages loaded.");
  90. return applicationMessages;
  91. }
  92. catch (Exception exception)
  93. {
  94. _logger.LogWarning(exception, "Error while loading persisted retained application messages.");
  95. return null;
  96. }
  97. }
  98. }
  99. }