2025-10-05 12:11:04 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using AutoMapper;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
using DMS.Application.DTOs;
|
2025-09-16 14:42:23 +08:00
|
|
|
|
using DMS.Application.Events;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
|
|
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
using DMS.Core.Enums;
|
|
|
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
|
namespace DMS.Application.Services.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MQTT管理服务,负责MQTT相关的业务逻辑。
|
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
|
public class MqttManagementService : IMqttManagementService
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IMqttAppService _mqttAppService;
|
2025-09-09 15:28:07 +08:00
|
|
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
2025-10-05 00:28:25 +08:00
|
|
|
|
private readonly IEventService _eventService;
|
2025-10-05 12:11:04 +08:00
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
private readonly IDataProcessingService _dataProcessingService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
private readonly IMenuManagementService _menuManagementService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
public MqttManagementService(IMqttAppService mqttAppService,
|
|
|
|
|
|
IAppDataStorageService appDataStorageService,
|
|
|
|
|
|
IEventService eventService,
|
|
|
|
|
|
IMapper mapper,
|
2025-10-05 17:07:17 +08:00
|
|
|
|
IDataProcessingService dataProcessingService,
|
|
|
|
|
|
IMenuManagementService menuManagementService)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mqttAppService = mqttAppService;
|
2025-09-09 15:28:07 +08:00
|
|
|
|
_appDataStorageService = appDataStorageService;
|
2025-10-05 00:28:25 +08:00
|
|
|
|
_eventService = eventService;
|
2025-10-05 12:11:04 +08:00
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
_dataProcessingService = dataProcessingService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
_menuManagementService = menuManagementService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取MQTT服务器DTO。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _mqttAppService.GetMqttServerByIdAsync(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有MQTT服务器DTO列表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _mqttAppService.GetAllMqttServersAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-05 12:11:04 +08:00
|
|
|
|
public async Task<int> UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await UpdateMqttServersAsync(new List<MqttServerDto>() { mqttServerDto });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步批量更新MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-05 12:11:04 +08:00
|
|
|
|
var result = await _mqttAppService.UpdateMqttServersAsync(mqttServerDtos);
|
|
|
|
|
|
|
|
|
|
|
|
// 批量更新成功后,更新内存中的MQTT服务器
|
|
|
|
|
|
if (result > 0 && mqttServerDtos != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var mqttServerDto in mqttServerDtos)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_appDataStorageService.MqttServers.TryGetValue(mqttServerDto.Id, out var mMqttServerDto))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 比较旧值和新值,确定哪个属性发生了变化
|
|
|
|
|
|
var changedProperties = GetChangedProperties(mMqttServerDto, mqttServerDto);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新内存中的MQTT服务器
|
|
|
|
|
|
_mapper.Map(mqttServerDto, mMqttServerDto);
|
|
|
|
|
|
|
|
|
|
|
|
// 为每个发生变化的属性触发事件
|
|
|
|
|
|
foreach (var property in changedProperties)
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
|
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, property));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果内存中不存在该MQTT服务器,则直接添加
|
|
|
|
|
|
_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto);
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
|
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto, MqttServerPropertyType.All));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-05 00:28:25 +08:00
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除一个MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-05 12:11:04 +08:00
|
|
|
|
public async Task<bool> DeleteMqttServerAsync(int id)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-05 00:28:25 +08:00
|
|
|
|
var mqttServer = await _mqttAppService.GetMqttServerByIdAsync(id); // 获取MQTT服务器信息用于内存删除
|
2025-10-05 12:11:04 +08:00
|
|
|
|
var result = await _mqttAppService.DeleteMqttServerAsync(id) > 0;
|
2025-10-05 00:28:25 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除成功后,从内存中移除MQTT服务器
|
2025-10-05 12:11:04 +08:00
|
|
|
|
if (result && mqttServer != null)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-05 00:28:25 +08:00
|
|
|
|
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
|
|
|
|
|
|
{
|
2025-10-05 12:11:04 +08:00
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
|
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
|
2025-10-05 00:28:25 +08:00
|
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
2025-10-05 12:11:04 +08:00
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步批量删除MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<bool> DeleteMqttServersAsync(List<int> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await _mqttAppService.DeleteMqttServersAsync(ids);
|
|
|
|
|
|
|
|
|
|
|
|
// 批量删除成功后,从内存中移除MQTT服务器
|
|
|
|
|
|
if (result && ids != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
|
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 17:07:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建MQTT服务器及其菜单项。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<MqttServerDto> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 首先创建MQTT服务器
|
|
|
|
|
|
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
|
|
|
|
|
|
|
|
|
|
|
|
if (mqttServerId > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
mqttServerDto.Id = mqttServerId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将MQTT服务器添加到内存中
|
|
|
|
|
|
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
|
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return mqttServerDto; // 返回null表示创建失败
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取发生变化的属性列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="oldMqttServer">旧MQTT服务器值</param>
|
|
|
|
|
|
/// <param name="newMqttServer">新MQTT服务器值</param>
|
|
|
|
|
|
/// <returns>发生变化的属性列表</returns>
|
|
|
|
|
|
private List<MqttServerPropertyType> GetChangedProperties(MqttServerDto oldMqttServer, MqttServerDto newMqttServer)
|
|
|
|
|
|
{
|
|
|
|
|
|
var changedProperties = new List<MqttServerPropertyType>();
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.ServerName != newMqttServer.ServerName)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.ServerName);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.ServerUrl != newMqttServer.ServerUrl)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.ServerUrl);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.Port != newMqttServer.Port)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.Port);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.IsConnect != newMqttServer.IsConnect)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.IsConnect);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.Username != newMqttServer.Username)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.Username);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.Password != newMqttServer.Password)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.Password);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.IsActive != newMqttServer.IsActive)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.IsActive);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.SubscribeTopic != newMqttServer.SubscribeTopic)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.SubscribeTopic);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.PublishTopic != newMqttServer.PublishTopic)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.PublishTopic);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.ClientId != newMqttServer.ClientId)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.ClientId);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.MessageFormat != newMqttServer.MessageFormat)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.MessageFormat);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.MessageHeader != newMqttServer.MessageHeader)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.MessageHeader);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.MessageContent != newMqttServer.MessageContent)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.MessageContent);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldMqttServer.MessageFooter != newMqttServer.MessageFooter)
|
|
|
|
|
|
changedProperties.Add(MqttServerPropertyType.MessageFooter);
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
return changedProperties;
|
|
|
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|