Files
DMS/DMS.Application/Services/Management/MqttManagementService.cs
David P.G a6fc543e4f 1 feat(mqtt): 实现MQTT服务器状态管理与事件系统
2
    3 1. 在MqttServer和MqttServerDto模型中添加IsConnect属性,用于跟踪连接状态
    4 2. 重构MqttManagementService服务,使用事件驱动方式管理服务器状态变化
    5 3. 实现MqttServerChangedEventArgs事件参数类,支持区分不同变更类型
    6 4. 在IEventService中添加OnMqttServerChanged事件,实现事件通知机制
    7 5. 优化数据存储结构,将MqttServers从ObservableCollection改为ObservableDictionary
    8 6. 更新MqttServiceManager以正确处理连接状态和事件触发
    9 7. 在WPF层更新UI以响应服务器状态变化
   10 8. 删除不再需要的Helper类(DataServicesHelper, MessageHelper, SiemensHelper)
   11 9. 在NLog配置中添加调试器输出目标以便调试
   12 10. 完善VariableHistoryViewModel防止空引用异常
2025-10-05 00:28:25 +08:00

93 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DMS.Application.DTOs;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces.Management;
using DMS.Core.Enums;
namespace DMS.Application.Services.Management;
/// <summary>
/// MQTT管理服务负责MQTT相关的业务逻辑。
/// </summary>
public class MqttManagementService : IMqttManagementService
{
private readonly IMqttAppService _mqttAppService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IEventService _eventService;
public MqttManagementService(IMqttAppService mqttAppService, IAppDataStorageService appDataStorageService, IEventService eventService)
{
_mqttAppService = mqttAppService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
}
/// <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)
{
var result = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
// 创建成功后将MQTT服务器添加到内存中
if (result > 0)
{
mqttServerDto.Id = result; // 假设返回的ID是新创建的
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
{
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
}
}
return result;
}
/// <summary>
/// 异步更新一个已存在的MQTT服务器。
/// </summary>
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
{
await _mqttAppService.UpdateMqttServerAsync(mqttServerDto);
// 更新成功后更新内存中的MQTT服务器
_appDataStorageService.MqttServers.AddOrUpdate(mqttServerDto.Id, mqttServerDto, (key, oldValue) => mqttServerDto);
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mqttServerDto));
}
/// <summary>
/// 异步删除一个MQTT服务器。
/// </summary>
public async Task DeleteMqttServerAsync(int id)
{
var mqttServer = await _mqttAppService.GetMqttServerByIdAsync(id); // 获取MQTT服务器信息用于内存删除
var result = await _mqttAppService.DeleteMqttServerAsync(id);
// 删除成功后从内存中移除MQTT服务器
if (result>0)
{
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
{
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
}
}
}
}