Files
DMS/DMS.Application/Services/Database/MqttAppService.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

120 lines
4.5 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 AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Core.Interfaces;
using DMS.Core.Models;
namespace DMS.Application.Services.Database;
/// <summary>
/// MQTT应用服务负责处理MQTT服务器相关的业务逻辑。
/// 实现 <see cref="IMqttAppService"/> 接口。
/// </summary>
public class MqttAppService : IMqttAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
/// <summary>
/// 构造函数通过依赖注入获取仓储管理器和AutoMapper实例。
/// </summary>
/// <param name="repoManager">仓储管理器实例。</param>
/// <param name="mapper">AutoMapper 实例。</param>
public MqttAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
/// <summary>
/// 异步根据ID获取MQTT服务器数据传输对象。
/// </summary>
/// <param name="id">MQTT服务器ID。</param>
/// <returns>MQTT服务器数据传输对象。</returns>
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
return _mapper.Map<MqttServerDto>(mqttServer);
}
/// <summary>
/// 异步获取所有MQTT服务器数据传输对象列表。
/// </summary>
/// <returns>MQTT服务器数据传输对象列表。</returns>
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
{
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
return _mapper.Map<List<MqttServerDto>>(mqttServers);
}
/// <summary>
/// 异步创建一个新MQTT服务器事务性操作
/// </summary>
/// <param name="mqttServerDto">要创建的MQTT服务器数据传输对象。</param>
/// <returns>新创建MQTT服务器的ID。</returns>
/// <exception cref="ApplicationException">如果创建MQTT服务器时发生错误。</exception>
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
{
try
{
await _repoManager.BeginTranAsync();
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
await _repoManager.MqttServers.AddAsync(mqttServer);
await _repoManager.CommitAsync();
return mqttServer.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建MQTT服务器时发生错误操作已回滚。", ex);
}
}
/// <summary>
/// 异步更新一个已存在的MQTT服务器事务性操作
/// </summary>
/// <param name="mqttServerDto">要更新的MQTT服务器数据传输对象。</param>
/// <returns>表示异步操作的任务。</returns>
/// <exception cref="ApplicationException">如果找不到MQTT服务器或更新MQTT服务器时发生错误。</exception>
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
{
try
{
await _repoManager.BeginTranAsync();
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer == null)
{
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
}
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新MQTT服务器时发生错误操作已回滚。", ex);
}
}
/// <summary>
/// 异步根据ID删除一个MQTT服务器事务性操作
/// </summary>
/// <param name="id">要删除MQTT服务器的ID。</param>
/// <returns>如果删除成功则为 true否则为 false。</returns>
/// <exception cref="ApplicationException">如果删除MQTT服务器时发生错误。</exception>
public async Task<int> DeleteMqttServerAsync(int id)
{
try
{
return await _repoManager.MqttServers.DeleteByIdAsync(id);
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException($"删除MQTT服务器时发生错误操作已回滚,错误信息:{ex.Message}", ex);
}
}
}