bugfix:修复Mqtt服务器详情页变量值不更新的问题,修复修改Mqtt别名后台不更新的问题,重新调整了别名的架构
This commit is contained in:
18
DMS.Application/Events/MqttAliasChangedEventArgs.cs
Normal file
18
DMS.Application/Events/MqttAliasChangedEventArgs.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Events;
|
||||
|
||||
public class MqttAliasChangedEventArgs : EventArgs
|
||||
{
|
||||
public ActionChangeType ChangeType { get; }
|
||||
public MqttAlias MqttAlias { get; }
|
||||
public MqttAliasPropertyType PropertyType { get; }
|
||||
|
||||
public MqttAliasChangedEventArgs(ActionChangeType changeType, MqttAlias mqttAlias, MqttAliasPropertyType propertyType = MqttAliasPropertyType.All)
|
||||
{
|
||||
ChangeType = changeType;
|
||||
MqttAlias = mqttAlias;
|
||||
PropertyType = propertyType;
|
||||
}
|
||||
}
|
||||
@@ -9,29 +9,25 @@ namespace DMS.Application.Interfaces.Database;
|
||||
/// </summary>
|
||||
public interface IMqttAliasAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步获取指定变量的所有MQTT别名关联。
|
||||
/// </summary>
|
||||
Task<List<MqttAlias>> GetAliasesForVariableAsync(int variableId);
|
||||
|
||||
/// <summary>
|
||||
/// 异步为变量分配或更新一个MQTT别名。
|
||||
/// </summary>
|
||||
/// <param name="variableId">变量ID。</param>
|
||||
/// <param name="mqttServerId">MQTT服务器ID。</param>
|
||||
/// <param name="alias">要设置的别名。</param>
|
||||
Task AssignAliasAsync(int variableId, int mqttServerId, string alias);
|
||||
/// <param name="mqttAlias"></param>
|
||||
Task<MqttAlias> AssignAliasAsync(MqttAlias mqttAlias);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT别名。
|
||||
/// </summary>
|
||||
/// <param name="aliasId">别名关联的ID。</param>
|
||||
/// <param name="newAlias">新的别名字符串。</param>
|
||||
Task UpdateAliasAsync(int aliasId, string newAlias);
|
||||
Task<int> UpdateAliasAsync(MqttAlias mqttAlias);
|
||||
|
||||
/// <summary>
|
||||
/// 异步移除一个MQTT别名关联。
|
||||
/// </summary>
|
||||
/// <param name="aliasId">要移除的别名关联的ID。</param>
|
||||
Task RemoveAliasAsync(int aliasId);
|
||||
Task<int> RemoveAliasAsync(int aliasId);
|
||||
|
||||
Task<List<MqttAlias>> GetAllAsync();
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public interface IAppDataStorageService
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有MQTT变量别名的数据
|
||||
/// </summary>
|
||||
ConcurrentDictionary<int, MqttAlias> VariableMqttAliases { get; }
|
||||
ConcurrentDictionary<int, MqttAlias> MqttAliases { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有触发器定义数据
|
||||
|
||||
@@ -68,6 +68,18 @@ public interface IEventService
|
||||
/// <param name="e">MQTT服务器改变事件参数</param>
|
||||
void RaiseMqttServerChanged(object sender, MqttServerChangedEventArgs e);
|
||||
|
||||
/// <summary>
|
||||
/// Mqtt别名改变事件
|
||||
/// </summary>
|
||||
event EventHandler<MqttAliasChangedEventArgs> OnMqttAliasChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发Mqtt别名改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">Mqtt别名改变事件参数</param>
|
||||
void RaiseMqttAliasChanged(object sender, MqttAliasChangedEventArgs e);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Interfaces.Management;
|
||||
|
||||
public interface IMqttAliasManagementService
|
||||
{
|
||||
Task<MqttAlias> AssignAliasAsync(MqttAlias alias);
|
||||
Task<int> UpdateAsync(MqttAlias alias);
|
||||
Task<bool> DeleteAsync(int id);
|
||||
Task<List<MqttAlias>> LoadAllMqttAliasAsync();
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class AppDataStorageService : IAppDataStorageService
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有MQTT变量别名的数据
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<int, MqttAlias> VariableMqttAliases { get; } = new();
|
||||
public ConcurrentDictionary<int, MqttAlias> MqttAliases { get; } = new();
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ public class DataLoaderService : IDataLoaderService
|
||||
private readonly IMenuAppService _menuService;
|
||||
private readonly IMqttAppService _mqttAppService;
|
||||
private readonly INlogAppService _nlogAppService;
|
||||
private readonly IMqttAliasManagementService _mqttAliasManagementService;
|
||||
private readonly ITriggerManagementService _triggerManagementService; // 添加触发器管理服务
|
||||
private readonly IEventService _eventService; // 添加事件服务
|
||||
|
||||
@@ -43,6 +44,7 @@ public class DataLoaderService : IDataLoaderService
|
||||
IMenuAppService menuService,
|
||||
IMqttAppService mqttAppService,
|
||||
INlogAppService nlogAppService,
|
||||
IMqttAliasManagementService mqttAliasManagementService,
|
||||
ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数
|
||||
IEventService eventService) // 添加事件服务参数
|
||||
{
|
||||
@@ -55,6 +57,7 @@ public class DataLoaderService : IDataLoaderService
|
||||
_menuService = menuService;
|
||||
_mqttAppService = mqttAppService;
|
||||
_nlogAppService = nlogAppService;
|
||||
this._mqttAliasManagementService = mqttAliasManagementService;
|
||||
_triggerManagementService = triggerManagementService; // 初始化触发器管理服务
|
||||
_eventService = eventService; // 初始化事件服务
|
||||
}
|
||||
@@ -81,7 +84,7 @@ public class DataLoaderService : IDataLoaderService
|
||||
await LoadAllNlogsAsync(LoadLogCount);
|
||||
|
||||
// 获取变量MQTT别名
|
||||
await LoadAllVariableMqttAliases();
|
||||
await _mqttAliasManagementService.LoadAllMqttAliasAsync();
|
||||
|
||||
// 加载所有触发器
|
||||
await LoadAllTriggersAsync();
|
||||
@@ -103,26 +106,7 @@ public class DataLoaderService : IDataLoaderService
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadAllVariableMqttAliases()
|
||||
{
|
||||
|
||||
var variableMqttAliases = await _repositoryManager.VariableMqttAliases.GetAllAsync();
|
||||
foreach (var variableMqttAlias in variableMqttAliases)
|
||||
{
|
||||
_appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAlias.Id, variableMqttAlias);
|
||||
if (_appDataStorageService.Variables.TryGetValue(variableMqttAlias.VariableId, out var variable))
|
||||
{
|
||||
variableMqttAlias.Variable = _mapper.Map<Variable>(variable);
|
||||
variable.MqttAliases?.Add(variableMqttAlias);
|
||||
}
|
||||
|
||||
if (_appDataStorageService.MqttServers.TryGetValue(variableMqttAlias.MqttServerId, out var mqttServer))
|
||||
{
|
||||
variableMqttAlias.MqttServer = mqttServer;
|
||||
mqttServer.VariableAliases?.Add(variableMqttAlias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载所有设备数据
|
||||
|
||||
@@ -3,9 +3,6 @@ using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Application.Services.Database;
|
||||
|
||||
@@ -15,111 +12,47 @@ namespace DMS.Application.Services.Database;
|
||||
public class MqttAliasAppService : IMqttAliasAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数。
|
||||
/// </summary>
|
||||
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
public MqttAliasAppService(IRepositoryManager repoManager,IAppDataStorageService appDataStorageService, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取指定变量的所有MQTT别名关联。
|
||||
/// </summary>
|
||||
public async Task<List<MqttAlias>> GetAliasesForVariableAsync(int variableId)
|
||||
{
|
||||
// 从仓储获取别名,并确保加载了关联的MqttServer信息
|
||||
var aliases = await _repoManager.VariableMqttAliases.GetAliasesForVariableAsync(variableId);
|
||||
return aliases.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步为变量分配或更新一个MQTT别名。
|
||||
/// </summary>
|
||||
public async Task AssignAliasAsync(int variableId, int mqttServerId, string alias)
|
||||
public async Task<MqttAlias> AssignAliasAsync(MqttAlias mqttAlias)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
|
||||
// 检查是否已存在该变量与该服务器的关联
|
||||
var existingAlias = await _repoManager.VariableMqttAliases.GetByVariableAndServerAsync(variableId, mqttServerId);
|
||||
|
||||
if (existingAlias != null)
|
||||
{
|
||||
// 如果存在,则更新别名
|
||||
existingAlias.Alias = alias;
|
||||
await _repoManager.VariableMqttAliases.UpdateAsync(existingAlias);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果不存在,则创建新的关联
|
||||
// 获取关联的Variable和MqttServer实体
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(variableId);
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerId);
|
||||
|
||||
var newAlias = new MqttAlias
|
||||
{
|
||||
VariableId = variableId,
|
||||
MqttServerId = mqttServerId,
|
||||
Alias = alias,
|
||||
Variable = variable,
|
||||
MqttServer = mqttServer
|
||||
};
|
||||
await _repoManager.VariableMqttAliases.AddAsync(newAlias);
|
||||
}
|
||||
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("分配/更新MQTT别名失败。", ex);
|
||||
}
|
||||
return await _repoManager.MqttAliases.AddAsync(mqttAlias);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT别名。
|
||||
/// </summary>
|
||||
public async Task UpdateAliasAsync(int aliasId, string newAlias)
|
||||
public async Task<int> UpdateAliasAsync(MqttAlias mqttAlias)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
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();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新MQTT别名失败。", ex);
|
||||
}
|
||||
return await _repoManager.MqttAliases.UpdateAsync(mqttAlias);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步移除一个MQTT别名关联。
|
||||
/// </summary>
|
||||
public async Task RemoveAliasAsync(int aliasId)
|
||||
public async Task<int> 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);
|
||||
}
|
||||
return await _repoManager.MqttAliases.DeleteByIdAsync(aliasId);
|
||||
}
|
||||
|
||||
public async Task<List<MqttAlias>> GetAllAsync()
|
||||
{
|
||||
var mqttAliases = await _repoManager.MqttAliases.GetAllAsync();
|
||||
|
||||
return mqttAliases;
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,21 @@ public class EventService : IEventService
|
||||
OnMqttServerChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mqtt别名改变事件
|
||||
/// </summary>
|
||||
public event EventHandler<MqttAliasChangedEventArgs> OnMqttAliasChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发Mqtt别名改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">Mqtt别名改变事件参数</param>
|
||||
public void RaiseMqttAliasChanged(object sender, MqttAliasChangedEventArgs e)
|
||||
{
|
||||
OnMqttAliasChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 数据加载事件
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
using AutoMapper;
|
||||
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;
|
||||
|
||||
public class MqttAliasManagementService : IMqttAliasManagementService
|
||||
{
|
||||
private readonly IMqttAliasAppService _appService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IAppDataStorageService _storageService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MqttAliasManagementService(IMqttAliasAppService appService, IEventService eventService,
|
||||
IAppDataStorageService storageService, IMapper mapper)
|
||||
{
|
||||
_appService = appService;
|
||||
_eventService = eventService;
|
||||
_storageService = storageService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MqttAlias> AssignAliasAsync(MqttAlias alias)
|
||||
{
|
||||
var newAlias = await _appService.AssignAliasAsync(alias);
|
||||
if (newAlias != null)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttServers.TryGetValue(newAlias.MqttServerId, out var server))
|
||||
{
|
||||
server.VariableAliases.Add(newAlias);
|
||||
}
|
||||
|
||||
// Add to cache
|
||||
if (_storageService.Variables.TryGetValue(newAlias.VariableId, out var variable))
|
||||
{
|
||||
variable.MqttAliases.Add(newAlias);
|
||||
}
|
||||
|
||||
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, newAlias));
|
||||
}
|
||||
|
||||
return newAlias;
|
||||
}
|
||||
|
||||
public async Task<List<MqttAlias>> LoadAllMqttAliasAsync()
|
||||
{
|
||||
var mqttAliases = await _appService.GetAllAsync();
|
||||
foreach (var mqttAlias in mqttAliases)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttServers.TryGetValue(mqttAlias.MqttServerId, out var server))
|
||||
{
|
||||
mqttAlias.MqttServer = server;
|
||||
server.VariableAliases.Add(mqttAlias);
|
||||
}
|
||||
|
||||
// Add to cache
|
||||
if (_storageService.Variables.TryGetValue(mqttAlias.VariableId, out var variable))
|
||||
{
|
||||
mqttAlias.Variable= variable;
|
||||
variable.MqttAliases.Add(mqttAlias);
|
||||
}
|
||||
_storageService.MqttAliases.TryAdd(mqttAlias.Id, mqttAlias);
|
||||
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, mqttAlias));
|
||||
}
|
||||
|
||||
return mqttAliases;
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(MqttAlias alias)
|
||||
{
|
||||
int res = await _appService.UpdateAliasAsync(alias);
|
||||
if (res>0)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttAliases.TryGetValue(alias.Id, out var mqttAlias))
|
||||
{
|
||||
mqttAlias.Alias = alias.Alias;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<bool> DeleteAsync(int id)
|
||||
{
|
||||
var res = await DeleteBatchAsync(new List<int> { id });
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteBatchAsync(List<int> ids)
|
||||
{
|
||||
int counter = 0;
|
||||
foreach (var id in ids)
|
||||
{
|
||||
if (!_storageService.MqttAliases.TryGetValue(id, out var mqttAlias))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var variableId = mqttAlias.VariableId;
|
||||
var mqttServerId = mqttAlias.MqttServerId;
|
||||
|
||||
var result = await _appService.RemoveAliasAsync(id);
|
||||
if (result == 0) continue;
|
||||
|
||||
// Remove from cache
|
||||
if (_storageService.MqttServers.TryGetValue(mqttServerId, out var server))
|
||||
{
|
||||
var aliasToRemove = server.VariableAliases.FirstOrDefault(a => a.Id == id);
|
||||
if (aliasToRemove != null)
|
||||
{
|
||||
server.VariableAliases.Remove(aliasToRemove);
|
||||
|
||||
}
|
||||
}
|
||||
// Remove from cache
|
||||
if (_storageService.Variables.TryGetValue(mqttServerId, out var variable))
|
||||
{
|
||||
var aliasToRemove = variable.MqttAliases.FirstOrDefault(a => a.Id == id);
|
||||
if (aliasToRemove != null)
|
||||
{
|
||||
variable.MqttAliases.Remove(aliasToRemove);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
_eventService.RaiseMqttAliasChanged(
|
||||
this, new MqttAliasChangedEventArgs(ActionChangeType.Deleted, mqttAlias));
|
||||
counter++;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user