2025-07-21 14:35:17 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-09-06 16:12:30 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-10-06 17:25:05 +08:00
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
|
namespace DMS.Application.Services.Database;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// IMqttAliasAppService 的实现,负责管理变量与MQTT服务器的别名关联。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttAliasAppService : IMqttAliasAppService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repoManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// 构造函数。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-07-21 14:35:17 +08:00
|
|
|
|
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
|
|
|
|
|
{
|
|
|
|
|
|
_repoManager = repoManager;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// 异步获取指定变量的所有MQTT别名关联。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-10-06 17:25:05 +08:00
|
|
|
|
public async Task<List<VariableMqttAlias>> GetAliasesForVariableAsync(int variableId)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-09-06 16:12:30 +08:00
|
|
|
|
// 从仓储获取别名,并确保加载了关联的MqttServer信息
|
|
|
|
|
|
var aliases = await _repoManager.VariableMqttAliases.GetAliasesForVariableAsync(variableId);
|
2025-10-06 17:25:05 +08:00
|
|
|
|
return aliases.ToList();
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// 异步为变量分配或更新一个MQTT别名。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
public async Task AssignAliasAsync(int variableId, int mqttServerId, string alias)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查是否已存在该变量与该服务器的关联
|
|
|
|
|
|
var existingAlias = await _repoManager.VariableMqttAliases.GetByVariableAndServerAsync(variableId, mqttServerId);
|
|
|
|
|
|
|
|
|
|
|
|
if (existingAlias != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果存在,则更新别名
|
|
|
|
|
|
existingAlias.Alias = alias;
|
|
|
|
|
|
await _repoManager.VariableMqttAliases.UpdateAsync(existingAlias);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果不存在,则创建新的关联
|
2025-09-06 19:41:28 +08:00
|
|
|
|
// 获取关联的Variable和MqttServer实体
|
|
|
|
|
|
var variable = await _repoManager.Variables.GetByIdAsync(variableId);
|
|
|
|
|
|
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerId);
|
|
|
|
|
|
|
2025-09-06 16:12:30 +08:00
|
|
|
|
var newAlias = new VariableMqttAlias
|
|
|
|
|
|
{
|
|
|
|
|
|
VariableId = variableId,
|
|
|
|
|
|
MqttServerId = mqttServerId,
|
2025-09-06 19:41:28 +08:00
|
|
|
|
Alias = alias,
|
|
|
|
|
|
Variable = variable,
|
|
|
|
|
|
MqttServer = mqttServer
|
2025-09-06 16:12:30 +08:00
|
|
|
|
};
|
|
|
|
|
|
await _repoManager.VariableMqttAliases.AddAsync(newAlias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
throw new ApplicationException("分配/更新MQTT别名失败。", ex);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// 异步更新一个已存在的MQTT别名。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
public async Task UpdateAliasAsync(int aliasId, string newAlias)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
var aliasToUpdate = await _repoManager.VariableMqttAliases.GetByIdAsync(aliasId);
|
|
|
|
|
|
if (aliasToUpdate == null)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
2025-09-06 16:12:30 +08:00
|
|
|
|
throw new KeyNotFoundException($"未找到ID为 {aliasId} 的MQTT别名关联。");
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-09-06 16:12:30 +08:00
|
|
|
|
aliasToUpdate.Alias = newAlias;
|
|
|
|
|
|
await _repoManager.VariableMqttAliases.UpdateAsync(aliasToUpdate);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
throw new ApplicationException("更新MQTT别名失败。", ex);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
/// 异步移除一个MQTT别名关联。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
2025-09-06 16:12:30 +08:00
|
|
|
|
public async Task RemoveAliasAsync(int aliasId)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
await _repoManager.VariableMqttAliases.DeleteByIdAsync(aliasId);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
throw new ApplicationException("移除MQTT别名失败。", ex);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|