diff --git a/Models/Mqtt.cs b/Models/Mqtt.cs
index 2bb2e5e..2135e6d 100644
--- a/Models/Mqtt.cs
+++ b/Models/Mqtt.cs
@@ -1,11 +1,12 @@
-using PMSWPF.Enums;
+using CommunityToolkit.Mvvm.ComponentModel;
+using PMSWPF.Enums;
namespace PMSWPF.Models;
///
/// 表示MQTT配置信息。
///
-public class Mqtt
+public partial class Mqtt : ObservableObject
{
///
/// MQTT客户端ID。
@@ -30,7 +31,8 @@ public class Mqtt
///
/// 是否启用此MQTT配置。
///
- public bool IsActive { get; set; }
+ [ObservableProperty]
+ private bool _isActive;
///
/// 是否设置为默认MQTT客户端。
@@ -81,4 +83,10 @@ public class Mqtt
/// 关联的变量数据列表。
///
public List? VariableDatas { get; set; }
+
+ ///
+ /// 是否连接。
+ ///
+ [ObservableProperty]
+ private bool _isConnected;
}
\ No newline at end of file
diff --git a/Services/MqttBackgroundService.cs b/Services/MqttBackgroundService.cs
index a230a90..d4660b1 100644
--- a/Services/MqttBackgroundService.cs
+++ b/Services/MqttBackgroundService.cs
@@ -141,35 +141,42 @@ namespace PMSWPF.Services
private async Task LoadMqttConfigurations()
{
// 从数据服务获取所有MQTT配置。
- var mqtts = await _dataServices.GetMqttsAsync();
- foreach (var mqtt in mqtts)
+ var allMqtts = await _dataServices.GetMqttsAsync();
+ var activeMqtts = allMqtts.Where(m => m.IsActive).ToList();
+ var activeMqttIds = activeMqtts.Select(m => m.Id).ToHashSet();
+
+ // 断开并移除不再活跃或已删除的MQTT客户端。
+ var clientsToDisconnect = _mqttClients.Keys.Except(activeMqttIds).ToList();
+ foreach (var id in clientsToDisconnect)
+ {
+ if (_mqttClients.TryGetValue(id, out var client))
+ {
+ if (client.IsConnected)
+ {
+ await client.DisconnectAsync();
+ // 更新模型中的连接状态
+ if (_mqttConfigurations.TryGetValue(id, out var mqttConfig))
+ {
+ mqttConfig.IsConnected = false;
+ }
+ }
+ _mqttClients.Remove(id);
+ NlogHelper.Info($"Disconnected and removed MQTT client for ID: {id} (no longer active or removed).");
+ }
+ _mqttConfigurations.Remove(id);
+ _mqttVariableData.Remove(id);
+ }
+
+ // 连接或更新活跃的客户端。
+ foreach (var mqtt in activeMqtts)
{
- // 如果客户端字典中不包含当前MQTT配置的客户端,则尝试连接。
if (!_mqttClients.ContainsKey(mqtt.Id))
{
await ConnectMqttClient(mqtt);
}
- // 更新或添加MQTT配置到字典。
+ // 始终更新或添加MQTT配置到字典。
_mqttConfigurations[mqtt.Id] = mqtt;
}
-
- // 断开并移除不再配置中的MQTT客户端。
- var removedMqttIds = _mqttClients.Keys.Except(mqtts.Select(m => m.Id)).ToList();
- foreach (var id in removedMqttIds)
- {
- if (_mqttClients.ContainsKey(id))
- {
- var client = _mqttClients[id];
- if (client.IsConnected)
- {
- await client.DisconnectAsync();
- }
- _mqttClients.Remove(id);
- NlogHelper.Info($"Disconnected and removed MQTT client for ID: {id}");
- }
- _mqttConfigurations.Remove(id);
- _mqttVariableData.Remove(id);
- }
}
///
@@ -197,6 +204,7 @@ namespace PMSWPF.Services
{
NlogHelper.Info($"Connected to MQTT broker: {mqtt.Name}");
NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}");
+ mqtt.IsConnected = true;
});
// 设置断开连接事件处理程序。
@@ -204,6 +212,7 @@ namespace PMSWPF.Services
{
NlogHelper.Warn($"Disconnected from MQTT broker: {mqtt.Name}. Reason: {e.Reason}");
NotificationHelper.ShowInfo($"与MQTT服务器断开连接: {mqtt.Name}");
+ mqtt.IsConnected = false;
// 尝试重新连接。
await Task.Delay(TimeSpan.FromSeconds(5)); // 等待5秒后重连
try
diff --git a/ViewModels/MqttsViewModel.cs b/ViewModels/MqttsViewModel.cs
index 256a671..57430e6 100644
--- a/ViewModels/MqttsViewModel.cs
+++ b/ViewModels/MqttsViewModel.cs
@@ -20,9 +20,33 @@ public partial class MqttsViewModel : ViewModelBase
private readonly ILogger _logger;
private readonly NavgatorServices _navgatorServices;
- [ObservableProperty]
private ObservableCollection _mqtts;
+ public ObservableCollection 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;
@@ -52,6 +76,26 @@ public partial class MqttsViewModel : ViewModelBase
};
}
+ private async void Mqtt_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
+ {
+ if (e.PropertyName == nameof(Mqtt.IsActive))
+ {
+ if (sender is Mqtt mqtt)
+ {
+ try
+ {
+ await _mqttRepository.Edit(mqtt);
+ NotificationHelper.ShowSuccess($"MQTT: {mqtt.Name} 的启用状态已更新。");
+ MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
+ }
+ catch (Exception ex)
+ {
+ NotificationHelper.ShowError($"更新MQTT启用状态失败: {mqtt.Name} - {ex.Message}", ex);
+ }
+ }
+ }
+ }
+
[RelayCommand]
public async void AddMqtt()
{
diff --git a/Views/MqttsView.xaml b/Views/MqttsView.xaml
index cf1b07b..f921381 100644
--- a/Views/MqttsView.xaml
+++ b/Views/MqttsView.xaml
@@ -12,12 +12,21 @@
d:DesignWidth="300">
-
+
+
+