refactor:删除MqttServerDto,将使用MqttServerDto的全部转为使用MqttServer
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.DTOs
|
namespace DMS.Application.DTOs
|
||||||
{
|
{
|
||||||
@@ -10,7 +10,7 @@ namespace DMS.Application.DTOs
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// MQTT服务器信息
|
/// MQTT服务器信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MqttServerDto MqttServer { get; set; }
|
public MqttServer MqttServer { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 菜单项信息
|
/// 菜单项信息
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.Events
|
namespace DMS.Application.Events
|
||||||
{
|
{
|
||||||
@@ -14,9 +14,9 @@ namespace DMS.Application.Events
|
|||||||
public ActionChangeType ChangeType { get; }
|
public ActionChangeType ChangeType { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MQTT服务器DTO
|
/// MQTT服务器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MqttServerDto MqttServer { get; }
|
public MqttServer MqttServer { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发生变化的属性类型
|
/// 发生变化的属性类型
|
||||||
@@ -28,9 +28,9 @@ namespace DMS.Application.Events
|
|||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="changeType">变更类型</param>
|
/// <param name="changeType">变更类型</param>
|
||||||
/// <param name="mqttServer">MQTT服务器DTO</param>
|
/// <param name="mqttServer">MQTT服务器</param>
|
||||||
/// <param name="propertyType">发生变化的属性类型</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;
|
ChangeType = changeType;
|
||||||
MqttServer = mqttServer;
|
MqttServer = mqttServer;
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.Interfaces.Database;
|
namespace DMS.Application.Interfaces.Database;
|
||||||
@@ -9,29 +8,29 @@ namespace DMS.Application.Interfaces.Database;
|
|||||||
public interface IMqttAppService
|
public interface IMqttAppService
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID获取MQTT服务器DTO。
|
/// 异步根据ID获取MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<MqttServerDto> GetMqttServerByIdAsync(int id);
|
Task<MqttServer> GetMqttServerByIdAsync(int id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步获取所有MQTT服务器DTO列表。
|
/// 异步获取所有MQTT服务器列表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<List<MqttServerDto>> GetAllMqttServersAsync();
|
Task<List<MqttServer>> GetAllMqttServersAsync();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步创建一个新的MQTT服务器。
|
/// 异步创建一个新的MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto);
|
Task<int> CreateMqttServerAsync(MqttServer mqttServer);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步更新一个已存在的MQTT服务器。
|
/// 异步更新一个已存在的MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task UpdateMqttServerAsync(MqttServerDto mqttServerDto);
|
Task UpdateMqttServerAsync(MqttServer mqttServer);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步批量更新MQTT服务器。
|
/// 异步批量更新MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos);
|
Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID删除一个MQTT服务器。
|
/// 异步根据ID删除一个MQTT服务器。
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public interface IAppDataStorageService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 安全字典,用于存储所有MQTT服务器数据
|
/// 安全字典,用于存储所有MQTT服务器数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
ConcurrentDictionary<int, MqttServerDto> MqttServers { get; }
|
ConcurrentDictionary<int, MqttServer> MqttServers { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 安全字典,用于存储所有日志数据
|
/// 安全字典,用于存储所有日志数据
|
||||||
|
|||||||
@@ -1,33 +1,10 @@
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.Interfaces.Management;
|
namespace DMS.Application.Interfaces.Management;
|
||||||
|
|
||||||
public interface IMqttManagementService
|
public interface IMqttManagementService
|
||||||
{
|
{
|
||||||
/// <summary>
|
Task<MqttServer> CreateMqttServerAsync(MqttServer mqttServer);
|
||||||
/// 异步根据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);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步删除一个MQTT服务器。
|
/// 异步删除一个MQTT服务器。
|
||||||
@@ -38,5 +15,8 @@ public interface IMqttManagementService
|
|||||||
/// 异步批量删除MQTT服务器。
|
/// 异步批量删除MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<bool> DeleteMqttServersAsync(List<int> ids);
|
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);
|
||||||
}
|
}
|
||||||
@@ -27,9 +27,7 @@ public class MappingProfile : Profile
|
|||||||
CreateMap<VariableDto, VariableDto>()
|
CreateMap<VariableDto, VariableDto>()
|
||||||
.ReverseMap();
|
.ReverseMap();
|
||||||
|
|
||||||
// MqttServer 映射
|
|
||||||
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
|
|
||||||
CreateMap<MqttServerDto, MqttServerDto>().ReverseMap();
|
|
||||||
|
|
||||||
// VariableHistory 映射
|
// VariableHistory 映射
|
||||||
CreateMap<VariableHistory, VariableHistoryDto>()
|
CreateMap<VariableHistory, VariableHistoryDto>()
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public class AppDataStorageService : IAppDataStorageService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 安全字典,用于存储所有MQTT服务器数据
|
/// 安全字典,用于存储所有MQTT服务器数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ConcurrentDictionary<int, MqttServerDto> MqttServers { get; } = new();
|
public ConcurrentDictionary<int, MqttServer> MqttServers { get; } = new();
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ public class DataLoaderService : IDataLoaderService
|
|||||||
|
|
||||||
if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer))
|
if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer))
|
||||||
{
|
{
|
||||||
variableMqttAlias.MqttServer = _mapper.Map<MqttServer>(mqttServer);
|
variableMqttAlias.MqttServer = mqttServer;
|
||||||
mqttServer.VariableAliases?.Add(variableMqttAlias);
|
mqttServer.VariableAliases?.Add(variableMqttAlias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,11 +209,11 @@ public class DataLoaderService : IDataLoaderService
|
|||||||
public async Task LoadAllMqttServersAsync()
|
public async Task LoadAllMqttServersAsync()
|
||||||
{
|
{
|
||||||
_appDataStorageService.MqttServers.Clear();
|
_appDataStorageService.MqttServers.Clear();
|
||||||
var mqttServerDtos =await _mqttAppService.GetAllMqttServersAsync();
|
var mqttServers =await _mqttAppService.GetAllMqttServersAsync();
|
||||||
// 加载MQTT服务器数据到内存
|
// 加载MQTT服务器数据到内存
|
||||||
foreach (var mqttServerDto in mqttServerDtos)
|
foreach (var mqttServer in mqttServers)
|
||||||
{
|
{
|
||||||
_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto);
|
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
using AutoMapper;
|
|
||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Application.Interfaces;
|
|
||||||
using DMS.Application.Interfaces.Database;
|
using DMS.Application.Interfaces.Database;
|
||||||
using DMS.Core.Interfaces;
|
using DMS.Core.Interfaces;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
@@ -14,52 +11,47 @@ namespace DMS.Application.Services.Database;
|
|||||||
public class MqttAppService : IMqttAppService
|
public class MqttAppService : IMqttAppService
|
||||||
{
|
{
|
||||||
private readonly IRepositoryManager _repoManager;
|
private readonly IRepositoryManager _repoManager;
|
||||||
private readonly IMapper _mapper;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
/// 构造函数,通过依赖注入获取仓储管理器实例。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repoManager">仓储管理器实例。</param>
|
/// <param name="repoManager">仓储管理器实例。</param>
|
||||||
/// <param name="mapper">AutoMapper 实例。</param>
|
public MqttAppService(IRepositoryManager repoManager)
|
||||||
public MqttAppService(IRepositoryManager repoManager, IMapper mapper)
|
|
||||||
{
|
{
|
||||||
_repoManager = repoManager;
|
_repoManager = repoManager;
|
||||||
_mapper = mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID获取MQTT服务器数据传输对象。
|
/// 异步根据ID获取MQTT服务器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">MQTT服务器ID。</param>
|
/// <param name="id">MQTT服务器ID。</param>
|
||||||
/// <returns>MQTT服务器数据传输对象。</returns>
|
/// <returns>MQTT服务器。</returns>
|
||||||
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
|
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
|
return await _repoManager.MqttServers.GetByIdAsync(id);
|
||||||
return _mapper.Map<MqttServerDto>(mqttServer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步获取所有MQTT服务器数据传输对象列表。
|
/// 异步获取所有MQTT服务器列表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>MQTT服务器数据传输对象列表。</returns>
|
/// <returns>MQTT服务器列表。</returns>
|
||||||
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
|
public async Task<List<MqttServer>> GetAllMqttServersAsync()
|
||||||
{
|
{
|
||||||
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
|
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
|
||||||
return _mapper.Map<List<MqttServerDto>>(mqttServers);
|
return mqttServers.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步创建一个新MQTT服务器(事务性操作)。
|
/// 异步创建一个新MQTT服务器(事务性操作)。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mqttServerDto">要创建的MQTT服务器数据传输对象。</param>
|
/// <param name="mqttServer">要创建的MQTT服务器。</param>
|
||||||
/// <returns>新创建MQTT服务器的ID。</returns>
|
/// <returns>新创建MQTT服务器的ID。</returns>
|
||||||
/// <exception cref="ApplicationException">如果创建MQTT服务器时发生错误。</exception>
|
/// <exception cref="ApplicationException">如果创建MQTT服务器时发生错误。</exception>
|
||||||
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
public async Task<int> CreateMqttServerAsync(MqttServer mqttServer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _repoManager.BeginTranAsync();
|
await _repoManager.BeginTranAsync();
|
||||||
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
|
|
||||||
await _repoManager.MqttServers.AddAsync(mqttServer);
|
await _repoManager.MqttServers.AddAsync(mqttServer);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
return mqttServer.Id;
|
return mqttServer.Id;
|
||||||
@@ -74,21 +66,36 @@ public class MqttAppService : IMqttAppService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步更新一个已存在的MQTT服务器(事务性操作)。
|
/// 异步更新一个已存在的MQTT服务器(事务性操作)。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mqttServerDto">要更新的MQTT服务器数据传输对象。</param>
|
/// <param name="mqttServer">要更新的MQTT服务器。</param>
|
||||||
/// <returns>表示异步操作的任务。</returns>
|
/// <returns>表示异步操作的任务。</returns>
|
||||||
/// <exception cref="ApplicationException">如果找不到MQTT服务器或更新MQTT服务器时发生错误。</exception>
|
/// <exception cref="ApplicationException">如果找不到MQTT服务器或更新MQTT服务器时发生错误。</exception>
|
||||||
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
public async Task UpdateMqttServerAsync(MqttServer mqttServer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _repoManager.BeginTranAsync();
|
await _repoManager.BeginTranAsync();
|
||||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
var existingMqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServer.Id);
|
||||||
if (mqttServer == null)
|
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();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -121,22 +128,36 @@ public class MqttAppService : IMqttAppService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步批量更新MQTT服务器(事务性操作)。
|
/// 异步批量更新MQTT服务器(事务性操作)。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mqttServerDtos">要更新的MQTT服务器数据传输对象列表。</param>
|
/// <param name="mqttServers">要更新的MQTT服务器列表。</param>
|
||||||
/// <returns>成功更新的MQTT服务器数量。</returns>
|
/// <returns>成功更新的MQTT服务器数量。</returns>
|
||||||
/// <exception cref="ApplicationException">如果批量更新MQTT服务器时发生错误。</exception>
|
/// <exception cref="ApplicationException">如果批量更新MQTT服务器时发生错误。</exception>
|
||||||
public async Task<int> UpdateMqttServersAsync(List<MqttServerDto> mqttServerDtos)
|
public async Task<int> UpdateMqttServersAsync(List<MqttServer> mqttServers)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _repoManager.BeginTranAsync();
|
await _repoManager.BeginTranAsync();
|
||||||
var count = 0;
|
var count = 0;
|
||||||
foreach (var mqttServerDto in mqttServerDtos)
|
foreach (var mqttServer in mqttServers)
|
||||||
{
|
{
|
||||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
var existingMqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServer.Id);
|
||||||
if (mqttServer != null)
|
if (existingMqttServer != null)
|
||||||
{
|
{
|
||||||
_mapper.Map(mqttServerDto, mqttServer);
|
existingMqttServer.ServerName = mqttServer.ServerName;
|
||||||
await _repoManager.MqttServers.UpdateAsync(mqttServer);
|
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++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using AutoMapper;
|
|
||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Application.Events;
|
using DMS.Application.Events;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Application.Interfaces.Database;
|
using DMS.Application.Interfaces.Database;
|
||||||
using DMS.Application.Interfaces.Management;
|
using DMS.Application.Interfaces.Management;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.Services.Management;
|
namespace DMS.Application.Services.Management;
|
||||||
|
|
||||||
@@ -17,83 +17,91 @@ public class MqttManagementService : IMqttManagementService
|
|||||||
private readonly IMqttAppService _mqttAppService;
|
private readonly IMqttAppService _mqttAppService;
|
||||||
private readonly IAppDataStorageService _appDataStorageService;
|
private readonly IAppDataStorageService _appDataStorageService;
|
||||||
private readonly IEventService _eventService;
|
private readonly IEventService _eventService;
|
||||||
private readonly IMapper _mapper;
|
|
||||||
private readonly IDataProcessingService _dataProcessingService;
|
|
||||||
private readonly IMenuManagementService _menuManagementService;
|
|
||||||
|
|
||||||
public MqttManagementService(IMqttAppService mqttAppService,
|
public MqttManagementService(IMqttAppService mqttAppService,
|
||||||
IAppDataStorageService appDataStorageService,
|
IAppDataStorageService appDataStorageService,
|
||||||
IEventService eventService,
|
IEventService eventService)
|
||||||
IMapper mapper,
|
|
||||||
IDataProcessingService dataProcessingService,
|
|
||||||
IMenuManagementService menuManagementService)
|
|
||||||
{
|
{
|
||||||
_mqttAppService = mqttAppService;
|
_mqttAppService = mqttAppService;
|
||||||
_appDataStorageService = appDataStorageService;
|
_appDataStorageService = appDataStorageService;
|
||||||
_eventService = eventService;
|
_eventService = eventService;
|
||||||
_mapper = mapper;
|
|
||||||
_dataProcessingService = dataProcessingService;
|
|
||||||
_menuManagementService = menuManagementService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID获取MQTT服务器DTO。
|
/// 异步根据ID获取MQTT服务器DTO。
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// 异步获取所有MQTT服务器DTO列表。
|
/// 异步获取所有MQTT服务器DTO列表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
|
public async Task<List<MqttServer>> GetAllMqttServersAsync()
|
||||||
{
|
{
|
||||||
return await _mqttAppService.GetAllMqttServersAsync();
|
return _appDataStorageService.MqttServers.Values.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步更新一个已存在的MQTT服务器。
|
/// 异步更新一个已存在的MQTT服务器。
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// 异步批量更新MQTT服务器。
|
/// 异步批量更新MQTT服务器。
|
||||||
/// </summary>
|
/// </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服务器
|
// 批量更新成功后,更新内存中的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服务器
|
// 更新内存中的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)
|
foreach (var property in changedProperties)
|
||||||
{
|
{
|
||||||
_eventService.RaiseMqttServerChanged(
|
_eventService.RaiseMqttServerChanged(
|
||||||
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, property));
|
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServer, property));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 如果内存中不存在该MQTT服务器,则直接添加
|
// 如果内存中不存在该MQTT服务器,则直接添加
|
||||||
_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto);
|
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
||||||
_eventService.RaiseMqttServerChanged(
|
_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服务器
|
// 删除成功后,从内存中移除MQTT服务器
|
||||||
if (result && mqttServer != null)
|
if (result && mqttServer != null)
|
||||||
{
|
{
|
||||||
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
|
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
|
||||||
{
|
{
|
||||||
_eventService.RaiseMqttServerChanged(
|
_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)
|
foreach (var id in ids)
|
||||||
{
|
{
|
||||||
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
|
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServer))
|
||||||
{
|
{
|
||||||
_eventService.RaiseMqttServerChanged(
|
_eventService.RaiseMqttServerChanged(
|
||||||
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
|
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,27 +156,27 @@ public class MqttManagementService : IMqttManagementService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步创建MQTT服务器及其菜单项。
|
/// 异步创建MQTT服务器及其菜单项。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<MqttServerDto> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
public async Task<MqttServer> CreateMqttServerAsync(MqttServer mqttServer)
|
||||||
{
|
{
|
||||||
// 首先创建MQTT服务器
|
// 首先创建MQTT服务器
|
||||||
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
|
var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServer);
|
||||||
|
|
||||||
if (mqttServerId > 0)
|
if (mqttServerId > 0)
|
||||||
{
|
{
|
||||||
mqttServerDto.Id = mqttServerId;
|
mqttServer.Id = mqttServerId;
|
||||||
|
|
||||||
|
|
||||||
// 将MQTT服务器添加到内存中
|
// 将MQTT服务器添加到内存中
|
||||||
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
|
if (_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
|
||||||
{
|
{
|
||||||
_eventService.RaiseMqttServerChanged(
|
_eventService.RaiseMqttServerChanged(
|
||||||
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
|
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mqttServerDto; // 返回null表示创建失败
|
return mqttServer; // 返回null表示创建失败
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -177,7 +185,7 @@ public class MqttManagementService : IMqttManagementService
|
|||||||
/// <param name="oldMqttServer">旧MQTT服务器值</param>
|
/// <param name="oldMqttServer">旧MQTT服务器值</param>
|
||||||
/// <param name="newMqttServer">新MQTT服务器值</param>
|
/// <param name="newMqttServer">新MQTT服务器值</param>
|
||||||
/// <returns>发生变化的属性列表</returns>
|
/// <returns>发生变化的属性列表</returns>
|
||||||
private List<MqttServerPropertyType> GetChangedProperties(MqttServerDto oldMqttServer, MqttServerDto newMqttServer)
|
private List<MqttServerPropertyType> GetChangedProperties(MqttServer oldMqttServer, MqttServer newMqttServer)
|
||||||
{
|
{
|
||||||
var changedProperties = new List<MqttServerPropertyType>();
|
var changedProperties = new List<MqttServerPropertyType>();
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,12 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Configurations\**" />
|
||||||
|
<EmbeddedResource Remove="Configurations\**" />
|
||||||
|
<None Remove="Configurations\**" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MailKit" Version="4.7.1.1" />
|
<PackageReference Include="MailKit" Version="4.7.1.1" />
|
||||||
<PackageReference Include="MQTTnet" Version="3.0.16" />
|
<PackageReference Include="MQTTnet" Version="3.0.16" />
|
||||||
@@ -26,8 +32,4 @@
|
|||||||
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
|
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Configurations\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -186,34 +186,15 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_mqttServers.Clear();
|
_mqttServers.Clear();
|
||||||
|
|
||||||
// 从数据服务中心获取所有激活的MQTT服务器
|
// 从数据服务中心获取所有激活的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);
|
_mqttServers.TryAdd(mqttServer.Id, mqttServer);
|
||||||
_mqttServiceManager.AddMqttServer(mqttServer);
|
_mqttServiceManager.AddMqttServer(mqttServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation($"成功加载 {mqttServerDtos.Count} 个MQTT配置");
|
_logger.LogInformation($"成功加载 {mqttServers.Count} 个MQTT配置");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using AutoMapper;
|
|
||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Application.Events;
|
using DMS.Application.Events;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
@@ -23,7 +22,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
private readonly IAppDataCenterService _appDataCenterService;
|
private readonly IAppDataCenterService _appDataCenterService;
|
||||||
private readonly IMqttServiceFactory _mqttServiceFactory;
|
private readonly IMqttServiceFactory _mqttServiceFactory;
|
||||||
private readonly IEventService _eventService;
|
private readonly IEventService _eventService;
|
||||||
private readonly IMapper _mapper;
|
|
||||||
private readonly ConcurrentDictionary<int, MqttDeviceContext> _mqttContexts;
|
private readonly ConcurrentDictionary<int, MqttDeviceContext> _mqttContexts;
|
||||||
private readonly SemaphoreSlim _semaphore;
|
private readonly SemaphoreSlim _semaphore;
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
@@ -33,15 +32,13 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
IDataProcessingService dataProcessingService,
|
IDataProcessingService dataProcessingService,
|
||||||
IAppDataCenterService appDataCenterService,
|
IAppDataCenterService appDataCenterService,
|
||||||
IMqttServiceFactory mqttServiceFactory,
|
IMqttServiceFactory mqttServiceFactory,
|
||||||
IEventService eventService,
|
IEventService eventService)
|
||||||
IMapper mapper)
|
|
||||||
{
|
{
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
||||||
_appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
_appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
||||||
_mqttServiceFactory = mqttServiceFactory ?? throw new ArgumentNullException(nameof(mqttServiceFactory));
|
_mqttServiceFactory = mqttServiceFactory ?? throw new ArgumentNullException(nameof(mqttServiceFactory));
|
||||||
_eventService = eventService ?? throw new ArgumentNullException(nameof(eventService));
|
_eventService = eventService ?? throw new ArgumentNullException(nameof(eventService));
|
||||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
|
||||||
_mqttContexts = new ConcurrentDictionary<int, MqttDeviceContext>();
|
_mqttContexts = new ConcurrentDictionary<int, MqttDeviceContext>();
|
||||||
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
|
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
|
||||||
|
|
||||||
@@ -187,8 +184,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
context.MqttServerConfig.IsConnect = false;
|
context.MqttServerConfig.IsConnect = false;
|
||||||
_logger.LogWarning("MQTT服务器 {ServerName} 连接失败", context.MqttServerConfig.ServerName);
|
_logger.LogWarning("MQTT服务器 {ServerName} 连接失败", context.MqttServerConfig.ServerName);
|
||||||
}
|
}
|
||||||
//触发MQTT连接状态改变事件
|
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, context.MqttServerConfig, MqttServerPropertyType.IsConnect));
|
||||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, _mapper.Map<MqttServerDto>(context.MqttServerConfig), MqttServerPropertyType.IsConnect));
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -196,7 +192,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
context.MqttServerConfig.ServerName, ex.Message);
|
context.MqttServerConfig.ServerName, ex.Message);
|
||||||
context.ReconnectAttempts++;
|
context.ReconnectAttempts++;
|
||||||
context.MqttServerConfig.IsConnect = false;
|
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
|
finally
|
||||||
{
|
{
|
||||||
@@ -220,7 +216,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
|
|
||||||
// 如果连接状态从连接变为断开,触发事件
|
// 如果连接状态从连接变为断开,触发事件
|
||||||
context.MqttServerConfig.IsConnect = false;
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -366,7 +362,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理MQTT服务器添加事件
|
/// 处理MQTT服务器添加事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void HandleMqttServerAdded(MqttServerDto mqttServer)
|
private void HandleMqttServerAdded(MqttServer mqttServer)
|
||||||
{
|
{
|
||||||
if (mqttServer == null)
|
if (mqttServer == null)
|
||||||
{
|
{
|
||||||
@@ -379,11 +375,8 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_logger.LogInformation("处理MQTT服务器添加事件: {MqttServerId} ({MqttServerName})",
|
_logger.LogInformation("处理MQTT服务器添加事件: {MqttServerId} ({MqttServerName})",
|
||||||
mqttServer.Id, mqttServer.ServerName);
|
mqttServer.Id, mqttServer.ServerName);
|
||||||
|
|
||||||
// 将DTO转换为MqttServer实体
|
|
||||||
var mqttServerEntity = _mapper.Map<MqttServer>(mqttServer);
|
|
||||||
|
|
||||||
// 添加服务器到监控列表
|
// 添加服务器到监控列表
|
||||||
AddMqttServer(mqttServerEntity);
|
AddMqttServer(mqttServer);
|
||||||
|
|
||||||
// 如果服务器是激活状态,则尝试连接
|
// 如果服务器是激活状态,则尝试连接
|
||||||
if (mqttServer.IsActive)
|
if (mqttServer.IsActive)
|
||||||
@@ -413,7 +406,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理MQTT服务器更新事件
|
/// 处理MQTT服务器更新事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task HandleMqttServerUpdated(MqttServerDto mqttServer, MqttServerPropertyType propertyType)
|
private async Task HandleMqttServerUpdated(MqttServer mqttServer, MqttServerPropertyType propertyType)
|
||||||
{
|
{
|
||||||
if (mqttServer == null)
|
if (mqttServer == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ namespace DMS.WPF.Profiles
|
|||||||
CreateMap<MenuBeanDto, MenuItem>()
|
CreateMap<MenuBeanDto, MenuItem>()
|
||||||
.ReverseMap();
|
.ReverseMap();
|
||||||
|
|
||||||
CreateMap<MqttServerDto, MqttServerItem>().ReverseMap();
|
|
||||||
CreateMap<MqttServerItem, MqttServerItem>().ReverseMap();
|
|
||||||
|
|
||||||
CreateMap<MqttServer, MqttServerItem>().ReverseMap();
|
CreateMap<MqttServer, MqttServerItem>().ReverseMap();
|
||||||
CreateMap<UserDto, UserItem>().ReverseMap();
|
CreateMap<UserDto, UserItem>().ReverseMap();
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Application.Interfaces.Management;
|
using DMS.Application.Interfaces.Management;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
using DMS.WPF.Interfaces;
|
using DMS.WPF.Interfaces;
|
||||||
using DMS.WPF.ViewModels;
|
using DMS.WPF.ViewModels;
|
||||||
using DMS.WPF.ItemViewModel;
|
using DMS.WPF.ItemViewModel;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
using DMS.Application.DTOs;
|
||||||
|
|
||||||
namespace DMS.WPF.Services;
|
namespace DMS.WPF.Services;
|
||||||
|
|
||||||
@@ -45,9 +47,9 @@ public class MqttDataService : IMqttDataService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 加载MQTT服务器数据
|
// 加载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)
|
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);
|
_dataStorageService.MqttServers.Add(mqttServerItem.Id, mqttServerItem);
|
||||||
|
|
||||||
@@ -96,8 +98,8 @@ public class MqttDataService : IMqttDataService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<bool> UpdateMqttServer(MqttServerItem mqttServer)
|
public async Task<bool> UpdateMqttServer(MqttServerItem mqttServer)
|
||||||
{
|
{
|
||||||
var dto = _mapper.Map<MqttServerDto>(mqttServer);
|
var mqttServerModel = _mapper.Map<DMS.Core.Models.MqttServer>(mqttServer);
|
||||||
var result = await _mqttManagementService.UpdateMqttServerAsync(dto);
|
var result = await _mqttManagementService.UpdateMqttServerAsync(mqttServerModel);
|
||||||
|
|
||||||
if (result > 0)
|
if (result > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using DMS.Application.DTOs;
|
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.WPF.ItemViewModel;
|
using DMS.WPF.ItemViewModel;
|
||||||
using System;
|
using System;
|
||||||
@@ -36,27 +36,27 @@ namespace DMS.WPF.ViewModels.Dialogs
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mqttServerDtos = await _mqttAppService.GetAllMqttServersAsync();
|
var mqttServers = await _mqttAppService.GetAllMqttServersAsync();
|
||||||
MqttServers.Clear();
|
MqttServers.Clear();
|
||||||
|
|
||||||
foreach (var dto in mqttServerDtos)
|
foreach (var mqttServer in mqttServers)
|
||||||
{
|
{
|
||||||
MqttServers.Add(new MqttServerItem
|
MqttServers.Add(new MqttServerItem
|
||||||
{
|
{
|
||||||
Id = dto.Id,
|
Id = mqttServer.Id,
|
||||||
ServerName = dto.ServerName,
|
ServerName = mqttServer.ServerName,
|
||||||
ServerUrl = dto.ServerUrl,
|
ServerUrl = mqttServer.ServerUrl,
|
||||||
Port = dto.Port,
|
Port = mqttServer.Port,
|
||||||
Username = dto.Username,
|
Username = mqttServer.Username,
|
||||||
Password = dto.Password,
|
Password = mqttServer.Password,
|
||||||
IsActive = dto.IsActive,
|
IsActive = mqttServer.IsActive,
|
||||||
SubscribeTopic = dto.SubscribeTopic,
|
SubscribeTopic = mqttServer.SubscribeTopic,
|
||||||
PublishTopic = dto.PublishTopic,
|
PublishTopic = mqttServer.PublishTopic,
|
||||||
ClientId = dto.ClientId,
|
ClientId = mqttServer.ClientId,
|
||||||
CreatedAt = dto.CreatedAt,
|
CreatedAt = mqttServer.CreatedAt,
|
||||||
ConnectedAt = dto.ConnectedAt,
|
ConnectedAt = mqttServer.ConnectedAt,
|
||||||
ConnectionDuration = dto.ConnectionDuration,
|
ConnectionDuration = mqttServer.ConnectionDuration,
|
||||||
MessageFormat = dto.MessageFormat
|
MessageFormat = mqttServer.MessageFormat
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user