2025-09-09 13:35:16 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-10-05 14:45:41 +08:00
|
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MQTT数据服务类,负责管理MQTT服务器相关的数据和操作。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttDataService : IMqttDataService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper _mapper;
|
2025-09-09 15:28:07 +08:00
|
|
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
2025-10-05 14:45:41 +08:00
|
|
|
|
private readonly IMqttManagementService _mqttManagementService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
private readonly IMenuDataService _menuDataService;
|
|
|
|
|
|
private readonly IMenuManagementService _menuManagementServiceImpl;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MqttDataService类的构造函数。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
|
|
|
|
|
/// <param name="mqttAppService">MQTT应用服务实例。</param>
|
2025-10-05 17:07:17 +08:00
|
|
|
|
public MqttDataService(IMapper mapper, IAppDataStorageService appDataStorageService, IMqttManagementService mqttManagementService, IMenuDataService menuDataService, IMenuManagementService menuManagementServiceImpl, IDataStorageService dataStorageService)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mapper = mapper;
|
2025-09-09 15:28:07 +08:00
|
|
|
|
_appDataStorageService = appDataStorageService;
|
2025-10-05 14:45:41 +08:00
|
|
|
|
_mqttManagementService = mqttManagementService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
_menuDataService = menuDataService;
|
|
|
|
|
|
_menuManagementServiceImpl = menuManagementServiceImpl;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
_dataStorageService = dataStorageService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载所有MQTT服务器数据。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task LoadMqttServers()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 加载MQTT服务器数据
|
2025-10-05 00:28:25 +08:00
|
|
|
|
foreach (var mqttServerDto in _appDataStorageService.MqttServers.Values)
|
|
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
_dataStorageService.MqttServers.TryAdd(mqttServerDto.Id, _mapper.Map<MqttServerItem>(mqttServerDto));
|
2025-10-05 00:28:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 记录异常或处理错误
|
|
|
|
|
|
Console.WriteLine($"加载MQTT服务器数据时发生错误: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public async Task<MqttServerItem> AddMqttServer(MqttServerItem mqttServer)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
var addMqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map<MqttServerDto>(mqttServer));
|
|
|
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
|
MqttServerItem mqttServerItem = _mapper.Map<MqttServerItem>(addMqttServerDto);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
_dataStorageService.MqttServers.Add(mqttServerItem.Id, mqttServerItem);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var mqttRootMenu = _dataStorageService.Menus.FirstOrDefault(m => m.Header == "Mqtt服务器");
|
|
|
|
|
|
|
|
|
|
|
|
if (mqttRootMenu is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var mqttServerMenu = new MenuBeanDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
Header = mqttServerItem.ServerName,
|
|
|
|
|
|
TargetId = mqttServerItem.Id,
|
|
|
|
|
|
ParentId = mqttRootMenu.Id,
|
|
|
|
|
|
Icon = "\uE753", // 使用设备图标
|
|
|
|
|
|
MenuType = MenuType.MqttServerMenu,
|
|
|
|
|
|
TargetViewKey = nameof(MqttServerDetailViewModel),
|
|
|
|
|
|
};
|
2025-10-06 18:17:56 +08:00
|
|
|
|
await _menuDataService.AddMenuItem(_mapper.Map<MenuItem>(mqttServerMenu));
|
2025-10-05 17:07:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
return mqttServerItem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public async Task<bool> UpdateMqttServer(MqttServerItem mqttServer)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dto = _mapper.Map<MqttServerDto>(mqttServer);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
var result = await _mqttManagementService.UpdateMqttServerAsync(dto);
|
|
|
|
|
|
|
|
|
|
|
|
if (result > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新菜单项
|
|
|
|
|
|
var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.MqttServerMenu && m.TargetId == mqttServer.Id);
|
|
|
|
|
|
if (menu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新菜单标题
|
|
|
|
|
|
menu.Header = mqttServer.ServerName;
|
|
|
|
|
|
|
|
|
|
|
|
// 使用菜单管理服务更新菜单
|
|
|
|
|
|
var menuDto = _mapper.Map<MenuBeanDto>(menu);
|
|
|
|
|
|
await _menuManagementServiceImpl.UpdateMenuAsync(menuDto);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result > 0;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public async Task<bool> DeleteMqttServer(MqttServerItem mqttServer)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
|
// 从数据库和内存中删除MQTT服务器
|
|
|
|
|
|
var result = await _mqttManagementService.DeleteMqttServerAsync(mqttServer.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 从界面删除MQTT服务器菜单
|
|
|
|
|
|
var mqttServerMenu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.MqttServerMenu && m.TargetId == mqttServer.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (mqttServerMenu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _menuDataService.DeleteMenuItem(mqttServerMenu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从界面删除MQTT服务器
|
|
|
|
|
|
_dataStorageService.MqttServers.Remove(mqttServer.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|