refactor:删除MqttServerDto,将使用MqttServerDto的全部转为使用MqttServer

This commit is contained in:
2025-10-06 19:32:45 +08:00
parent c85f89db33
commit 94ad940e03
17 changed files with 174 additions and 222 deletions

View File

@@ -1,4 +1,4 @@
using DMS.Application.DTOs;
using DMS.Core.Models;
namespace DMS.Application.DTOs
{
@@ -10,7 +10,7 @@ namespace DMS.Application.DTOs
/// <summary>
/// MQTT服务器信息
/// </summary>
public MqttServerDto MqttServer { get; set; }
public MqttServer MqttServer { get; set; }
/// <summary>
/// 菜单项信息

View File

@@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
using DMS.Core.Models;
namespace DMS.Application.DTOs;
/// <summary>
/// 用于在UI上显示MQTT服务器配置信息的DTO。
/// </summary>
public class MqttServerDto
{
public int Id { get; set; }
public string ServerName { get; set; }
public string ServerUrl { get; set; }
public int Port { get; set; }
public bool IsConnect { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool IsActive { get; set; }
public string SubscribeTopic { get; set; }
public string PublishTopic { get; set; }
public string ClientId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? ConnectedAt { get; set; }
public long ConnectionDuration { get; set; }
public string MessageFormat { get; set; }
public string MessageHeader { get; set; }
public string MessageContent { get; set; }
public string MessageFooter { get; set; }
public List<MqttAlias> VariableAliases { get; set; } = new();
}

View File

@@ -1,5 +1,5 @@
using DMS.Application.DTOs;
using DMS.Core.Enums;
using DMS.Core.Models;
namespace DMS.Application.Events
{
@@ -14,9 +14,9 @@ namespace DMS.Application.Events
public ActionChangeType ChangeType { get; }
/// <summary>
/// MQTT服务器DTO
/// MQTT服务器
/// </summary>
public MqttServerDto MqttServer { get; }
public MqttServer MqttServer { get; }
/// <summary>
/// 发生变化的属性类型
@@ -28,9 +28,9 @@ namespace DMS.Application.Events
/// 构造函数
/// </summary>
/// <param name="changeType">变更类型</param>
/// <param name="mqttServer">MQTT服务器DTO</param>
/// <param name="mqttServer">MQTT服务器</param>
/// <param name="propertyType">发生变化的属性类型</param>
public MqttServerChangedEventArgs(ActionChangeType changeType, MqttServerDto mqttServer, MqttServerPropertyType propertyType = MqttServerPropertyType.All)
public MqttServerChangedEventArgs(ActionChangeType changeType, MqttServer mqttServer, MqttServerPropertyType propertyType = MqttServerPropertyType.All)
{
ChangeType = changeType;
MqttServer = mqttServer;

View File

@@ -1,4 +1,3 @@
using DMS.Application.DTOs;
using DMS.Core.Models;
namespace DMS.Application.Interfaces.Database;
@@ -9,29 +8,29 @@ namespace DMS.Application.Interfaces.Database;
public interface IMqttAppService
{
/// <summary>
/// 异步根据ID获取MQTT服务器DTO
/// 异步根据ID获取MQTT服务器。
/// </summary>
Task<MqttServerDto> GetMqttServerByIdAsync(int id);
Task<MqttServer> GetMqttServerByIdAsync(int id);
/// <summary>
/// 异步获取所有MQTT服务器DTO列表。
/// 异步获取所有MQTT服务器列表。
/// </summary>
Task<List<MqttServerDto>> GetAllMqttServersAsync();
Task<List<MqttServer>> GetAllMqttServersAsync();
/// <summary>
/// 异步创建一个新的MQTT服务器。
/// </summary>
Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto);
Task<int> CreateMqttServerAsync(MqttServer mqttServer);
/// <summary>
/// 异步更新一个已存在的MQTT服务器。
/// </summary>
Task UpdateMqttServerAsync(MqttServerDto mqttServerDto);
Task UpdateMqttServerAsync(MqttServer mqttServer);
/// <summary>
/// 异步批量更新MQTT服务器。
/// </summary>
Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos);
Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers);
/// <summary>
/// 异步根据ID删除一个MQTT服务器。

View File

@@ -34,7 +34,7 @@ public interface IAppDataStorageService
/// <summary>
/// 安全字典用于存储所有MQTT服务器数据
/// </summary>
ConcurrentDictionary<int, MqttServerDto> MqttServers { get; }
ConcurrentDictionary<int, MqttServer> MqttServers { get; }
/// <summary>
/// 安全字典,用于存储所有日志数据

View File

@@ -1,33 +1,10 @@
using DMS.Application.DTOs;
using DMS.Core.Models;
namespace DMS.Application.Interfaces.Management;
public interface IMqttManagementService
{
/// <summary>
/// 异步根据ID获取MQTT服务器DTO。
/// </summary>
Task<MqttServerDto> GetMqttServerByIdAsync(int id);
/// <summary>
/// 异步获取所有MQTT服务器DTO列表。
/// </summary>
Task<List<MqttServerDto>> GetAllMqttServersAsync();
/// <summary>
/// 异步创建一个新的MQTT服务器。
/// </summary>
Task<MqttServerDto> CreateMqttServerAsync(MqttServerDto mqttServerDto);
/// <summary>
/// 异步更新一个已存在的MQTT服务器。
/// </summary>
Task<int> UpdateMqttServerAsync(MqttServerDto mqttServerDto);
/// <summary>
/// 异步批量更新MQTT服务器。
/// </summary>
Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos);
Task<MqttServer> CreateMqttServerAsync(MqttServer mqttServer);
/// <summary>
/// 异步删除一个MQTT服务器。
@@ -38,5 +15,8 @@ public interface IMqttManagementService
/// 异步批量删除MQTT服务器。
/// </summary>
Task<bool> DeleteMqttServersAsync(List<int> ids);
Task<List<MqttServer>> GetAllMqttServersAsync();
Task<MqttServer> GetMqttServerByIdAsync(int id);
Task<int> UpdateMqttServerAsync(MqttServer mqttServer);
Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers);
}

