Files
DMS/DMS.WPF/ViewModels/MqttsViewModel.cs
David P.G a6fc543e4f 1 feat(mqtt): 实现MQTT服务器状态管理与事件系统
2
    3 1. 在MqttServer和MqttServerDto模型中添加IsConnect属性,用于跟踪连接状态
    4 2. 重构MqttManagementService服务,使用事件驱动方式管理服务器状态变化
    5 3. 实现MqttServerChangedEventArgs事件参数类,支持区分不同变更类型
    6 4. 在IEventService中添加OnMqttServerChanged事件,实现事件通知机制
    7 5. 优化数据存储结构,将MqttServers从ObservableCollection改为ObservableDictionary
    8 6. 更新MqttServiceManager以正确处理连接状态和事件触发
    9 7. 在WPF层更新UI以响应服务器状态变化
   10 8. 删除不再需要的Helper类(DataServicesHelper, MessageHelper, SiemensHelper)
   11 9. 在NLog配置中添加调试器输出目标以便调试
   12 10. 完善VariableHistoryViewModel防止空引用异常
2025-10-05 00:28:25 +08:00

192 lines
6.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.Logging;
using ObservableCollections;
using System.Collections.ObjectModel;
namespace DMS.WPF.ViewModels;
/// <summary>
/// MQTT服务器管理视图模型负责MQTT服务器的增删改查操作。
/// </summary>
public partial class MqttsViewModel : ViewModelBase
{
private readonly IWPFDataService _wpfDataService;
private readonly IMqttAppService _mqttAppService;
private readonly IDataStorageService _dataStorageService;
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
private readonly INavigationService _navigationService;
private readonly INotificationService _notificationService;
/// <summary>
/// 设备列表。
/// </summary>
[ObservableProperty]
private INotifyCollectionChangedSynchronizedViewList<MqttServerItemViewModel> _mqttServeise;
/// <summary>
/// 当前选中的MQTT服务器。
/// </summary>
[ObservableProperty]
private MqttServerItemViewModel _selectedMqtt;
private readonly ILogger<MqttsViewModel> _logger;
public MqttsViewModel(
ILogger<MqttsViewModel> logger,
IDialogService dialogService,
IWPFDataService wpfDataService,
IMqttAppService mqttAppService,
IDataStorageService dataStorageService,
IMapper mapper,
INavigationService navigationService,
INotificationService notificationService
)
{
_logger = logger;
_dialogService = dialogService;
_wpfDataService = wpfDataService;
_mqttAppService = mqttAppService;
_dataStorageService = dataStorageService;
_mapper = mapper;
_navigationService = navigationService;
_notificationService = notificationService;
_mqttServeise = _dataStorageService.MqttServers.ToNotifyCollectionChanged(x=>x.Value);
}
/// <summary>
/// 添加MQTT服务器命令。
/// </summary>
[RelayCommand]
public async Task AddMqtt()
{
try
{
// 1. 显示添加MQTT服务器对话框
MqttServerItemViewModel mqtt = await _dialogService.ShowDialogAsync(new MqttDialogViewModel()
{
Title = "添加MQTT服务器",
PrimaryButText = "添加MQTT服务器"
});
// 如果用户取消或对话框未返回MQTT服务器则直接返回
if (mqtt == null)
{
return;
}
var mqttItem = await _wpfDataService.MqttDataService.AddMqttServer(mqtt);
_notificationService.ShowSuccess($"MQTT服务器添加成功{mqttItem.ServerName}");
}
catch (Exception e)
{
_logger.LogError(e, "添加MQTT服务器的过程中发生错误");
_notificationService.ShowError($"添加MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 删除MQTT服务器命令。
/// </summary>
[RelayCommand]
public async Task DeleteMqtt()
{
try
{
if (SelectedMqtt == null)
{
_notificationService.ShowError("你没有选择任何MQTT服务器请选择MQTT服务器后再点击删除");
return;
}
var viewModel = new ConfirmDialogViewModel("删除MQTT服务器", $"确认要删除MQTT服务器名为:{SelectedMqtt.ServerName}", "删除MQTT服务器");
if (await _dialogService.ShowDialogAsync(viewModel))
{
var mqttName = SelectedMqtt.ServerName;
await _wpfDataService.MqttDataService.DeleteMqttServer(SelectedMqtt);
_notificationService.ShowSuccess($"删除MQTT服务器成功,服务器名:{mqttName}");
}
}
catch (Exception e)
{
_logger.LogError(e, "删除MQTT服务器的过程中发生错误");
_notificationService.ShowError($"删除MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 编辑MQTT服务器命令。
/// </summary>
[RelayCommand]
public async Task EditMqtt()
{
try
{
if (SelectedMqtt == null)
{
_notificationService.ShowError("你没有选择任何MQTT服务器请选择MQTT服务器后再点击编辑");
return;
}
MqttDialogViewModel mqttDialogViewModel = new MqttDialogViewModel(SelectedMqtt)
{
Title = "编辑MQTT服务器",
PrimaryButText = "保存修改"
};
// 1. 显示MQTT服务器对话框
MqttServerItemViewModel mqtt = await _dialogService.ShowDialogAsync(mqttDialogViewModel);
// 如果用户取消或对话框未返回MQTT服务器则直接返回
if (mqtt == null)
{
return;
}
await _wpfDataService.MqttDataService.UpdateMqttServer(mqtt);
// 更新UI
_mapper.Map(mqtt, SelectedMqtt);
_notificationService.ShowSuccess($"编辑MQTT服务器成功{mqtt.ServerName}");
}
catch (Exception e)
{
_logger.LogError(e, "编辑MQTT服务器的过程中发生错误");
_notificationService.ShowError($"编辑MQTT服务器的过程中发生错误{e.Message}", e);
}
}
/// <summary>
/// 导航到MQTT服务器详情页面。
/// </summary>
[RelayCommand]
private async Task NavigateToMqttDetail()
{
if (SelectedMqtt == null)
{
_notificationService.ShowWarn("请选择一个MQTT服务器以查看详情。");
return;
}
// 导航到MQTT服务器详情页
// var menu = new MenuItemViewModel
// {
// TargetViewKey = "MqttServerDetailView",
// TargetId = SelectedMqtt.Id
// };
await _navigationService.NavigateToAsync(
this, new NavigationParameter(nameof(MqttServerDetailViewModel), SelectedMqtt.Id, NavigationType.Mqtt));
}
}