2025-09-07 21:16:56 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.DTOs.Events;
|
|
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
using DMS.Core.Interfaces;
|
|
|
|
|
|
using DMS.Core.Enums;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DMS.Application.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <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-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当MQTT服务器数据发生变化时触发
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler<MqttServerChangedEventArgs> MqttServerChanged;
|
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
|
public MqttManagementService(IMqttAppService mqttAppService,IAppDataStorageService appDataStorageService)
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mqttAppService = mqttAppService;
|
2025-09-09 15:28:07 +08:00
|
|
|
|
_appDataStorageService = appDataStorageService;
|
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>
|
|
|
|
|
|
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _mqttAppService.UpdateMqttServerAsync(mqttServerDto);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除一个MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task DeleteMqttServerAsync(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _mqttAppService.DeleteMqttServerAsync(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在内存中添加MQTT服务器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void AddMqttServerToMemory(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
|
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Added, mqttServerDto));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在内存中更新MQTT服务器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void UpdateMqttServerInMemory(MqttServerDto mqttServerDto)
|
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
|
_appDataStorageService.MqttServers.AddOrUpdate(mqttServerDto.Id, mqttServerDto, (key, oldValue) => mqttServerDto);
|
2025-09-07 21:16:56 +08:00
|
|
|
|
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Updated, mqttServerDto));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在内存中删除MQTT服务器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RemoveMqttServerFromMemory(int mqttServerId)
|
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
|
if (_appDataStorageService.MqttServers.TryRemove(mqttServerId, out var mqttServerDto))
|
2025-09-07 21:16:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Deleted, mqttServerDto));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 触发MQTT服务器变更事件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual void OnMqttServerChanged(MqttServerChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
MqttServerChanged?.Invoke(this, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|