View File

@@ -27,9 +27,7 @@ public class MappingProfile : Profile
CreateMap<VariableDto, VariableDto>()
.ReverseMap();
// MqttServer 映射
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
CreateMap<MqttServerDto, MqttServerDto>().ReverseMap();
// VariableHistory 映射
CreateMap<VariableHistory, VariableHistoryDto>()

View File

@@ -35,7 +35,7 @@ public class AppDataStorageService : IAppDataStorageService
/// <summary>
/// 安全字典用于存储所有MQTT服务器数据
/// </summary>
public ConcurrentDictionary<int, MqttServerDto> MqttServers { get; } = new();
public ConcurrentDictionary<int, MqttServer> MqttServers { get; } = new();
/// <summary>

View File

@@ -118,7 +118,7 @@ public class DataLoaderService : IDataLoaderService
if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer))
{
variableMqttAlias.MqttServer = _mapper.Map<MqttServer>(mqttServer);
variableMqttAlias.MqttServer = mqttServer;
mqttServer.VariableAliases?.Add(variableMqttAlias);
}
}
@@ -209,11 +209,11 @@ public class DataLoaderService : IDataLoaderService
public async Task LoadAllMqttServersAsync()
{
_appDataStorageService.MqttServers.Clear();
var mqttServerDtos =await _mqttAppService.GetAllMqttServersAsync();
var mqttServers =await _mqttAppService.GetAllMqttServersAsync();
// 加载MQTT服务器数据到内存
foreach (var mqttServerDto in mqttServerDtos)
foreach (var mqttServer in mqttServers)
{
_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto);
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
}
}

View File

