Files
DMS/DMS.WPF/ViewModels/MqttsViewModel.cs

190 lines
6.8 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
2025-09-06 12:03:39 +08:00
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2025-09-06 12:03:39 +08:00
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
2025-09-16 12:29:09 +08:00
using DMS.Application.Interfaces.Database;
using DMS.Core.Enums;
using DMS.Core.Models;
2025-09-03 18:22:01 +08:00
using DMS.WPF.Interfaces;
2025-07-26 10:05:43 +08:00
using DMS.WPF.Services;
2025-09-06 12:03:39 +08:00
using DMS.WPF.ViewModels.Dialogs;
2025-07-26 10:05:43 +08:00
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.Logging;
2025-07-19 11:11:01 +08:00
namespace DMS.WPF.ViewModels;
2025-09-06 12:03:39 +08:00
/// <summary>
/// MQTT服务器管理视图模型负责MQTT服务器的增删改查操作。
/// </summary>
public partial class MqttsViewModel : ViewModelBase
{
private readonly IWPFDataService _wpfDataService;
2025-09-06 12:03:39 +08:00
private readonly IMqttAppService _mqttAppService;
private readonly IDataStorageService _dataStorageService;
2025-09-06 12:03:39 +08:00
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
2025-09-06 12:03:39 +08:00
private readonly INavigationService _navigationService;
private readonly INotificationService _notificationService;
/// <summary>
/// MQTT服务器列表。
/// </summary>
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private ObservableCollection<MqttServerItemViewModel> _mqtts;
2025-09-06 12:03:39 +08:00
/// <summary>
/// 当前选中的MQTT服务器。
/// </summary>
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private MqttServerItemViewModel _selectedMqtt;
2025-09-06 12:03:39 +08:00
private readonly ILogger<MqttsViewModel> _logger;
public MqttsViewModel(
2025-09-06 12:03:39 +08:00
ILogger<MqttsViewModel> logger,
IDialogService dialogService,
IWPFDataService wpfDataService,
2025-09-06 12:03:39 +08:00
IMqttAppService mqttAppService,
IDataStorageService dataStorageService,
2025-09-06 12:03:39 +08:00
IMapper mapper,
INavigationService navigationService,
INotificationService notificationService
)
{
_logger = logger;
_dialogService = dialogService;
_wpfDataService = wpfDataService;
2025-09-06 12:03:39 +08:00
_mqttAppService = mqttAppService;
_dataStorageService = dataStorageService;
2025-09-06 12:03:39 +08:00
_mapper = mapper;
_navigationService = navigationService;
_notificationService = notificationService;
Mqtts = _dataStorageService.MqttServers;
}
2025-09-06 12:03:39 +08:00
/// <summary>
/// 添加MQTT服务器命令。
/// </summary>
[RelayCommand]
2025-09-06 12:03:39 +08:00
public async Task AddMqtt()
{
2025-09-06 12:03:39 +08:00
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);
2025-09-06 12:03:39 +08:00
_notificationService.ShowSuccess($"MQTT服务器添加成功{mqttItem.ServerName}");
}
catch (Exception e)
{
_logger.LogError(e, "添加MQTT服务器的过程中发生错误");
_notificationService.ShowError($"添加MQTT服务器的过程中发生错误{e.Message}", e);
}
}
2025-09-06 12:03:39 +08:00
/// <summary>
/// 删除MQTT服务器命令。
/// </summary>
[RelayCommand]
2025-09-06 12:03:39 +08:00
public async Task DeleteMqtt()
{
2025-09-06 12:03:39 +08:00
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);
2025-09-06 12:03:39 +08:00
_notificationService.ShowSuccess($"删除MQTT服务器成功,服务器名:{mqttName}");
}
}
catch (Exception e)
{
_logger.LogError(e, "删除MQTT服务器的过程中发生错误");
_notificationService.ShowError($"删除MQTT服务器的过程中发生错误{e.Message}", e);
}
}
2025-09-06 12:03:39 +08:00
/// <summary>
/// 编辑MQTT服务器命令。
/// </summary>
[RelayCommand]
2025-09-06 12:03:39 +08:00
public async Task EditMqtt()
{
2025-09-06 12:03:39 +08:00
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);
2025-09-06 12:03:39 +08:00
// 更新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]
2025-09-06 12:03:39 +08:00
private async Task NavigateToMqttDetail()
{
2025-09-06 12:03:39 +08:00
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));
}
}