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;
|
||||
}
|
||||
}
|
||||
7
DMS.Core/Enums/MqttAliasPropertyType.cs
Normal file
7
DMS.Core/Enums/MqttAliasPropertyType.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace DMS.Core.Enums;
|
||||
|
||||
public enum MqttAliasPropertyType
|
||||
{
|
||||
Alias,
|
||||
All
|
||||
}
|
||||
@@ -33,7 +33,7 @@ public interface IRepositoryManager : IDisposable
|
||||
/// <summary>
|
||||
/// 获取变量MQTT别名仓储的实例。
|
||||
/// </summary>
|
||||
IVariableMqttAliasRepository VariableMqttAliases { get; set; }
|
||||
IVariableMqttAliasRepository MqttAliases { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取菜单仓储的实例。
|
||||
|
||||
@@ -6,15 +6,6 @@ namespace DMS.Core.Interfaces.Repositories
|
||||
{
|
||||
public interface IVariableMqttAliasRepository : IBaseRepository<MqttAlias>
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步获取指定变量的所有MQTT别名关联。
|
||||
/// </summary>
|
||||
Task<List<MqttAlias>> GetAliasesForVariableAsync(int variableId);
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据变量和服务器获取别名关联。
|
||||
/// </summary>
|
||||
Task<MqttAlias> GetByVariableAndServerAsync(int variableId, int mqttServerId);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有变量与MQTT别名关联。
|
||||
|
||||
@@ -35,4 +35,16 @@ public class MqttAlias
|
||||
/// 关联的MQTT服务器导航属性。
|
||||
/// </summary>
|
||||
public MqttServer MqttServer { get; set; }
|
||||
|
||||
public MqttAlias()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MqttAlias(int variableId, int mqttServerId, string alias)
|
||||
{
|
||||
VariableId = variableId;
|
||||
MqttServerId = mqttServerId;
|
||||
Alias = alias;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using SqlSugar;
|
||||
namespace DMS.Infrastructure.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库实体:对应数据库中的 VariableMqttAliases 表。
|
||||
/// 数据库实体:对应数据库中的 MqttAliases 表。
|
||||
/// </summary>
|
||||
public class DbVariableMqttAlias
|
||||
{
|
||||
|
||||
@@ -49,7 +49,7 @@ public class RepositoryManager : IRepositoryManager
|
||||
VariableTables = variableTables;
|
||||
Variables = variables;
|
||||
MqttServers = mqttServers;
|
||||
VariableMqttAliases = variableMqttAliases;
|
||||
MqttAliases = variableMqttAliases;
|
||||
Menus = menus;
|
||||
VariableHistories = variableHistories;
|
||||
Users = users;
|
||||
@@ -86,7 +86,7 @@ public class RepositoryManager : IRepositoryManager
|
||||
/// <summary>
|
||||
/// 获取变量MQTT别名仓储实例。
|
||||
/// </summary>
|
||||
public IVariableMqttAliasRepository VariableMqttAliases { get; set; }
|
||||
public IVariableMqttAliasRepository MqttAliases { get; set; }
|
||||
/// <summary>
|
||||
/// 获取菜单仓储实例。
|
||||
/// </summary>
|
||||
|
||||
@@ -111,77 +111,4 @@ public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>,
|
||||
var addedEntities = await base.AddBatchAsync(dbEntities);
|
||||
return _mapper.Map<List<MqttAlias>>(addedEntities);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取指定变量的所有MQTT别名关联。
|
||||
/// </summary>
|
||||
public async Task<List<MqttAlias>> GetAliasesForVariableAsync(int variableId)
|
||||
{
|
||||
// 查询别名关联,并包含关联的Variable和MqttServer信息
|
||||
var dbList = await _dbContext.GetInstance().Queryable<DbVariableMqttAlias>()
|
||||
.Where(x => x.VariableId == variableId)
|
||||
.ToListAsync();
|
||||
|
||||
// 手动加载关联的Variable和MqttServer实体
|
||||
var variableIds = dbList.Select(x => x.VariableId).Distinct().ToList();
|
||||
var mqttServerIds = dbList.Select(x => x.MqttServerId).Distinct().ToList();
|
||||
|
||||
var variables = await _dbContext.GetInstance().Queryable<DbVariable>()
|
||||
.In(x => x.Id, variableIds)
|
||||
.ToListAsync();
|
||||
|
||||
var mqttServers = await _dbContext.GetInstance().Queryable<DbMqttServer>()
|
||||
.In(x => x.Id, mqttServerIds)
|
||||
.ToListAsync();
|
||||
|
||||
// 将关联实体映射到领域模型
|
||||
var variableDict = variables.ToDictionary(v => v.Id, v => _mapper.Map<Variable>(v));
|
||||
var mqttServerDict = mqttServers.ToDictionary(m => m.Id, m => _mapper.Map<MqttServer>(m));
|
||||
|
||||
// 映射主实体并设置导航属性
|
||||
var result = _mapper.Map<List<MqttAlias>>(dbList);
|
||||
foreach (var alias in result)
|
||||
{
|
||||
if (variableDict.TryGetValue(alias.VariableId, out var variable))
|
||||
{
|
||||
alias.Variable = variable;
|
||||
}
|
||||
|
||||
if (mqttServerDict.TryGetValue(alias.MqttServerId, out var mqttServer))
|
||||
{
|
||||
alias.MqttServer = mqttServer;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据变量和服务器获取别名关联。
|
||||
/// </summary>
|
||||
public async Task<MqttAlias> GetByVariableAndServerAsync(int variableId, int mqttServerId)
|
||||
{
|
||||
var dbAlias = await _dbContext.GetInstance().Queryable<DbVariableMqttAlias>()
|
||||
.Where(x => x.VariableId == variableId && x.MqttServerId == mqttServerId)
|
||||
.FirstAsync();
|
||||
|
||||
if (dbAlias == null)
|
||||
return null;
|
||||
|
||||
// 手动加载关联的Variable和MqttServer实体
|
||||
var variable = await _dbContext.GetInstance().Queryable<DbVariable>()
|
||||
.Where(x => x.Id == variableId)
|
||||
.FirstAsync();
|
||||
|
||||
var mqttServer = await _dbContext.GetInstance().Queryable<DbMqttServer>()
|
||||
.Where(x => x.Id == mqttServerId)
|
||||
.FirstAsync();
|
||||
|
||||
// 映射主实体并设置导航属性
|
||||
var result = _mapper.Map<MqttAlias>(dbAlias);
|
||||
result.Variable = _mapper.Map<Variable>(variable);
|
||||
result.MqttServer = _mapper.Map<MqttServer>(mqttServer);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -270,9 +270,12 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
StringBuilder sb = new StringBuilder();
|
||||
var now = DateTime.Now;
|
||||
var timestamp = ((DateTimeOffset)now).ToUnixTimeMilliseconds();
|
||||
sb.Append(variableMqtt.MqttServer.MessageHeader.Replace("{timestamp}", timestamp.ToString()));
|
||||
sb.Append(variableMqtt.MqttServer.MessageContent.Replace("{name}", variableMqtt.Alias).Replace("{value}", variableMqtt.Variable.DisplayValue));
|
||||
sb.Append(variableMqtt.MqttServer.MessageFooter);
|
||||
if (!String.IsNullOrWhiteSpace(variableMqtt.MqttServer.MessageHeader))
|
||||
sb.Append(variableMqtt.MqttServer.MessageHeader.Replace("{timestamp}", timestamp.ToString()));
|
||||
if (!String.IsNullOrWhiteSpace(variableMqtt.MqttServer.MessageContent))
|
||||
sb.Append(variableMqtt.MqttServer.MessageContent.Replace("{name}", variableMqtt.Alias).Replace("{value}", variableMqtt.Variable.DisplayValue));
|
||||
if (!String.IsNullOrWhiteSpace(variableMqtt.MqttServer.MessageFooter))
|
||||
sb.Append(variableMqtt.MqttServer.MessageFooter);
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
@@ -264,6 +264,8 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IVariableManagementService, VariableManagementService>();
|
||||
services.AddSingleton<IMenuManagementService, MenuManagementService>();
|
||||
services.AddSingleton<IMqttManagementService, MqttManagementService>();
|
||||
services.AddSingleton<IMqttAliasAppService, MqttAliasAppService>();
|
||||
services.AddSingleton<IMqttAliasManagementService, MqttAliasManagementService>();
|
||||
services.AddSingleton<ILogManagementService, LogManagementService>();
|
||||
services.AddSingleton<ITriggerManagementService, TriggerManagementService>(); // 注册触发器管理服务
|
||||
services.AddSingleton<ITriggerEvaluationService, TriggerEvaluationService>(); // 注册触发器评估服务
|
||||
@@ -301,6 +303,7 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IVariableTableDataService, VariableTableDataService>();
|
||||
services.AddSingleton<IMenuDataService, MenuDataService>();
|
||||
services.AddSingleton<IMqttDataService, MqttDataService>();
|
||||
services.AddSingleton<IMqttAliasDataService, MqttAliasDataService>();
|
||||
services.AddSingleton<ILogDataService, LogDataService>();
|
||||
services.AddSingleton<ITriggerDataService, TriggerDataService>(); // 注册触发器数据服务
|
||||
services.AddSingleton<IDataEventService, DataEventService>();
|
||||
|
||||
@@ -42,6 +42,11 @@ public interface IDataStorageService
|
||||
/// </summary>
|
||||
ObservableCollection<NlogItem> Nlogs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名列表。
|
||||
/// </summary>
|
||||
ObservableDictionary<int, MqttAliasItem> MqttAliases { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发器列表。
|
||||
/// </summary>
|
||||
|
||||
36
DMS.WPF/Interfaces/IMqttAliasDataService.cs
Normal file
36
DMS.WPF/Interfaces/IMqttAliasDataService.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
|
||||
namespace DMS.WPF.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名数据服务接口。
|
||||
/// </summary>
|
||||
public interface IMqttAliasDataService
|
||||
{
|
||||
/// <summary>
|
||||
/// 加载所有MQTT别名数据。
|
||||
/// </summary>
|
||||
Task LoadMqttAliases();
|
||||
|
||||
/// <summary>
|
||||
/// 添加MQTT别名。
|
||||
/// </summary>
|
||||
Task<MqttAliasItem> AssignAliasAsync(MqttAliasItem mqttAlias);
|
||||
|
||||
/// <summary>
|
||||
/// 更新MQTT别名。
|
||||
/// </summary>
|
||||
Task<bool> UpdateMqttAlias(MqttAliasItem mqttAlias);
|
||||
|
||||
/// <summary>
|
||||
/// 删除MQTT别名。
|
||||
/// </summary>
|
||||
Task<bool> DeleteMqttAlias(MqttAliasItem mqttAlias);
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取MQTT别名。
|
||||
/// </summary>
|
||||
Task<MqttAliasItem> GetMqttAliasById(int id);
|
||||
}
|
||||
@@ -31,6 +31,11 @@ public interface IWPFDataService
|
||||
/// </summary>
|
||||
IMqttDataService MqttDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名数据服务。
|
||||
/// </summary>
|
||||
IMqttAliasDataService MqttAliasDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 日志数据服务。
|
||||
/// </summary>
|
||||
|
||||
@@ -106,7 +106,7 @@ public partial class VariableItem : ObservableObject
|
||||
/// 一个变量可以有多个MQTT别名。
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private List<MqttAliasItem>? _mqttAliases=new List<MqttAliasItem>();
|
||||
private List<MqttAliasItem> _mqttAliases=new List<MqttAliasItem>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置变量的信号类型 (如:AI, DI, AO, DO)。
|
||||
|
||||
@@ -14,21 +14,25 @@ namespace DMS.WPF.Profiles
|
||||
CreateMap<Device, DeviceItem>()
|
||||
.ReverseMap();
|
||||
CreateMap<Variable, VariableItem>()
|
||||
.ForMember(dest => dest.MqttAliases, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
CreateMap<VariableTable, VariableTableItem>()
|
||||
.ReverseMap();
|
||||
CreateMap<OpcUaNode, OpcUaNodeItem>()
|
||||
.ReverseMap();
|
||||
CreateMap<VariableItem, VariableItem>();
|
||||
CreateMap<MqttAlias, MqttAliasItem>().ReverseMap();
|
||||
|
||||
CreateMap<MqttAlias, MqttAliasItem>()
|
||||
.ReverseMap();
|
||||
|
||||
|
||||
CreateMap<MenuBeanDto, MenuItem>()
|
||||
.ReverseMap();
|
||||
|
||||
|
||||
|
||||
CreateMap<MqttServer, MqttServerItem>().ReverseMap();
|
||||
|
||||
CreateMap<MqttServer, MqttServerItem>()
|
||||
.ForMember(dest => dest.VariableAliases, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
CreateMap<UserDto, UserItem>().ReverseMap();
|
||||
CreateMap<VariableHistoryDto, VariableHistoryItem>().ReverseMap();
|
||||
CreateMap<Variable, VariableItem>()
|
||||
|
||||
@@ -142,6 +142,9 @@ public class DataEventService : IDataEventService
|
||||
_wpfDataService.MqttDataService.LoadMqttServers();
|
||||
_logger?.LogDebug("MQTT服务器数据加载完成");
|
||||
|
||||
_wpfDataService.MqttAliasDataService.LoadMqttAliases();
|
||||
_logger?.LogDebug("MQTT别名加载完成");
|
||||
|
||||
_wpfDataService.LogDataService.LoadAllLog();
|
||||
_logger?.LogDebug("日志数据加载完成");
|
||||
|
||||
|
||||
@@ -46,6 +46,11 @@ public class DataStorageService : IDataStorageService
|
||||
/// </summary>
|
||||
public ObservableCollection<NlogItem> Nlogs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名列表。
|
||||
/// </summary>
|
||||
public ObservableDictionary<int, MqttAliasItem> MqttAliases { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发器列表。
|
||||
/// </summary>
|
||||
@@ -60,6 +65,7 @@ public class DataStorageService : IDataStorageService
|
||||
Menus=new ObservableCollection<MenuItem>();
|
||||
MenuTrees=new ObservableCollection<MenuItem>();
|
||||
Nlogs=new ObservableCollection<NlogItem>();
|
||||
MqttAliases=new ObservableDictionary<int, MqttAliasItem>();
|
||||
Triggers = new ObservableDictionary<int, TriggerItem>();
|
||||
|
||||
}
|
||||
|
||||
174
DMS.WPF/Services/MqttAliasDataService.cs
Normal file
174
DMS.WPF/Services/MqttAliasDataService.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using AutoMapper;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名数据服务类,负责管理MQTT别名相关的数据和操作。
|
||||
/// </summary>
|
||||
public class MqttAliasDataService : IMqttAliasDataService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IMqttAliasManagementService _mqttAliasManagementService;
|
||||
private readonly IDataStorageService _dataStorageService;
|
||||
|
||||
/// <summary>
|
||||
/// MqttAliasDataService类的构造函数。
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
/// <param name="appDataStorageService">应用数据存储服务实例。</param>
|
||||
/// <param name="mqttAliasManagementService">MQTT别名管理服务实例。</param>
|
||||
/// <param name="dataStorageService">数据存储服务实例。</param>
|
||||
public MqttAliasDataService(IMapper mapper,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IMqttAliasManagementService mqttAliasManagementService,
|
||||
IDataStorageService dataStorageService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_mqttAliasManagementService = mqttAliasManagementService;
|
||||
_dataStorageService = dataStorageService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载所有MQTT别名数据。
|
||||
/// </summary>
|
||||
public async Task LoadMqttAliases()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 清空现有数据
|
||||
_dataStorageService.MqttAliases.Clear();
|
||||
|
||||
// 加载MQTT别名数据
|
||||
foreach (var mqttAlias in _appDataStorageService.MqttAliases.Values)
|
||||
{
|
||||
MqttAliasItem mqttAliasItem = _mapper.Map<MqttAliasItem>(mqttAlias);
|
||||
if(_dataStorageService.MqttAliases.TryAdd(mqttAlias.Id, mqttAliasItem))
|
||||
{
|
||||
|
||||
if (_dataStorageService.MqttServers.TryGetValue(mqttAlias.MqttServerId,out var mqttServerItem))
|
||||
{
|
||||
mqttServerItem.VariableAliases.Add(mqttAliasItem);
|
||||
mqttAliasItem.MqttServer = mqttServerItem;
|
||||
}
|
||||
|
||||
if (_dataStorageService.Variables.TryGetValue(mqttAlias.VariableId,out var variableItem))
|
||||
{
|
||||
variableItem.MqttAliases.Add(mqttAliasItem);
|
||||
mqttAliasItem.Variable = variableItem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录异常或处理错误
|
||||
Console.WriteLine($"加载MQTT别名数据时发生错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加MQTT别名。
|
||||
/// </summary>
|
||||
public async Task<MqttAliasItem> AssignAliasAsync(MqttAliasItem mqttAlias)
|
||||
{
|
||||
var addMqttAlias = await _mqttAliasManagementService.AssignAliasAsync(_mapper.Map<MqttAlias>(mqttAlias));
|
||||
|
||||
|
||||
if (_dataStorageService.MqttAliases.ContainsKey(addMqttAlias.Id))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
mqttAlias.Id = addMqttAlias.Id;
|
||||
|
||||
_dataStorageService.MqttAliases.Add(mqttAlias.Id, mqttAlias);
|
||||
if (_dataStorageService.MqttServers.TryGetValue(mqttAlias.MqttServerId, out var mqttServerItem))
|
||||
{
|
||||
mqttAlias.MqttServer = mqttServerItem;
|
||||
mqttServerItem.VariableAliases.Add(mqttAlias);
|
||||
}
|
||||
if (_dataStorageService.Variables.TryGetValue(mqttAlias.VariableId, out var variableItem))
|
||||
{
|
||||
mqttAlias.Variable = variableItem;
|
||||
variableItem.MqttAliases.Add(mqttAlias);
|
||||
}
|
||||
|
||||
return mqttAlias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新MQTT别名。
|
||||
/// </summary>
|
||||
public async Task<bool> UpdateMqttAlias(MqttAliasItem mqttAlias)
|
||||
{
|
||||
var result = await _mqttAliasManagementService.UpdateAsync(_mapper.Map<MqttAlias>(mqttAlias));
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
// 更新界面数据
|
||||
if (_dataStorageService.MqttAliases.TryGetValue(mqttAlias.Id, out var existingAlias))
|
||||
{
|
||||
// 更新现有别名的属性
|
||||
existingAlias.VariableId = mqttAlias.VariableId;
|
||||
existingAlias.MqttServerId = mqttAlias.MqttServerId;
|
||||
existingAlias.Alias = mqttAlias.Alias;
|
||||
existingAlias.MqttServerName = mqttAlias.MqttServerName;
|
||||
}
|
||||
}
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除MQTT别名。
|
||||
/// </summary>
|
||||
public async Task<bool> DeleteMqttAlias(MqttAliasItem mqttAlias)
|
||||
{
|
||||
// 从数据库和内存中删除MQTT别名
|
||||
var result = await _mqttAliasManagementService.UpdateAsync(new MqttAlias
|
||||
{
|
||||
Id = mqttAlias.Id,
|
||||
VariableId = mqttAlias.VariableId,
|
||||
MqttServerId = mqttAlias.MqttServerId,
|
||||
Alias = null // Setting alias to null effectively removes the alias
|
||||
});
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
// 从界面删除MQTT别名
|
||||
_dataStorageService.MqttAliases.Remove(mqttAlias.Id);
|
||||
}
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取MQTT别名。
|
||||
/// </summary>
|
||||
public async Task<MqttAliasItem> GetMqttAliasById(int id)
|
||||
{
|
||||
if (_dataStorageService.MqttAliases.TryGetValue(id, out var mqttAliasItem))
|
||||
{
|
||||
return mqttAliasItem;
|
||||
}
|
||||
|
||||
// 如果内存中没有,则从数据库查询
|
||||
var mqttAlias = await _mqttAliasManagementService.AssignAliasAsync(new MqttAlias { Id = id });
|
||||
if (mqttAlias != null)
|
||||
{
|
||||
var mappedItem = _mapper.Map<MqttAliasItem>(mqttAlias);
|
||||
_dataStorageService.MqttAliases.TryAdd(mappedItem.Id, mappedItem);
|
||||
return mappedItem;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,11 @@ public class WPFDataService : IWPFDataService
|
||||
/// </summary>
|
||||
public IMqttDataService MqttDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT别名数据服务。
|
||||
/// </summary>
|
||||
public IMqttAliasDataService MqttAliasDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 日志数据服务。
|
||||
/// </summary>
|
||||
@@ -61,7 +66,8 @@ public class WPFDataService : IWPFDataService
|
||||
IMqttDataService mqttDataService,
|
||||
ILogDataService logDataService,
|
||||
IVariableTableDataService variableTableDataService,
|
||||
ITriggerDataService triggerDataService)
|
||||
ITriggerDataService triggerDataService,
|
||||
IMqttAliasDataService mqttAliasDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appDataCenterService = appDataCenterService;
|
||||
@@ -72,5 +78,6 @@ public class WPFDataService : IWPFDataService
|
||||
LogDataService = logDataService;
|
||||
VariableTableDataService = variableTableDataService;
|
||||
TriggerDataService = triggerDataService;
|
||||
MqttAliasDataService = mqttAliasDataService;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,42 +222,36 @@ namespace DMS.WPF.ViewModels
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var item in resMqttAliaes)
|
||||
|
||||
int counter = 0;
|
||||
foreach (var mqttAliasItem in resMqttAliaes)
|
||||
{
|
||||
foreach (var selectItem in selectedMqttAliaes)
|
||||
{
|
||||
if (item.Id == selectItem.Id)
|
||||
if (mqttAliasItem.Id == selectItem.Id)
|
||||
{
|
||||
selectItem.Alias = item.Alias;
|
||||
selectItem.Alias = mqttAliasItem.Alias;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 保存更改到数据服务
|
||||
var result = await _wpfDataService.MqttAliasDataService.UpdateMqttAlias(mqttAliasItem);
|
||||
if (result)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//var newAlias = dialogResult.Trim();
|
||||
|
||||
//if (string.IsNullOrEmpty(newAlias))
|
||||
//{
|
||||
// _notificationService.ShowWarn("发送名称不能为空。");
|
||||
// return;
|
||||
//}
|
||||
|
||||
//// 更新变量的发送名称
|
||||
//variableAlias.Alias = newAlias;
|
||||
|
||||
// 保存更改到数据服务
|
||||
// var result = await _wpfDataService.UpdateMqttServer(CurrentMqtt);
|
||||
//
|
||||
// if (result)
|
||||
// {
|
||||
// _notificationService.ShowSuccess($"变量 '{variableAlias.Variable.Name}' 的发送名称已更新为 '{newAlias}'");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _notificationService.ShowError("更新发送名称失败。");
|
||||
// // 如果更新失败,恢复原来的值
|
||||
// variableAlias.Alias = oldAlias;
|
||||
// }
|
||||
if (counter>0)
|
||||
{
|
||||
_notificationService.ShowSuccess($"成功修改发送名称:{counter}个");
|
||||
}
|
||||
else
|
||||
{
|
||||
_notificationService.ShowError("更新发送名称失败。");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -627,48 +627,14 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
|
||||
// 为每个变量分配MQTT别名
|
||||
foreach (var editedVariableMqtt in editedVariableMqtts)
|
||||
{
|
||||
await _mqttAliasAppService.AssignAliasAsync(
|
||||
editedVariableMqtt.VariableId,
|
||||
selectedMqtt.Id,
|
||||
editedVariableMqtt.Alias);
|
||||
|
||||
totalAffectedCount++;
|
||||
|
||||
// 更新内存中的 Variable 对象
|
||||
var originalVariable = validVariables.FirstOrDefault(v => v.Id == editedVariableMqtt.VariableId);
|
||||
if (originalVariable == null)
|
||||
var mqttAliasItem = await _wpfDataService.MqttAliasDataService.AssignAliasAsync(editedVariableMqtt);
|
||||
if (mqttAliasItem is not null)
|
||||
{
|
||||
continue;
|
||||
totalAffectedCount++;
|
||||
}
|
||||
|
||||
|
||||
if (originalVariable.MqttAliases == null)
|
||||
{
|
||||
originalVariable.MqttAliases = new();
|
||||
}
|
||||
|
||||
// 检查是否已存在该变量与该MQTT服务器的关联
|
||||
var existingVariableMqtt
|
||||
= originalVariable.MqttAliases.FirstOrDefault(vm => vm.MqttServerId == selectedMqtt.Id);
|
||||
|
||||
if (existingVariableMqtt == null)
|
||||
{
|
||||
// 如果不存在,则添加新的关联
|
||||
var variableMqtt = new MqttAliasItem
|
||||
{
|
||||
VariableId = originalVariable.Id,
|
||||
MqttServerId = selectedMqtt.Id,
|
||||
Alias = editedVariableMqtt.Alias,
|
||||
MqttServer = selectedMqtt,
|
||||
Variable = originalVariable
|
||||
};
|
||||
originalVariable.MqttAliases.Add(variableMqtt);
|
||||
selectedMqtt.VariableAliases.Add(variableMqtt);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果存在,则更新别名
|
||||
existingVariableMqtt.Alias = editedVariableMqtt.Alias;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalAffectedCount > 0)
|
||||
|
||||
Reference in New Issue
Block a user