@@ -1,6 +1,3 @@
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Core.Interfaces;
using DMS.Core.Models;
@@ -14,52 +11,47 @@ namespace DMS.Application.Services.Database;
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)
public MqttAppService(IRepositoryManager repoManager)
{
_repoManager = repoManager;
_mapper = mapper;
}
/// <summary>
/// 异步根据ID获取MQTT服务器数据传输对象
/// 异步根据ID获取MQTT服务器。
/// </summary>
/// <param name="id">MQTT服务器ID。</param>
/// <returns>MQTT服务器数据传输对象。</returns>
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
/// <returns>MQTT服务器。</returns>
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
return _mapper.Map<MqttServerDto>(mqttServer);
return await _repoManager.MqttServers.GetByIdAsync(id);
}
/// <summary>
/// 异步获取所有MQTT服务器数据传输对象列表。
/// 异步获取所有MQTT服务器列表。
/// </summary>
/// <returns>MQTT服务器数据传输对象列表。</returns>
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
/// <returns>MQTT服务器列表。</returns>
public async Task<List<MqttServer>> GetAllMqttServersAsync()
{
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
return _mapper.Map<List<MqttServerDto>>(mqttServers);
return mqttServers.ToList();
}
/// <summary>
/// 异步创建一个新MQTT服务器事务性操作
/// </summary>
/// <param name="mqttServerDto">要创建的MQTT服务器数据传输对象。</param>
/// <param name="mqttServer">要创建的MQTT服务器。</param>
/// <returns>新创建MQTT服务器的ID。</returns>
/// <exception cref="ApplicationException">如果创建MQTT服务器时发生错误。</exception>
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
public async Task<int> CreateMqttServerAsync(MqttServer mqttServer)
{
try
{
await _repoManager.BeginTranAsync();
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
await _repoManager.MqttServers.AddAsync(mqttServer);
await _repoManager.CommitAsync();
return mqttServer.Id;
@@ -74,21 +66,36 @@ public class MqttAppService : IMqttAppService
/// <summary>
/// 异步更新一个已存在的MQTT服务器事务性操作
/// </summary>
/// <param name="mqttServerDto">要更新的MQTT服务器数据传输对象。</param>
/// <param name="mqttServer">要更新的MQTT服务器。</param>
/// <returns>表示异步操作的任务。</returns>
/// <exception cref="ApplicationException">如果找不到MQTT服务器或更新MQTT服务器时发生错误。</exception>
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
public async Task UpdateMqttServerAsync(MqttServer mqttServer)
{
try
{
await _repoManager.BeginTranAsync();
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer == null)
var existingMqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServer.Id);
if (existingMqttServer == null)
{
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
throw new ApplicationException($"MQTT Server with ID {mqttServer.Id} not found.");
}
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
existingMqttServer.ServerName = mqttServer.ServerName;
existingMqttServer.ServerUrl = mqttServer.ServerUrl;
existingMqttServer.Port = mqttServer.Port;
existingMqttServer.Username = mqttServer.Username;
existingMqttServer.Password = mqttServer.Password;
existingMqttServer.IsActive = mqttServer.IsActive;
existingMqttServer.IsConnect = mqttServer.IsConnect;
existingMqttServer.SubscribeTopic = mqttServer.SubscribeTopic;
existingMqttServer.PublishTopic = mqttServer.PublishTopic;
existingMqttServer.ClientId = mqttServer.ClientId;
existingMqttServer.MessageFormat = mqttServer.MessageFormat;
existingMqttServer.MessageHeader = mqttServer.MessageHeader;
existingMqttServer.MessageContent = mqttServer.MessageContent;
existingMqttServer.MessageFooter = mqttServer.MessageFooter;
await _repoManager.MqttServers.UpdateAsync(existingMqttServer);
await _repoManager.CommitAsync();
}
catch (Exception ex)
@@ -121,22 +128,36 @@ public class MqttAppService : IMqttAppService
/// <summary>
/// 异步批量更新MQTT服务器事务性操作
/// </summary>
/// <param name="mqttServerDtos">要更新的MQTT服务器数据传输对象列表。</param>
/// <param name="mqttServers">要更新的MQTT服务器列表。</param>
/// <returns>成功更新的MQTT服务器数量。</returns>
/// <exception cref="ApplicationException">如果批量更新MQTT服务器时发生错误。</exception>
public async Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos)
public async Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers)
{
try
{
await _repoManager.BeginTranAsync();
var count = 0;
foreach (var mqttServerDto in mqttServerDtos)
foreach (var mqttServer in mqttServers)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer != null)
var existingMqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServer.Id);
if (existingMqttServer != null)
{
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
existingMqttServer.ServerName = mqttServer.ServerName;
existingMqttServer.ServerUrl = mqttServer.ServerUrl;
existingMqttServer.Port = mqttServer.Port;
existingMqttServer.Username = mqttServer.Username;
existingMqttServer.Password = mqttServer.Password;
existingMqttServer.IsActive = mqttServer.IsActive;
existingMqttServer.IsConnect = mqttServer.IsConnect;
existingMqttServer.SubscribeTopic = mqttServer.SubscribeTopic;
existingMqttServer.PublishTopic = mqttServer.PublishTopic;
existingMqttServer.ClientId = mqttServer.ClientId;
existingMqttServer.MessageFormat = mqttServer.MessageFormat;
existingMqttServer.MessageHeader = mqttServer.MessageHeader;
existingMqttServer.MessageContent = mqttServer.MessageContent;
existingMqttServer.MessageFooter = mqttServer.MessageFooter;
await _repoManager.MqttServers.UpdateAsync(existingMqttServer);
count++;
}
}

