初步给Mqtt添加了别名功能
This commit is contained in:
@@ -1,34 +1,37 @@
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DMS.Application.Interfaces;
|
namespace DMS.Application.Interfaces;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义MQTT别名管理相关的应用服务操作。
|
/// 定义了MQTT别名管理相关的应用服务操作。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMqttAliasAppService
|
public interface IMqttAliasAppService
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID获取MQTT别名DTO。
|
/// 异步获取指定变量的所有MQTT别名关联。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id);
|
Task<List<VariableMqttAliasDto>> GetAliasesForVariableAsync(int variableId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步获取所有MQTT别名DTO列表。
|
/// 异步为变量分配或更新一个MQTT别名。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync();
|
/// <param name="variableId">变量ID。</param>
|
||||||
|
/// <param name="mqttServerId">MQTT服务器ID。</param>
|
||||||
/// <summary>
|
/// <param name="alias">要设置的别名。</param>
|
||||||
/// 异步创建一个新的MQTT别名。
|
Task AssignAliasAsync(int variableId, int mqttServerId, string alias);
|
||||||
/// </summary>
|
|
||||||
Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步更新一个已存在的MQTT别名。
|
/// 异步更新一个已存在的MQTT别名。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto);
|
/// <param name="aliasId">别名关联的ID。</param>
|
||||||
|
/// <param name="newAlias">新的别名字符串。</param>
|
||||||
|
Task UpdateAliasAsync(int aliasId, string newAlias);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步删除一个MQTT别名。
|
/// 异步移除一个MQTT别名关联。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task DeleteMqttAliasAsync(int id);
|
/// <param name="aliasId">要移除的别名关联的ID。</param>
|
||||||
|
Task RemoveAliasAsync(int aliasId);
|
||||||
}
|
}
|
||||||
@@ -648,35 +648,64 @@ public class DataCenterService : IDataCenterService
|
|||||||
MenuTrees.Clear();
|
MenuTrees.Clear();
|
||||||
MqttServers.Clear();
|
MqttServers.Clear();
|
||||||
|
|
||||||
// 顺序加载所有数据,避免数据库连接并发问题
|
// 加载所有设备
|
||||||
var devices = await LoadAllDevicesAsync();
|
var devices = await _repositoryManager.Devices.GetAllAsync();
|
||||||
var variableTables = await LoadAllVariableTablesAsync();
|
var deviceDtos = _mapper.Map<List<DeviceDto>>(devices);
|
||||||
var variables = await LoadAllVariablesAsync();
|
|
||||||
var menus = await LoadAllMenusAsync();
|
// 加载所有变量表
|
||||||
|
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
||||||
|
var variableTableDtos = _mapper.Map<List<VariableTableDto>>(variableTables);
|
||||||
|
|
||||||
|
// 加载所有变量
|
||||||
|
var variables = await _repositoryManager.Variables.GetAllAsync();
|
||||||
|
var variableDtos = _mapper.Map<List<VariableDto>>(variables);
|
||||||
|
|
||||||
|
// 加载所有菜单
|
||||||
|
var menus = await _repositoryManager.Menus.GetAllAsync();
|
||||||
|
var menuDtos = _mapper.Map<List<MenuBeanDto>>(menus);
|
||||||
|
|
||||||
var mqttServers = await LoadAllMqttServersAsync();
|
var mqttServers = await LoadAllMqttServersAsync();
|
||||||
|
|
||||||
// 加载设备数据到内存
|
// 建立设备与变量表的关联
|
||||||
foreach (var device in devices)
|
foreach (var deviceDto in deviceDtos)
|
||||||
{
|
{
|
||||||
Devices.TryAdd(device.Id, device);
|
deviceDto.VariableTables = variableTableDtos
|
||||||
|
.Where(vt => vt.DeviceId == deviceDto.Id)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// 将设备添加到安全字典
|
||||||
|
Devices.TryAdd(deviceDto.Id, deviceDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载变量表数据到内存
|
// 建立变量表与变量的关联
|
||||||
foreach (var variableTable in variableTables)
|
foreach (var variableTableDto in variableTableDtos)
|
||||||
{
|
{
|
||||||
VariableTables.TryAdd(variableTable.Id, variableTable);
|
variableTableDto.Variables = variableDtos
|
||||||
|
.Where(v => v.VariableTableId == variableTableDto.Id)
|
||||||
|
.ToList();
|
||||||
|
if (Devices.TryGetValue(variableTableDto.DeviceId, out var deviceDto))
|
||||||
|
{
|
||||||
|
variableTableDto.Device = deviceDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载变量数据到内存
|
// 将变量表添加到安全字典
|
||||||
foreach (var variable in variables)
|
VariableTables.TryAdd(variableTableDto.Id, variableTableDto);
|
||||||
{
|
|
||||||
Variables.TryAdd(variable.Id, variable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载菜单数据到内存
|
// 将变量添加到安全字典
|
||||||
foreach (var menu in menus)
|
foreach (var variableDto in variableDtos)
|
||||||
{
|
{
|
||||||
Menus.TryAdd(menu.Id, menu);
|
if (VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable))
|
||||||
|
{
|
||||||
|
variableDto.VariableTable = variableTable;
|
||||||
|
}
|
||||||
|
Variables.TryAdd(variableDto.Id, variableDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将菜单添加到安全字典
|
||||||
|
foreach (var menuDto in menuDtos)
|
||||||
|
{
|
||||||
|
Menus.TryAdd(menuDto.Id, menuDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载MQTT服务器数据到内存
|
// 加载MQTT服务器数据到内存
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using DMS.Core.Interfaces;
|
|
||||||
using DMS.Core.Models;
|
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
|
using DMS.Core.Interfaces;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace DMS.Application.Services;
|
namespace DMS.Application.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MQTT别名应用服务,负责处理MQTT别名相关的业务逻辑。
|
/// IMqttAliasAppService 的实现,负责管理变量与MQTT服务器的别名关联。
|
||||||
/// 实现 <see cref="IMqttAliasAppService"/> 接口。
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MqttAliasAppService : IMqttAliasAppService
|
public class MqttAliasAppService : IMqttAliasAppService
|
||||||
{
|
{
|
||||||
@@ -16,10 +18,8 @@ public class MqttAliasAppService : IMqttAliasAppService
|
|||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
/// 构造函数。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repoManager">仓储管理器实例。</param>
|
|
||||||
/// <param name="mapper">AutoMapper 实例。</param>
|
|
||||||
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||||
{
|
{
|
||||||
_repoManager = repoManager;
|
_repoManager = repoManager;
|
||||||
@@ -27,94 +27,93 @@ public class MqttAliasAppService : IMqttAliasAppService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步根据ID获取MQTT别名数据传输对象。
|
/// 异步获取指定变量的所有MQTT别名关联。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">MQTT别名ID。</param>
|
public async Task<List<VariableMqttAliasDto>> GetAliasesForVariableAsync(int variableId)
|
||||||
/// <returns>MQTT别名数据传输对象。</returns>
|
|
||||||
public async Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id)
|
|
||||||
{
|
{
|
||||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(id);
|
// 从仓储获取别名,并确保加载了关联的MqttServer信息
|
||||||
return _mapper.Map<VariableMqttAliasDto>(mqttAlias);
|
var aliases = await _repoManager.VariableMqttAliases.GetAliasesForVariableAsync(variableId);
|
||||||
|
return _mapper.Map<List<VariableMqttAliasDto>>(aliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步获取所有MQTT别名数据传输对象列表。
|
/// 异步为变量分配或更新一个MQTT别名。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>MQTT别名数据传输对象列表。</returns>
|
public async Task AssignAliasAsync(int variableId, int mqttServerId, string alias)
|
||||||
public async Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync()
|
|
||||||
{
|
|
||||||
var mqttAliases = await _repoManager.VariableMqttAliases.GetAllAsync();
|
|
||||||
return _mapper.Map<List<VariableMqttAliasDto>>(mqttAliases);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步创建一个新MQTT别名(事务性操作)。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mqttAliasDto">要创建的MQTT别名数据传输对象。</param>
|
|
||||||
/// <returns>新创建MQTT别名的ID。</returns>
|
|
||||||
/// <exception cref="ApplicationException">如果创建MQTT别名时发生错误。</exception>
|
|
||||||
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _repoManager.BeginTranAsync();
|
await _repoManager.BeginTranAsync();
|
||||||
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
|
|
||||||
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
|
// 检查是否已存在该变量与该服务器的关联
|
||||||
await _repoManager.CommitAsync();
|
var existingAlias = await _repoManager.VariableMqttAliases.GetByVariableAndServerAsync(variableId, mqttServerId);
|
||||||
return mqttAlias.Id;
|
|
||||||
}
|
if (existingAlias != null)
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
await _repoManager.RollbackAsync();
|
// 如果存在,则更新别名
|
||||||
throw new ApplicationException("创建MQTT别名时发生错误,操作已回滚。", ex);
|
existingAlias.Alias = alias;
|
||||||
|
await _repoManager.VariableMqttAliases.UpdateAsync(existingAlias);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 如果不存在,则创建新的关联
|
||||||
|
var newAlias = new VariableMqttAlias
|
||||||
|
{
|
||||||
|
VariableId = variableId,
|
||||||
|
MqttServerId = mqttServerId,
|
||||||
|
Alias = alias
|
||||||
|
};
|
||||||
|
await _repoManager.VariableMqttAliases.AddAsync(newAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步更新一个已存在的MQTT别名(事务性操作)。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mqttAliasDto">要更新的MQTT别名数据传输对象。</param>
|
|
||||||
/// <returns>表示异步操作的任务。</returns>
|
|
||||||
/// <exception cref="ApplicationException">如果找不到MQTT别名或更新MQTT别名时发生错误。</exception>
|
|
||||||
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _repoManager.BeginTranAsync();
|
|
||||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
|
|
||||||
if (mqttAlias == null)
|
|
||||||
{
|
|
||||||
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
|
|
||||||
}
|
|
||||||
_mapper.Map(mqttAliasDto, mqttAlias);
|
|
||||||
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
|
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await _repoManager.RollbackAsync();
|
await _repoManager.RollbackAsync();
|
||||||
throw new ApplicationException("更新MQTT别名时发生错误,操作已回滚。", ex);
|
throw new ApplicationException("分配/更新MQTT别名失败。", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步删除一个MQTT别名(事务性操作)。
|
/// 异步更新一个已存在的MQTT别名。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">要删除MQTT别名的ID。</param>
|
public async Task UpdateAliasAsync(int aliasId, string newAlias)
|
||||||
/// <returns>表示异步操作的任务。</returns>
|
|
||||||
/// <exception cref="ApplicationException">如果删除MQTT别名时发生错误。</exception>
|
|
||||||
public async Task DeleteMqttAliasAsync(int id)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _repoManager.BeginTranAsync();
|
await _repoManager.BeginTranAsync();
|
||||||
await _repoManager.VariableMqttAliases.DeleteByIdAsync(id);
|
var aliasToUpdate = await _repoManager.VariableMqttAliases.GetByIdAsync(aliasId);
|
||||||
|
if (aliasToUpdate == null)
|
||||||
|
{
|
||||||
|
throw new KeyNotFoundException($"未找到ID为 {aliasId} 的MQTT别名关联。");
|
||||||
|
}
|
||||||
|
aliasToUpdate.Alias = newAlias;
|
||||||
|
await _repoManager.VariableMqttAliases.UpdateAsync(aliasToUpdate);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await _repoManager.RollbackAsync();
|
await _repoManager.RollbackAsync();
|
||||||
throw new ApplicationException("删除MQTT别名时发生错误,操作已回滚。", ex);
|
throw new ApplicationException("更新MQTT别名失败。", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步移除一个MQTT别名关联。
|
||||||
|
/// </summary>
|
||||||
|
public async Task RemoveAliasAsync(int aliasId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _repoManager.BeginTranAsync();
|
||||||
|
await _repoManager.VariableMqttAliases.DeleteByIdAsync(aliasId);
|
||||||
|
await _repoManager.CommitAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await _repoManager.RollbackAsync();
|
||||||
|
throw new ApplicationException("移除MQTT别名失败。", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,19 @@
|
|||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DMS.Core.Interfaces.Repositories
|
namespace DMS.Core.Interfaces.Repositories
|
||||||
{
|
{
|
||||||
public interface IVariableMqttAliasRepository:IBaseRepository<VariableMqttAlias>
|
public interface IVariableMqttAliasRepository : IBaseRepository<VariableMqttAlias>
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取指定变量的所有MQTT别名关联。
|
||||||
|
/// </summary>
|
||||||
|
Task<List<VariableMqttAlias>> GetAliasesForVariableAsync(int variableId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据变量和服务器获取别名关联。
|
||||||
|
/// </summary>
|
||||||
|
Task<VariableMqttAlias> GetByVariableAndServerAsync(int variableId, int mqttServerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,12 @@ using System.Diagnostics;
|
|||||||
using DMS.Core.Interfaces.Repositories;
|
using DMS.Core.Interfaces.Repositories;
|
||||||
using DMS.Infrastructure.Data;
|
using DMS.Infrastructure.Data;
|
||||||
using DMS.Infrastructure.Entities;
|
using DMS.Infrastructure.Entities;
|
||||||
|
|
||||||
|
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace DMS.Infrastructure.Repositories;
|
namespace DMS.Infrastructure.Repositories;
|
||||||
|
|
||||||
@@ -109,4 +110,26 @@ public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>,
|
|||||||
var dbEntities = _mapper.Map<List<DbVariableMqttAlias>>(entities);
|
var dbEntities = _mapper.Map<List<DbVariableMqttAlias>>(entities);
|
||||||
return base.AddBatchAsync(dbEntities);
|
return base.AddBatchAsync(dbEntities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步获取指定变量的所有MQTT别名关联。
|
||||||
|
/// </summary>
|
||||||
|
public async Task<List<VariableMqttAlias>> GetAliasesForVariableAsync(int variableId)
|
||||||
|
{
|
||||||
|
var dbList = await Db.Queryable<DbVariableMqttAlias>()
|
||||||
|
.Where(x => x.VariableId == variableId)
|
||||||
|
.ToListAsync();
|
||||||
|
return _mapper.Map<List<VariableMqttAlias>>(dbList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步根据变量和服务器获取别名关联。
|
||||||
|
/// </summary>
|
||||||
|
public async Task<VariableMqttAlias> GetByVariableAndServerAsync(int variableId, int mqttServerId)
|
||||||
|
{
|
||||||
|
var dbAlias = await Db.Queryable<DbVariableMqttAlias>()
|
||||||
|
.Where(x => x.VariableId == variableId && x.MqttServerId == mqttServerId)
|
||||||
|
.FirstAsync();
|
||||||
|
return _mapper.Map<VariableMqttAlias>(dbAlias);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -171,6 +171,7 @@ public partial class App : System.Windows.Application
|
|||||||
|
|
||||||
// 注册MQTT服务管理器
|
// 注册MQTT服务管理器
|
||||||
services.AddSingleton<IMqttServiceManager, MqttServiceManager>();
|
services.AddSingleton<IMqttServiceManager, MqttServiceManager>();
|
||||||
|
services.AddSingleton<IMqttAliasAppService, MqttAliasAppService>();
|
||||||
services.AddHostedService<MqttBackgroundService>();
|
services.AddHostedService<MqttBackgroundService>();
|
||||||
|
|
||||||
// 注册WPF中的服务
|
// 注册WPF中的服务
|
||||||
|
|||||||
Reference in New Issue
Block a user