修改了开启Mqtt和关闭Mqtt时的事件
This commit is contained in:
@@ -8,6 +8,8 @@ namespace PMSWPF.Models;
|
||||
/// </summary>
|
||||
public partial class Mqtt : ObservableObject
|
||||
{
|
||||
public event Action<Mqtt> OnMqttIsActiveChanged;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT客户端ID。
|
||||
/// </summary>
|
||||
@@ -34,6 +36,17 @@ public partial class Mqtt : ObservableObject
|
||||
[ObservableProperty]
|
||||
private bool _isActive;
|
||||
|
||||
partial void OnIsActiveChanged(bool value)
|
||||
{
|
||||
OnMqttIsActiveChanged?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示连接的消息:
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private string connectMessage;
|
||||
|
||||
/// <summary>
|
||||
/// 是否设置为默认MQTT客户端。
|
||||
/// </summary>
|
||||
|
||||
@@ -6,6 +6,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
@@ -28,6 +30,9 @@ namespace PMSWPF.Services
|
||||
// 存储MQTT配置的字典,键为MQTT配置ID,值为Mqtt模型对象。
|
||||
private readonly Dictionary<int, Mqtt> _mqttConfigDic;
|
||||
|
||||
// 存储每个客户端重连尝试次数的字典
|
||||
private readonly Dictionary<int, int> _reconnectAttempts;
|
||||
|
||||
|
||||
// 定时器,用于周期性地执行数据发布任务。
|
||||
private Timer _timer;
|
||||
@@ -45,6 +50,7 @@ namespace PMSWPF.Services
|
||||
_dataServices = dataServices;
|
||||
_mqttClients = new Dictionary<int, IMqttClient>();
|
||||
_mqttConfigDic = new Dictionary<int, Mqtt>();
|
||||
_reconnectAttempts = new Dictionary<int, int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +67,6 @@ namespace PMSWPF.Services
|
||||
_serviceMainThread.IsBackground = true;
|
||||
_serviceMainThread.Name = "MqttServiceMainThread";
|
||||
_serviceMainThread.Start();
|
||||
|
||||
}
|
||||
|
||||
private void Execute()
|
||||
@@ -75,9 +80,10 @@ namespace PMSWPF.Services
|
||||
}
|
||||
|
||||
_reloadEvent.WaitOne();
|
||||
|
||||
// 初始加载MQTT配置和变量数据。
|
||||
LoadMqttConfigurations();
|
||||
ConnectMqttClient();
|
||||
ConnectMqttList();
|
||||
|
||||
_reloadEvent.Reset();
|
||||
}
|
||||
@@ -95,6 +101,17 @@ namespace PMSWPF.Services
|
||||
// 取消订阅事件。
|
||||
_dataServices.OnMqttListChanged -= HandleMqttListChanged;
|
||||
|
||||
DisconnecetAll();
|
||||
|
||||
// 清空所有字典。
|
||||
_mqttClients.Clear();
|
||||
_mqttConfigDic.Clear();
|
||||
_reconnectAttempts.Clear();
|
||||
NlogHelper.Info("Mqtt后台服务已停止。"); // 记录服务停止信息
|
||||
}
|
||||
|
||||
private void DisconnecetAll()
|
||||
{
|
||||
// 断开所有已连接的MQTT客户端。
|
||||
foreach (var mqttId in _mqttClients.Keys.ToList())
|
||||
{
|
||||
@@ -103,14 +120,11 @@ namespace PMSWPF.Services
|
||||
mqtt.IsConnected = false;
|
||||
if (client.IsConnected)
|
||||
{
|
||||
client.DisconnectAsync().GetAwaiter().GetResult();
|
||||
client.DisconnectAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
// 清空所有字典。
|
||||
_mqttClients.Clear();
|
||||
_mqttConfigDic.Clear();
|
||||
NlogHelper.Info("Mqtt后台服务已停止。"); // 记录服务停止信息
|
||||
}
|
||||
|
||||
|
||||
@@ -121,28 +135,88 @@ namespace PMSWPF.Services
|
||||
private void LoadMqttConfigurations()
|
||||
{
|
||||
NlogHelper.Info("开始加载Mqtt配置文件...");
|
||||
_mqttConfigDic.Clear();
|
||||
// 从数据服务获取所有MQTT配置。
|
||||
var _mqttConfigList = _dataServices.Mqtts.Where(m => m.IsActive)
|
||||
.ToList();
|
||||
foreach (var mqtt in _mqttConfigList)
|
||||
{
|
||||
mqtt.OnMqttIsActiveChanged += OnMqttIsActiveChangedHandler;
|
||||
_mqttConfigDic[mqtt.Id] = mqtt;
|
||||
mqtt.ConnectMessage = "配置加载成功.";
|
||||
}
|
||||
|
||||
NlogHelper.Info($"Mqtt配置文件加载成功,开启的Mqtt客户端:{_mqttConfigList.Count}个。");
|
||||
}
|
||||
|
||||
private async void OnMqttIsActiveChangedHandler(Mqtt mqtt)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (mqtt.IsActive)
|
||||
{
|
||||
await ConnectMqtt(mqtt);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_mqttClients.TryGetValue(mqtt.Id,out var client))
|
||||
{
|
||||
NlogHelper.Warn($"没有在Mqtt连接字典中找到名字为:{mqtt.Name}的连接。");
|
||||
return;
|
||||
}
|
||||
|
||||
if (client.IsConnected)
|
||||
{
|
||||
await client.DisconnectAsync();
|
||||
}
|
||||
|
||||
mqtt.IsConnected = false;
|
||||
mqtt.ConnectMessage = "断开连接.";
|
||||
|
||||
_mqttClients.Remove(mqtt.Id);
|
||||
NlogHelper.Info($"{mqtt.Name}的客户端,与服务器断开连接.");
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
NotificationHelper.ShowError($"{mqtt.Name}客户端,开启或关闭的过程中发生了错误:{e.Message}",e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接到指定的MQTT代理。
|
||||
/// </summary>
|
||||
/// <param name="mqtt">MQTT配置对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
private void ConnectMqttClient()
|
||||
private async Task ConnectMqttList()
|
||||
{
|
||||
foreach (Mqtt mqtt in _mqttConfigDic.Values.ToList())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_mqttClients.TryGetValue(mqtt.Id, out var mclient) && mclient.IsConnected)
|
||||
{
|
||||
NlogHelper.Info($"{mqtt.Name}的Mqtt服务器连接已存在。");
|
||||
mqtt.ConnectMessage = "连接成功.";
|
||||
mqtt.IsConnected = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
await ConnectMqtt(mqtt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"连接MQTT服务器失败: {mqtt.Name} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ConnectMqtt(Mqtt mqtt)
|
||||
{
|
||||
NlogHelper.Info($"开始连接:{mqtt.Name}的服务器...");
|
||||
mqtt.ConnectMessage = "开始连接服务器...";
|
||||
// 创建MQTT客户端工厂和客户端实例。
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
@@ -155,62 +229,92 @@ namespace PMSWPF.Services
|
||||
.Build();
|
||||
|
||||
// 设置连接成功事件处理程序。
|
||||
client.UseConnectedHandler(async e =>
|
||||
{
|
||||
NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}");
|
||||
mqtt.IsConnected = true;
|
||||
|
||||
// 订阅主题
|
||||
await client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(mqtt.SubTopic).Build());
|
||||
NlogHelper.Info($"MQTT客户端 {mqtt.Name} 已订阅主题: {mqtt.SubTopic}");
|
||||
});
|
||||
client.UseConnectedHandler(async (e) => { await HandleConnected(e, client, mqtt); });
|
||||
|
||||
// 设置接收消息处理程序
|
||||
client.UseApplicationMessageReceivedHandler(e =>
|
||||
client.UseApplicationMessageReceivedHandler(e => { HandleMessageReceived(e, mqtt); });
|
||||
|
||||
// 设置断开连接事件处理程序。
|
||||
client.UseDisconnectedHandler(async (e) => await HandleDisconnected(e,options, client, mqtt));
|
||||
|
||||
// 尝试连接到MQTT代理。
|
||||
await client.ConnectAsync(options, CancellationToken.None);
|
||||
|
||||
// 将连接成功的客户端添加到字典。
|
||||
_mqttClients[mqtt.Id] = client;
|
||||
}
|
||||
|
||||
private static void HandleMessageReceived(MqttApplicationMessageReceivedEventArgs e, Mqtt mqtt)
|
||||
{
|
||||
var topic = e.ApplicationMessage.Topic;
|
||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
NlogHelper.Info($"MQTT客户端 {mqtt.Name} 收到消息: 主题={topic}, 消息={payload}");
|
||||
|
||||
// 在这里添加处理消息的逻辑
|
||||
});
|
||||
}
|
||||
|
||||
// 设置断开连接事件处理程序。
|
||||
client.UseDisconnectedHandler(async e =>
|
||||
private async Task HandleDisconnected(MqttClientDisconnectedEventArgs args, IMqttClientOptions options,
|
||||
IMqttClient client,
|
||||
Mqtt mqtt)
|
||||
{
|
||||
NotificationHelper.ShowWarn($"与MQTT服务器断开连接: {mqtt.Name}");
|
||||
mqtt.ConnectMessage = "断开连接.";
|
||||
mqtt.IsConnected = false;
|
||||
// 服务停止
|
||||
if (_stopEvent.WaitOne(0))
|
||||
return;
|
||||
if (!mqtt.IsActive)
|
||||
if (_stopEvent.WaitOne(0) || !mqtt.IsActive)
|
||||
return;
|
||||
|
||||
NlogHelper.Info($"5秒后重新连接Mqtt服务器:{mqtt.Name}");
|
||||
// 尝试重新连接。
|
||||
await Task.Delay(TimeSpan.FromSeconds(5)); // 等待5秒后重连
|
||||
// 增加重连尝试次数
|
||||
if (!_reconnectAttempts.ContainsKey(mqtt.Id))
|
||||
{
|
||||
_reconnectAttempts[mqtt.Id] = 0;
|
||||
}
|
||||
|
||||
_reconnectAttempts[mqtt.Id]++;
|
||||
|
||||
// 指数退避策略
|
||||
var maxDelay = TimeSpan.FromMinutes(5); // 最大延迟5分钟
|
||||
var baseDelay = TimeSpan.FromSeconds(5); // 基础延迟5秒
|
||||
var delay = TimeSpan.FromSeconds(baseDelay.TotalSeconds *
|
||||
Math.Pow(2, _reconnectAttempts[mqtt.Id] - 1));
|
||||
if (delay > maxDelay)
|
||||
{
|
||||
delay = maxDelay;
|
||||
}
|
||||
|
||||
NlogHelper.Info(
|
||||
$"与MQTT服务器:{mqtt.Name} 的连接已断开。将在 {delay.TotalSeconds} 秒后尝试第 {_reconnectAttempts[mqtt.Id]} 次重新连接...");
|
||||
|
||||
mqtt.ConnectMessage = $"连接已断开。将在 {delay.TotalSeconds} 秒后尝试第 {_reconnectAttempts[mqtt.Id]} 次重新连接...";
|
||||
await Task.Delay(delay);
|
||||
try
|
||||
{
|
||||
mqtt.ConnectMessage = $"开始重新连接服务器...";
|
||||
await client.ConnectAsync(options, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
mqtt.ConnectMessage = $"重新与Mqtt服务器连接失败.";
|
||||
NlogHelper.Error($"重新与Mqtt服务器连接失败: {mqtt.Name}", ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 尝试连接到MQTT代理。
|
||||
client.ConnectAsync(options, CancellationToken.None)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
// 将连接成功的客户端添加到字典。
|
||||
_mqttClients[mqtt.Id] = client;
|
||||
}
|
||||
catch (Exception ex)
|
||||
private async Task HandleConnected(MqttClientConnectedEventArgs args, IMqttClient client, Mqtt mqtt)
|
||||
{
|
||||
NotificationHelper.ShowError($"连接MQTT服务器失败: {mqtt.Name} - {ex.Message}", ex);
|
||||
}
|
||||
// 重置重连尝试次数
|
||||
if (_reconnectAttempts.ContainsKey(mqtt.Id))
|
||||
{
|
||||
_reconnectAttempts[mqtt.Id] = 0;
|
||||
}
|
||||
|
||||
NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}");
|
||||
mqtt.IsConnected = true;
|
||||
mqtt.ConnectMessage = "连接成功.";
|
||||
|
||||
// 订阅主题
|
||||
await client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(mqtt.SubTopic)
|
||||
.Build());
|
||||
NlogHelper.Info($"MQTT客户端 {mqtt.Name} 已订阅主题: {mqtt.SubTopic}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,33 +19,33 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
private readonly MqttRepository _mqttRepository;
|
||||
private readonly ILogger<MqttsViewModel> _logger;
|
||||
private readonly NavgatorServices _navgatorServices;
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Mqtt> _mqtts;
|
||||
|
||||
public ObservableCollection<Mqtt> Mqtts
|
||||
{
|
||||
get => _mqtts;
|
||||
set
|
||||
{
|
||||
if (_mqtts != null)
|
||||
{
|
||||
foreach (var mqtt in _mqtts)
|
||||
{
|
||||
mqtt.PropertyChanged -= Mqtt_PropertyChanged;
|
||||
}
|
||||
}
|
||||
|
||||
SetProperty(ref _mqtts, value);
|
||||
|
||||
if (_mqtts != null)
|
||||
{
|
||||
foreach (var mqtt in _mqtts)
|
||||
{
|
||||
mqtt.PropertyChanged += Mqtt_PropertyChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// public ObservableCollection<Mqtt> Mqtts
|
||||
// {
|
||||
// get => _mqtts;
|
||||
// set
|
||||
// {
|
||||
// if (_mqtts != null)
|
||||
// {
|
||||
// foreach (var mqtt in _mqtts)
|
||||
// {
|
||||
// mqtt.PropertyChanged -= Mqtt_PropertyChanged;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// SetProperty(ref _mqtts, value);
|
||||
//
|
||||
// if (_mqtts != null)
|
||||
// {
|
||||
// foreach (var mqtt in _mqtts)
|
||||
// {
|
||||
// mqtt.PropertyChanged += Mqtt_PropertyChanged;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
[ObservableProperty]
|
||||
private Mqtt _selectedMqtt;
|
||||
|
||||
@@ -136,6 +136,14 @@
|
||||
FontSize="14" />
|
||||
<TextBlock Text="{Binding ConnTime}" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal"
|
||||
Margin="0,2">
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Message}"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,8,0"
|
||||
FontSize="14" />
|
||||
<TextBlock Text="{Binding ConnectMessage}" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal"
|
||||
Margin="0,2">
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.FavoriteStar}"
|
||||
|
||||
Reference in New Issue
Block a user