View File

@@ -1,11 +1,11 @@
using System.Collections.Concurrent;
using AutoMapper;
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;
using DMS.Core.Models;
namespace DMS.Application.Services.Management;
@@ -17,83 +17,91 @@ public class MqttManagementService : IMqttManagementService
private readonly IMqttAppService _mqttAppService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IEventService _eventService;
private readonly IMapper _mapper;
private readonly IDataProcessingService _dataProcessingService;
private readonly IMenuManagementService _menuManagementService;
public MqttManagementService(IMqttAppService mqttAppService,
IAppDataStorageService appDataStorageService,
IEventService eventService,
IMapper mapper,
IDataProcessingService dataProcessingService,
IMenuManagementService menuManagementService)
IEventService eventService)
{
_mqttAppService = mqttAppService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
_mapper = mapper;
_dataProcessingService = dataProcessingService;
_menuManagementService = menuManagementService;
}
/// <summary>
/// 异步根据ID获取MQTT服务器DTO。
/// </summary>
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
{
return await _mqttAppService.GetMqttServerByIdAsync(id);
if (_appDataStorageService.MqttServers.TryGetValue(id,out var mqttServer))
{
return mqttServer;
}
return null;
}
/// <summary>
/// 异步获取所有MQTT服务器DTO列表。
/// </summary>
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
public async Task<List<MqttServer>> GetAllMqttServersAsync()
{
return await _mqttAppService.GetAllMqttServersAsync();
return _appDataStorageService.MqttServers.Values.ToList();
}
/// <summary>
/// 异步更新一个已存在的MQTT服务器。
/// </summary>
public async Task<int> UpdateMqttServerAsync(MqttServerDto mqttServerDto)
public async Task<int> UpdateMqttServerAsync(MqttServer mqttServer)
{
return await UpdateMqttServersAsync(new List<MqttServerDto>() { mqttServerDto });
return await UpdateMqttServersAsync(new List<MqttServer>() { mqttServer });
}
/// <summary>
/// 异步批量更新MQTT服务器。
/// </summary>
public async Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos)
public async Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers)
{
var result = await _mqttAppService.UpdateMqttServersAsync(mqttServerDtos);
var result = await _mqttAppService.UpdateMqttServersAsync(mqttServers);
// 批量更新成功后更新内存中的MQTT服务器
if (result > 0 && mqttServerDtos != null)
if (result > 0 && mqttServers != null)
{
foreach (var mqttServerDto in mqttServerDtos)
foreach (var mqttServer in mqttServers)
{
if (_appDataStorageService.MqttServers.TryGetValue(mqttServerDto.Id, out var mMqttServerDto))
if (_appDataStorageService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
{
// 比较旧值和新值,确定哪个属性发生了变化
var changedProperties = GetChangedProperties(mMqttServerDto, mqttServerDto);
var changedProperties = GetChangedProperties(mMqttServer, mqttServer);
// 更新内存中的MQTT服务器
_mapper.Map(mqttServerDto, mMqttServerDto);
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;
// 为每个发生变化的属性触发事件
foreach (var property in changedProperties)
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, property));
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServer, property));
}
}
else
{
// 如果内存中不存在该MQTT服务器则直接添加
_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto);
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto, MqttServerPropertyType.All));
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer, MqttServerPropertyType.All));
}
}
}
@@ -112,10 +120,10 @@ public class MqttManagementService : IMqttManagementService
// 删除成功后从内存中移除MQTT服务器
if (result && mqttServer != null)
{
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerFromCache));
}
}
@@ -134,10 +142,10 @@ public class MqttManagementService : IMqttManagementService
{
foreach (var id in ids)
{
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServer))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
}
}
}
@@ -148,27 +156,27 @@ public class MqttManagementService : IMqttManagementService
/// <summary>
/// 异步创建MQTT服务器及其菜单项。
/// </summary>
public async Task<MqttServerDto> CreateMqttServerAsync(MqttServerDto mqttServerDto)
public async Task<MqttServer> CreateMqttServerAsync(MqttServer mqttServer)
{
// 首先创建MQTT服务器
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServer);
if (mqttServerId > 0)
{
mqttServerDto.Id = mqttServerId;
mqttServer.Id = mqttServerId;
// 将MQTT服务器添加到内存中
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
if (_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer));
}
}
return mqttServerDto; // 返回null表示创建失败
return mqttServer; // 返回null表示创建失败
}
/// <summary>
@@ -177,7 +185,7 @@ public class MqttManagementService : IMqttManagementService
/// <param name="oldMqttServer">旧MQTT服务器值</param>
/// <param name="newMqttServer">新MQTT服务器值</param>
/// <returns>发生变化的属性列表</returns>
private List<MqttServerPropertyType> GetChangedProperties(MqttServerDto oldMqttServer, MqttServerDto newMqttServer)
private List<MqttServerPropertyType> GetChangedProperties(MqttServer oldMqttServer, MqttServer newMqttServer)
{
var changedProperties = new List<MqttServerPropertyType>();

View File

@@ -6,6 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Configurations\**" />
<EmbeddedResource Remove="Configurations\**" />
<None Remove="Configurations\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="4.7.1.1" />
<PackageReference Include="MQTTnet" Version="3.0.16" />
@@ -26,8 +32,4 @@
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configurations\" />
</ItemGroup>
</Project>

View File

@@ -186,34 +186,15 @@ namespace DMS.Infrastructure.Services.Mqtt
_mqttServers.Clear();
// 从数据服务中心获取所有激活的MQTT服务器
var mqttServerDtos = _appDataStorageService.MqttServers.Values.ToList();
var mqttServers = _appDataStorageService.MqttServers.Values.ToList();
foreach (var mqttServerDto in mqttServerDtos)
foreach (var mqttServer in mqttServers)
{
// 将 MqttServerDto 转换为 MqttServerConfig
var mqttServer = new MqttServer
{
Id = mqttServerDto.Id,
ServerName = mqttServerDto.ServerName,
ServerUrl = mqttServerDto.ServerUrl,
Port = mqttServerDto.Port,
Username = mqttServerDto.Username,
Password = mqttServerDto.Password,
IsActive = mqttServerDto.IsActive,
SubscribeTopic = mqttServerDto.SubscribeTopic,
PublishTopic = mqttServerDto.PublishTopic,
ClientId = mqttServerDto.ClientId,
CreatedAt = mqttServerDto.CreatedAt,
ConnectedAt = mqttServerDto.ConnectedAt,
ConnectionDuration = mqttServerDto.ConnectionDuration,
MessageFormat = mqttServerDto.MessageFormat
};
_mqttServers.TryAdd(mqttServer.Id, mqttServer);
_mqttServiceManager.AddMqttServer(mqttServer);
}
_logger.LogInformation($"成功加载 {mqttServerDtos.Count} 个MQTT配置");
_logger.LogInformation($"成功加载 {mqttServers.Count} 个MQTT配置");
return true;
}
catch (Exception ex)

