2025-10-05 12:11:04 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2025-10-06 19:32:45 +08:00
|
|
|
|
|
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-10-06 19:32:45 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
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-10-18 17:18:09 +08:00
|
|
|
|
private readonly IAppStorageService _appStorageService;
|
2025-10-05 00:28:25 +08:00
|
|
|
|
private readonly IEventService _eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
public MqttManagementService(IMqttAppService mqttAppService,
|
2025-10-18 17:18:09 +08:00
|
|
|
|
IAppStorageService appStorageService,
|
2025-10-06 19:32:45 +08:00
|
|
|
|
IEventService eventService)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mqttAppService = mqttAppService;
|
2025-10-18 17:18:09 +08:00
|
|
|
|
_appStorageService = appStorageService;
|
2025-10-05 00:28:25 +08:00
|
|
|
|
_eventService = eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取MQTT服务器DTO。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
|
if (_appStorageService.MqttServers.TryGetValue(id,out var mqttServer))
|
2025-10-06 19:32:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
return mqttServer;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有MQTT服务器DTO列表。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
public async Task<List<MqttServer>> GetAllMqttServersAsync()
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
|
return _appStorageService.MqttServers.Values.ToList();
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
public async Task<int> UpdateMqttServerAsync(MqttServer mqttServer)
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
2025-10-06 19:32:45 +08:00
|
|
|
|
return await UpdateMqttServersAsync(new List<MqttServer>() { mqttServer });
|
2025-10-05 12:11:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步批量更新MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
public async Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
2025-10-06 19:32:45 +08:00
|
|
|
|
var result = await _mqttAppService.UpdateMqttServersAsync(mqttServers);
|
2025-10-05 12:11:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 批量更新成功后,更新内存中的MQTT服务器
|
2025-10-06 19:32:45 +08:00
|
|
|
|
if (result > 0 && mqttServers != null)
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
2025-10-06 19:32:45 +08:00
|
|
|
|
foreach (var mqttServer in mqttServers)
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
|
if (_appStorageService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 比较旧值和新值,确定哪个属性发生了变化
|
2025-10-06 19:32:45 +08:00
|
|
|
|
var changedProperties = GetChangedProperties(mMqttServer, mqttServer);
|
2025-10-05 12:11:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新内存中的MQTT服务器
|
2025-10-06 19:32:45 +08:00
|
|
|
|
mMqttServer.ServerName = mqttServer.ServerName;
|
|
|
|
|
|
mMqttServer.ServerUrl = mqttServer.ServerUrl;
|
|
|
|
|
|
mMqttServer.Port = mqttServer.Port;
|
|
|
|
|
|
mMqttServer.Username = mqttServer.Username;
|
|
|
|
|
|
mMqttServer.Password = mqttServer.Password;
|
|
|
|
|
|
mMqttServer.IsActive = mqttServer.IsActive;
|
|
|
|
|
|
mMqttServer.IsConnect = mqttServer.IsConnect;
|
|
|
|
|
|
mMqttServer.SubscribeTopic = mqttServer.SubscribeTopic;
|
|
|
|
|
|
mMqttServer.PublishTopic = mqttServer.PublishTopic;
|
|
|
|
|
|
mMqttServer.ClientId = mqttServer.ClientId;
|
|
|
|
|
|
mMqttServer.MessageFormat = mqttServer.MessageFormat;
|
|
|
|
|
|
mMqttServer.MessageHeader = mqttServer.MessageHeader;
|
|
|
|
|
|
mMqttServer.MessageContent = mqttServer.MessageContent;
|
|
|
|
|
|
mMqttServer.MessageFooter = mqttServer.MessageFooter;
|
2025-10-05 12:11:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 为每个发生变化的属性触发事件
|
|
|
|
|
|
foreach (var property in changedProperties)
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
2025-10-06 19:32:45 +08:00
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServer, property));
|
2025-10-05 12:11:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果内存中不存在该MQTT服务器,则直接添加
|
2025-10-18 17:18:09 +08:00
|
|
|
|
_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
2025-10-05 12:11:04 +08:00
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
2025-10-06 19:32:45 +08:00
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer, MqttServerPropertyType.All));
|
2025-10-05 12:11:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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-18 17:18:09 +08:00
|
|
|
|
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
|
2025-10-05 00:28:25 +08:00
|
|
|
|
{
|
2025-10-05 12:11:04 +08:00
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
2025-10-06 19:32:45 +08:00
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerFromCache));
|
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)
|
|
|
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
|
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServer))
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
2025-10-06 19:32:45 +08:00
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
|
2025-10-05 12:11:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 17:07:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建MQTT服务器及其菜单项。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
public async Task<MqttServer> CreateMqttServerAsync(MqttServer mqttServer)
|
2025-10-05 17:07:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 首先创建MQTT服务器
|
2025-10-06 19:32:45 +08:00
|
|
|
|
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServer);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (mqttServerId > 0)
|
|
|
|
|
|
{
|
2025-10-06 19:32:45 +08:00
|
|
|
|
mqttServer.Id = mqttServerId;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将MQTT服务器添加到内存中
|
2025-10-18 17:18:09 +08:00
|
|
|
|
if (_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
|
2025-10-05 17:07:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
_eventService.RaiseMqttServerChanged(
|
2025-10-06 19:32:45 +08:00
|
|
|
|
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer));
|
2025-10-05 17:07:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-06 19:32:45 +08:00
|
|
|
|
return mqttServer; // 返回null表示创建失败
|
2025-10-05 17:07:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 12:11:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取发生变化的属性列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="oldMqttServer">旧MQTT服务器值</param>
|
|
|
|
|
|
/// <param name="newMqttServer">新MQTT服务器值</param>
|
|
|
|
|
|
/// <returns>发生变化的属性列表</returns>
|
2025-10-06 19:32:45 +08:00
|
|
|
|
private List<MqttServerPropertyType> GetChangedProperties(MqttServer oldMqttServer, MqttServer newMqttServer)
|
2025-10-05 12:11:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
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-10-18 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步加载所有MQTT服务器数据到内存中。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task LoadAllMqttServersAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
_appStorageService.MqttServers.Clear();
|
|
|
|
|
|
var mqttServers = await _mqttAppService.GetAllMqttServersAsync();
|
|
|
|
|
|
// 加载MQTT服务器数据到内存
|
|
|
|
|
|
foreach (var mqttServer in mqttServers)
|
|
|
|
|
|
{
|
|
|
|
|
|
_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
|
}
|