bugfix:修复Mqtt服务器详情页变量值不更新的问题,修复修改Mqtt别名后台不更新的问题,重新调整了别名的架构
This commit is contained in:
@@ -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