View File

@@ -1,8 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
@@ -23,7 +22,7 @@ namespace DMS.Infrastructure.Services.Mqtt
private readonly IAppDataCenterService _appDataCenterService;
private readonly IMqttServiceFactory _mqttServiceFactory;
private readonly IEventService _eventService;
private readonly IMapper _mapper;
private readonly ConcurrentDictionary<int, MqttDeviceContext> _mqttContexts;
private readonly SemaphoreSlim _semaphore;
private bool _disposed = false;
@@ -33,15 +32,13 @@ namespace DMS.Infrastructure.Services.Mqtt
IDataProcessingService dataProcessingService,
IAppDataCenterService appDataCenterService,
IMqttServiceFactory mqttServiceFactory,
IEventService eventService,
IMapper mapper)
IEventService eventService)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
_appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
_mqttServiceFactory = mqttServiceFactory ?? throw new ArgumentNullException(nameof(mqttServiceFactory));
_eventService = eventService ?? throw new ArgumentNullException(nameof(eventService));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_mqttContexts = new ConcurrentDictionary<int, MqttDeviceContext>();
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
@@ -187,8 +184,7 @@ namespace DMS.Infrastructure.Services.Mqtt
context.MqttServerConfig.IsConnect = false;
_logger.LogWarning("MQTT服务器 {ServerName} 连接失败", context.MqttServerConfig.ServerName);
}
//触发MQTT连接状态改变事件
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, _mapper.Map<MqttServerDto>(context.MqttServerConfig), MqttServerPropertyType.IsConnect));
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, context.MqttServerConfig, MqttServerPropertyType.IsConnect));
}
catch (Exception ex)
{
@@ -196,7 +192,7 @@ namespace DMS.Infrastructure.Services.Mqtt
context.MqttServerConfig.ServerName, ex.Message);
context.ReconnectAttempts++;
context.MqttServerConfig.IsConnect = false;
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, _mapper.Map<MqttServerDto>(context.MqttServerConfig), MqttServerPropertyType.IsConnect));
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, context.MqttServerConfig, MqttServerPropertyType.IsConnect));
}
finally
{
@@ -220,7 +216,7 @@ namespace DMS.Infrastructure.Services.Mqtt
// 如果连接状态从连接变为断开,触发事件
context.MqttServerConfig.IsConnect = false;
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, _mapper.Map<MqttServerDto>(context.MqttServerConfig), MqttServerPropertyType.IsConnect));
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, context.MqttServerConfig, MqttServerPropertyType.IsConnect));
}
catch (Exception ex)
{
@@ -366,7 +362,7 @@ namespace DMS.Infrastructure.Services.Mqtt
/// <summary>
/// 处理MQTT服务器添加事件
/// </summary>
private void HandleMqttServerAdded(MqttServerDto mqttServer)
private void HandleMqttServerAdded(MqttServer mqttServer)
{
if (mqttServer == null)
{
@@ -379,11 +375,8 @@ namespace DMS.Infrastructure.Services.Mqtt
_logger.LogInformation("处理MQTT服务器添加事件: {MqttServerId} ({MqttServerName})",
mqttServer.Id, mqttServer.ServerName);
// 将DTO转换为MqttServer实体
var mqttServerEntity = _mapper.Map<MqttServer>(mqttServer);
// 添加服务器到监控列表
AddMqttServer(mqttServerEntity);
AddMqttServer(mqttServer);
// 如果服务器是激活状态,则尝试连接
if (mqttServer.IsActive)
@@ -413,7 +406,7 @@ namespace DMS.Infrastructure.Services.Mqtt
/// <summary>
/// 处理MQTT服务器更新事件
/// </summary>
private async Task HandleMqttServerUpdated(MqttServerDto mqttServer, MqttServerPropertyType propertyType)
private async Task HandleMqttServerUpdated(MqttServer mqttServer, MqttServerPropertyType propertyType)
{
if (mqttServer == null)
{

View File

@@ -24,8 +24,7 @@ namespace DMS.WPF.Profiles
CreateMap<MenuBeanDto, MenuItem>()
.ReverseMap();
CreateMap<MqttServerDto, MqttServerItem>().ReverseMap();
CreateMap<MqttServerItem, MqttServerItem>().ReverseMap();
CreateMap<MqttServer, MqttServerItem>().ReverseMap();
CreateMap<UserDto, UserItem>().ReverseMap();

View File

@@ -1,11 +1,13 @@
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Management;
using DMS.Core.Enums;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels;
using DMS.WPF.ItemViewModel;
using DMS.Core.Models;
using DMS.Application.DTOs;
namespace DMS.WPF.Services;
@@ -45,9 +47,9 @@ public class MqttDataService : IMqttDataService
try
{
// 加载MQTT服务器数据
foreach (var mqttServerDto in _appDataStorageService.MqttServers.Values)
foreach (var mqttServer in _appDataStorageService.MqttServers.Values)
{
_dataStorageService.MqttServers.TryAdd(mqttServerDto.Id, _mapper.Map<MqttServerItem>(mqttServerDto));
_dataStorageService.MqttServers.TryAdd(mqttServer.Id, _mapper.Map<MqttServerItem>(mqttServer));
}
}
@@ -65,9 +67,9 @@ public class MqttDataService : IMqttDataService
public async Task<MqttServerItem> AddMqttServer(MqttServerItem mqttServer)
{
var addMqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map<MqttServerDto>(mqttServer));
var addMqttServer = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map<MqttServer>(mqttServer));
MqttServerItem mqttServerItem = _mapper.Map<MqttServerItem>(addMqttServerDto);
MqttServerItem mqttServerItem = _mapper.Map<MqttServerItem>(addMqttServer);
_dataStorageService.MqttServers.Add(mqttServerItem.Id, mqttServerItem);
@@ -96,8 +98,8 @@ public class MqttDataService : IMqttDataService
/// </summary>
public async Task<bool> UpdateMqttServer(MqttServerItem mqttServer)
{
var dto = _mapper.Map<MqttServerDto>(mqttServer);
var result = await _mqttManagementService.UpdateMqttServerAsync(dto);
var mqttServerModel = _mapper.Map<DMS.Core.Models.MqttServer>(mqttServer);
var result = await _mqttManagementService.UpdateMqttServerAsync(mqttServerModel);
if (result > 0)
{

View File

@@ -1,6 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.WPF.ItemViewModel;
using System;
@@ -36,27 +36,27 @@ namespace DMS.WPF.ViewModels.Dialogs
{
try
{
var mqttServerDtos = await _mqttAppService.GetAllMqttServersAsync();
var mqttServers = await _mqttAppService.GetAllMqttServersAsync();
MqttServers.Clear();
foreach (var dto in mqttServerDtos)
foreach (var mqttServer in mqttServers)
{
MqttServers.Add(new MqttServerItem
{
Id = dto.Id,
ServerName = dto.ServerName,
ServerUrl = dto.ServerUrl,
Port = dto.Port,
Username = dto.Username,
Password = dto.Password,
IsActive = dto.IsActive,
SubscribeTopic = dto.SubscribeTopic,
PublishTopic = dto.PublishTopic,
ClientId = dto.ClientId,
CreatedAt = dto.CreatedAt,
ConnectedAt = dto.ConnectedAt,
ConnectionDuration = dto.ConnectionDuration,
MessageFormat = dto.MessageFormat
Id = mqttServer.Id,
ServerName = mqttServer.ServerName,
ServerUrl = mqttServer.ServerUrl,
Port = mqttServer.Port,
Username = mqttServer.Username,
Password = mqttServer.Password,
IsActive = mqttServer.IsActive,
SubscribeTopic = mqttServer.SubscribeTopic,
PublishTopic = mqttServer.PublishTopic,
ClientId = mqttServer.ClientId,
CreatedAt = mqttServer.CreatedAt,
ConnectedAt = mqttServer.ConnectedAt,
ConnectionDuration = mqttServer.ConnectionDuration,
MessageFormat = mqttServer.MessageFormat
});
}
}