diff --git a/DMS.Application/DTOs/CreateMqttWithMenuDto.cs b/DMS.Application/DTOs/CreateMqttWithMenuDto.cs index 8cfd07b..77dc132 100644 --- a/DMS.Application/DTOs/CreateMqttWithMenuDto.cs +++ b/DMS.Application/DTOs/CreateMqttWithMenuDto.cs @@ -1,4 +1,4 @@ -using DMS.Application.DTOs; +using DMS.Core.Models; namespace DMS.Application.DTOs { @@ -10,7 +10,7 @@ namespace DMS.Application.DTOs /// /// MQTT服务器信息 /// - public MqttServerDto MqttServer { get; set; } + public MqttServer MqttServer { get; set; } /// /// 菜单项信息 diff --git a/DMS.Application/DTOs/MqttServerDto.cs b/DMS.Application/DTOs/MqttServerDto.cs deleted file mode 100644 index 3816159..0000000 --- a/DMS.Application/DTOs/MqttServerDto.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using DMS.Core.Models; - -namespace DMS.Application.DTOs; - -/// -/// 用于在UI上显示MQTT服务器配置信息的DTO。 -/// -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 VariableAliases { get; set; } = new(); -} \ No newline at end of file diff --git a/DMS.Application/Events/MqttServerChangedEventArgs.cs b/DMS.Application/Events/MqttServerChangedEventArgs.cs index 582aa20..29d8e61 100644 --- a/DMS.Application/Events/MqttServerChangedEventArgs.cs +++ b/DMS.Application/Events/MqttServerChangedEventArgs.cs @@ -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; } /// - /// MQTT服务器DTO + /// MQTT服务器 /// - public MqttServerDto MqttServer { get; } + public MqttServer MqttServer { get; } /// /// 发生变化的属性类型 @@ -28,9 +28,9 @@ namespace DMS.Application.Events /// 构造函数 /// /// 变更类型 - /// MQTT服务器DTO + /// MQTT服务器 /// 发生变化的属性类型 - public MqttServerChangedEventArgs(ActionChangeType changeType, MqttServerDto mqttServer, MqttServerPropertyType propertyType = MqttServerPropertyType.All) + public MqttServerChangedEventArgs(ActionChangeType changeType, MqttServer mqttServer, MqttServerPropertyType propertyType = MqttServerPropertyType.All) { ChangeType = changeType; MqttServer = mqttServer; diff --git a/DMS.Application/Interfaces/Database/IMqttAppService.cs b/DMS.Application/Interfaces/Database/IMqttAppService.cs index c07c7ef..fb11471 100644 --- a/DMS.Application/Interfaces/Database/IMqttAppService.cs +++ b/DMS.Application/Interfaces/Database/IMqttAppService.cs @@ -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 { /// - /// 异步根据ID获取MQTT服务器DTO。 + /// 异步根据ID获取MQTT服务器。 /// - Task GetMqttServerByIdAsync(int id); + Task GetMqttServerByIdAsync(int id); /// - /// 异步获取所有MQTT服务器DTO列表。 + /// 异步获取所有MQTT服务器列表。 /// - Task> GetAllMqttServersAsync(); + Task> GetAllMqttServersAsync(); /// /// 异步创建一个新的MQTT服务器。 /// - Task CreateMqttServerAsync(MqttServerDto mqttServerDto); + Task CreateMqttServerAsync(MqttServer mqttServer); /// /// 异步更新一个已存在的MQTT服务器。 /// - Task UpdateMqttServerAsync(MqttServerDto mqttServerDto); + Task UpdateMqttServerAsync(MqttServer mqttServer); /// /// 异步批量更新MQTT服务器。 /// - Task UpdateMqttServersAsync(List mqttServerDtos); + Task UpdateMqttServersAsync(List mqttServers); /// /// 异步根据ID删除一个MQTT服务器。 diff --git a/DMS.Application/Interfaces/IAppDataStorageService.cs b/DMS.Application/Interfaces/IAppDataStorageService.cs index 5e9ff18..535819c 100644 --- a/DMS.Application/Interfaces/IAppDataStorageService.cs +++ b/DMS.Application/Interfaces/IAppDataStorageService.cs @@ -34,7 +34,7 @@ public interface IAppDataStorageService /// /// 安全字典,用于存储所有MQTT服务器数据 /// - ConcurrentDictionary MqttServers { get; } + ConcurrentDictionary MqttServers { get; } /// /// 安全字典,用于存储所有日志数据 diff --git a/DMS.Application/Interfaces/Management/IMqttManagementService.cs b/DMS.Application/Interfaces/Management/IMqttManagementService.cs index 74b6783..76e59f3 100644 --- a/DMS.Application/Interfaces/Management/IMqttManagementService.cs +++ b/DMS.Application/Interfaces/Management/IMqttManagementService.cs @@ -1,33 +1,10 @@ -using DMS.Application.DTOs; +using DMS.Core.Models; namespace DMS.Application.Interfaces.Management; public interface IMqttManagementService { - /// - /// 异步根据ID获取MQTT服务器DTO。 - /// - Task GetMqttServerByIdAsync(int id); - - /// - /// 异步获取所有MQTT服务器DTO列表。 - /// - Task> GetAllMqttServersAsync(); - - /// - /// 异步创建一个新的MQTT服务器。 - /// - Task CreateMqttServerAsync(MqttServerDto mqttServerDto); - - /// - /// 异步更新一个已存在的MQTT服务器。 - /// - Task UpdateMqttServerAsync(MqttServerDto mqttServerDto); - - /// - /// 异步批量更新MQTT服务器。 - /// - Task UpdateMqttServersAsync(List mqttServerDtos); + Task CreateMqttServerAsync(MqttServer mqttServer); /// /// 异步删除一个MQTT服务器。 @@ -38,5 +15,8 @@ public interface IMqttManagementService /// 异步批量删除MQTT服务器。 /// Task DeleteMqttServersAsync(List ids); - + Task> GetAllMqttServersAsync(); + Task GetMqttServerByIdAsync(int id); + Task UpdateMqttServerAsync(MqttServer mqttServer); + Task UpdateMqttServersAsync(List mqttServers); } \ No newline at end of file diff --git a/DMS.Application/Profiles/MappingProfile.cs b/DMS.Application/Profiles/MappingProfile.cs index 81a8022..b8c983d 100644 --- a/DMS.Application/Profiles/MappingProfile.cs +++ b/DMS.Application/Profiles/MappingProfile.cs @@ -27,9 +27,7 @@ public class MappingProfile : Profile CreateMap() .ReverseMap(); - // MqttServer 映射 - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + // VariableHistory 映射 CreateMap() diff --git a/DMS.Application/Services/AppDataStorageService.cs b/DMS.Application/Services/AppDataStorageService.cs index 65cc8b8..4c7a64f 100644 --- a/DMS.Application/Services/AppDataStorageService.cs +++ b/DMS.Application/Services/AppDataStorageService.cs @@ -35,7 +35,7 @@ public class AppDataStorageService : IAppDataStorageService /// /// 安全字典,用于存储所有MQTT服务器数据 /// - public ConcurrentDictionary MqttServers { get; } = new(); + public ConcurrentDictionary MqttServers { get; } = new(); /// diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index a27745f..3839249 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -118,7 +118,7 @@ public class DataLoaderService : IDataLoaderService if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer)) { - variableMqttAlias.MqttServer = _mapper.Map(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); } } diff --git a/DMS.Application/Services/Database/MqttAppService.cs b/DMS.Application/Services/Database/MqttAppService.cs index 1a4dfa4..4f1cfda 100644 --- a/DMS.Application/Services/Database/MqttAppService.cs +++ b/DMS.Application/Services/Database/MqttAppService.cs @@ -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; /// - /// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。 + /// 构造函数,通过依赖注入获取仓储管理器实例。 /// /// 仓储管理器实例。 - /// AutoMapper 实例。 - public MqttAppService(IRepositoryManager repoManager, IMapper mapper) + public MqttAppService(IRepositoryManager repoManager) { _repoManager = repoManager; - _mapper = mapper; } /// - /// 异步根据ID获取MQTT服务器数据传输对象。 + /// 异步根据ID获取MQTT服务器。 /// /// MQTT服务器ID。 - /// MQTT服务器数据传输对象。 - public async Task GetMqttServerByIdAsync(int id) + /// MQTT服务器。 + public async Task GetMqttServerByIdAsync(int id) { - var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id); - return _mapper.Map(mqttServer); + return await _repoManager.MqttServers.GetByIdAsync(id); } /// - /// 异步获取所有MQTT服务器数据传输对象列表。 + /// 异步获取所有MQTT服务器列表。 /// - /// MQTT服务器数据传输对象列表。 - public async Task> GetAllMqttServersAsync() + /// MQTT服务器列表。 + public async Task> GetAllMqttServersAsync() { var mqttServers = await _repoManager.MqttServers.GetAllAsync(); - return _mapper.Map>(mqttServers); + return mqttServers.ToList(); } /// /// 异步创建一个新MQTT服务器(事务性操作)。 /// - /// 要创建的MQTT服务器数据传输对象。 + /// 要创建的MQTT服务器。 /// 新创建MQTT服务器的ID。 /// 如果创建MQTT服务器时发生错误。 - public async Task CreateMqttServerAsync(MqttServerDto mqttServerDto) + public async Task CreateMqttServerAsync(MqttServer mqttServer) { try { await _repoManager.BeginTranAsync(); - var mqttServer = _mapper.Map(mqttServerDto); await _repoManager.MqttServers.AddAsync(mqttServer); await _repoManager.CommitAsync(); return mqttServer.Id; @@ -74,21 +66,36 @@ public class MqttAppService : IMqttAppService /// /// 异步更新一个已存在的MQTT服务器(事务性操作)。 /// - /// 要更新的MQTT服务器数据传输对象。 + /// 要更新的MQTT服务器。 /// 表示异步操作的任务。 /// 如果找不到MQTT服务器或更新MQTT服务器时发生错误。 - 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 /// /// 异步批量更新MQTT服务器(事务性操作)。 /// - /// 要更新的MQTT服务器数据传输对象列表。 + /// 要更新的MQTT服务器列表。 /// 成功更新的MQTT服务器数量。 /// 如果批量更新MQTT服务器时发生错误。 - public async Task UpdateMqttServersAsync(List mqttServerDtos) + public async Task UpdateMqttServersAsync(List 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++; } } diff --git a/DMS.Application/Services/Management/MqttManagementService.cs b/DMS.Application/Services/Management/MqttManagementService.cs index 199095e..027eb2f 100644 --- a/DMS.Application/Services/Management/MqttManagementService.cs +++ b/DMS.Application/Services/Management/MqttManagementService.cs @@ -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; } /// /// 异步根据ID获取MQTT服务器DTO。 /// - public async Task GetMqttServerByIdAsync(int id) + public async Task GetMqttServerByIdAsync(int id) { - return await _mqttAppService.GetMqttServerByIdAsync(id); + if (_appDataStorageService.MqttServers.TryGetValue(id,out var mqttServer)) + { + return mqttServer; + } + return null; } /// /// 异步获取所有MQTT服务器DTO列表。 /// - public async Task> GetAllMqttServersAsync() + public async Task> GetAllMqttServersAsync() { - return await _mqttAppService.GetAllMqttServersAsync(); + return _appDataStorageService.MqttServers.Values.ToList(); } /// /// 异步更新一个已存在的MQTT服务器。 /// - public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto) + public async Task UpdateMqttServerAsync(MqttServer mqttServer) { - return await UpdateMqttServersAsync(new List() { mqttServerDto }); + return await UpdateMqttServersAsync(new List() { mqttServer }); } /// /// 异步批量更新MQTT服务器。 /// - public async Task UpdateMqttServersAsync(List mqttServerDtos) + public async Task UpdateMqttServersAsync(List 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 /// /// 异步创建MQTT服务器及其菜单项。 /// - public async Task CreateMqttServerAsync(MqttServerDto mqttServerDto) + public async Task 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表示创建失败 } /// @@ -177,7 +185,7 @@ public class MqttManagementService : IMqttManagementService /// 旧MQTT服务器值 /// 新MQTT服务器值 /// 发生变化的属性列表 - private List GetChangedProperties(MqttServerDto oldMqttServer, MqttServerDto newMqttServer) + private List GetChangedProperties(MqttServer oldMqttServer, MqttServer newMqttServer) { var changedProperties = new List(); diff --git a/DMS.Infrastructure/DMS.Infrastructure.csproj b/DMS.Infrastructure/DMS.Infrastructure.csproj index fda924a..9a2b1e5 100644 --- a/DMS.Infrastructure/DMS.Infrastructure.csproj +++ b/DMS.Infrastructure/DMS.Infrastructure.csproj @@ -6,6 +6,12 @@ enable + + + + + + @@ -26,8 +32,4 @@ - - - - diff --git a/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs index 2665a78..cff9dd5 100644 --- a/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs @@ -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) diff --git a/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs b/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs index 9280f04..addcea6 100644 --- a/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs @@ -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 _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(); _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(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(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(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 /// /// 处理MQTT服务器添加事件 /// - 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); - // 添加服务器到监控列表 - AddMqttServer(mqttServerEntity); + AddMqttServer(mqttServer); // 如果服务器是激活状态,则尝试连接 if (mqttServer.IsActive) @@ -413,7 +406,7 @@ namespace DMS.Infrastructure.Services.Mqtt /// /// 处理MQTT服务器更新事件 /// - private async Task HandleMqttServerUpdated(MqttServerDto mqttServer, MqttServerPropertyType propertyType) + private async Task HandleMqttServerUpdated(MqttServer mqttServer, MqttServerPropertyType propertyType) { if (mqttServer == null) { diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 84dfe8b..e202d03 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -24,8 +24,7 @@ namespace DMS.WPF.Profiles CreateMap() .ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + CreateMap().ReverseMap(); CreateMap().ReverseMap(); diff --git a/DMS.WPF/Services/MqttDataService.cs b/DMS.WPF/Services/MqttDataService.cs index 753bd38..7502f56 100644 --- a/DMS.WPF/Services/MqttDataService.cs +++ b/DMS.WPF/Services/MqttDataService.cs @@ -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(mqttServerDto)); + _dataStorageService.MqttServers.TryAdd(mqttServer.Id, _mapper.Map(mqttServer)); } } @@ -65,9 +67,9 @@ public class MqttDataService : IMqttDataService public async Task AddMqttServer(MqttServerItem mqttServer) { - var addMqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map(mqttServer)); + var addMqttServer = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map(mqttServer)); - MqttServerItem mqttServerItem = _mapper.Map(addMqttServerDto); + MqttServerItem mqttServerItem = _mapper.Map(addMqttServer); _dataStorageService.MqttServers.Add(mqttServerItem.Id, mqttServerItem); @@ -96,8 +98,8 @@ public class MqttDataService : IMqttDataService /// public async Task UpdateMqttServer(MqttServerItem mqttServer) { - var dto = _mapper.Map(mqttServer); - var result = await _mqttManagementService.UpdateMqttServerAsync(dto); + var mqttServerModel = _mapper.Map(mqttServer); + var result = await _mqttManagementService.UpdateMqttServerAsync(mqttServerModel); if (result > 0) { diff --git a/DMS.WPF/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs index f5f7ac4..d72e71d 100644 --- a/DMS.WPF/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/MqttSelectionDialogViewModel.cs @@ -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 }); } }