重构MQTT事件处理和激活状态管理功能
This commit is contained in:
@@ -55,17 +55,6 @@ public interface IEventService
|
|||||||
|
|
||||||
#region MQTT事件
|
#region MQTT事件
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// MQTT连接状态改变事件
|
|
||||||
/// </summary>
|
|
||||||
event EventHandler<MqttConnectionChangedEventArgs> MqttConnectionChanged;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 触发MQTT连接状态改变事件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">事件发送者</param>
|
|
||||||
/// <param name="e">MQTT连接状态改变事件参数</param>
|
|
||||||
void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MQTT服务器改变事件
|
/// MQTT服务器改变事件
|
||||||
@@ -107,7 +96,6 @@ public interface IEventService
|
|||||||
/// <param name="e">变量值改变事件参数</param>
|
/// <param name="e">变量值改变事件参数</param>
|
||||||
void RaiseVariableChanged(object sender, VariableChangedEventArgs e);
|
void RaiseVariableChanged(object sender, VariableChangedEventArgs e);
|
||||||
|
|
||||||
void RaiseVariableActiveChanged(object sender,VariablesActiveChangedEventArgs e);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量导入变量事件
|
/// 批量导入变量事件
|
||||||
@@ -121,11 +109,6 @@ public interface IEventService
|
|||||||
/// <param name="e">批量导入变量事件参数</param>
|
/// <param name="e">批量导入变量事件参数</param>
|
||||||
void RaiseBatchImportVariables(object sender, BatchImportVariablesEventArgs e);
|
void RaiseBatchImportVariables(object sender, BatchImportVariablesEventArgs e);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 变量启停改变事件
|
|
||||||
/// </summary>
|
|
||||||
event EventHandler<VariablesActiveChangedEventArgs> OnVariableActiveChanged;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据加载完成事件
|
/// 数据加载完成事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ public class MappingProfile : Profile
|
|||||||
|
|
||||||
// MqttServer 映射
|
// MqttServer 映射
|
||||||
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
|
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
|
||||||
|
CreateMap<MqttServerDto, MqttServerDto>().ReverseMap();
|
||||||
|
|
||||||
// VariableMqttAlias 映射
|
// VariableMqttAlias 映射
|
||||||
CreateMap<VariableMqttAlias, VariableMqttAliasDto>()
|
CreateMap<VariableMqttAlias, VariableMqttAliasDto>()
|
||||||
|
|||||||
@@ -85,15 +85,6 @@ public class EventService : IEventService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 变量启停改变事件
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler<VariablesActiveChangedEventArgs> OnVariableActiveChanged;
|
|
||||||
public void RaiseVariableActiveChanged(object sender, VariablesActiveChangedEventArgs e)
|
|
||||||
{
|
|
||||||
OnVariableActiveChanged?.Invoke(sender, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量导入变量事件
|
/// 批量导入变量事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -128,22 +119,6 @@ public class EventService : IEventService
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region MQTT事件
|
#region MQTT事件
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// MQTT连接状态改变事件
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler<MqttConnectionChangedEventArgs> MqttConnectionChanged;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 触发MQTT连接状态改变事件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">事件发送者</param>
|
|
||||||
/// <param name="e">MQTT连接状态改变事件参数</param>
|
|
||||||
public void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
MqttConnectionChanged?.Invoke(sender, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MQTT服务器改变事件
|
/// MQTT服务器改变事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -105,12 +105,6 @@ public class MqttManagementService : IMqttManagementService
|
|||||||
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, property));
|
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, property));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有任何属性发生变化,至少触发一次更新事件
|
|
||||||
if (changedProperties.Count == 0)
|
|
||||||
{
|
|
||||||
_eventService.RaiseMqttServerChanged(
|
|
||||||
this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mMqttServerDto, MqttServerPropertyType.All));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
namespace DMS.Core.Events;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// MQTT连接状态改变事件参数
|
|
||||||
/// </summary>
|
|
||||||
public class MqttConnectionChangedEventArgs : EventArgs
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// MQTT服务器ID
|
|
||||||
/// </summary>
|
|
||||||
public int MqttServerId { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// MQTT服务器名称
|
|
||||||
/// </summary>
|
|
||||||
public string MqttServerName { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 旧连接状态
|
|
||||||
/// </summary>
|
|
||||||
public bool OldConnectionStatus { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 新连接状态
|
|
||||||
/// </summary>
|
|
||||||
public bool NewConnectionStatus { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 状态改变时间
|
|
||||||
/// </summary>
|
|
||||||
public DateTime ChangeTime { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化MqttConnectionChangedEventArgs类的新实例
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mqttServerId">MQTT服务器ID</param>
|
|
||||||
/// <param name="mqttServerName">MQTT服务器名称</param>
|
|
||||||
/// <param name="oldStatus">旧连接状态</param>
|
|
||||||
/// <param name="newStatus">新连接状态</param>
|
|
||||||
public MqttConnectionChangedEventArgs(int mqttServerId, string mqttServerName, bool oldStatus, bool newStatus)
|
|
||||||
{
|
|
||||||
MqttServerId = mqttServerId;
|
|
||||||
MqttServerName = mqttServerName;
|
|
||||||
OldConnectionStatus = oldStatus;
|
|
||||||
NewConnectionStatus = newStatus;
|
|
||||||
ChangeTime = DateTime.Now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
namespace DMS.Core.Events;
|
|
||||||
|
|
||||||
public class VariablesActiveChangedEventArgs: EventArgs
|
|
||||||
{
|
|
||||||
|
|
||||||
public List<int> VariableIds { get; }
|
|
||||||
|
|
||||||
public int DeviceId{get;}
|
|
||||||
|
|
||||||
public bool NewStatus { get; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public VariablesActiveChangedEventArgs(List<int> variableIds,int deviceId, bool newStatus)
|
|
||||||
{
|
|
||||||
VariableIds = variableIds;
|
|
||||||
DeviceId=deviceId;
|
|
||||||
NewStatus = newStatus;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -186,9 +186,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_mqttServers.Clear();
|
_mqttServers.Clear();
|
||||||
|
|
||||||
// 从数据服务中心获取所有激活的MQTT服务器
|
// 从数据服务中心获取所有激活的MQTT服务器
|
||||||
var mqttServerDtos = _appDataStorageService.MqttServers.Values
|
var mqttServerDtos = _appDataStorageService.MqttServers.Values.ToList();
|
||||||
.Where(m => m.IsActive)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
foreach (var mqttServerDto in mqttServerDtos)
|
foreach (var mqttServerDto in mqttServerDtos)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -49,11 +49,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_eventService.OnMqttServerChanged += OnMqttServerChanged;
|
_eventService.OnMqttServerChanged += OnMqttServerChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 标志是否正在处理事件,用于防止递归调用
|
|
||||||
/// </summary>
|
|
||||||
private readonly ConcurrentDictionary<int, bool> _isProcessingUpdate = new();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化服务管理器
|
/// 初始化服务管理器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -81,10 +76,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_mqttContexts.AddOrUpdate(mqttServer.Id, context, (key, oldValue) => context);
|
_mqttContexts.AddOrUpdate(mqttServer.Id, context, (key, oldValue) => context);
|
||||||
_logger.LogInformation("已添加MQTT服务器 {MqttServerId} 到监控列表", mqttServer.Id);
|
_logger.LogInformation("已添加MQTT服务器 {MqttServerId} 到监控列表", mqttServer.Id);
|
||||||
|
|
||||||
// 使用AutoMapper触发MQTT服务器改变事件
|
|
||||||
var mqttServerDto = _mapper.Map<MqttServerDto>(mqttServer);
|
|
||||||
|
|
||||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(Core.Enums.ActionChangeType.Added, mqttServerDto));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -97,10 +88,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
await DisconnectMqttServerAsync(mqttServerId, cancellationToken);
|
await DisconnectMqttServerAsync(mqttServerId, cancellationToken);
|
||||||
_logger.LogInformation("已移除MQTT服务器 {MqttServerId} 的监控", mqttServerId);
|
_logger.LogInformation("已移除MQTT服务器 {MqttServerId} 的监控", mqttServerId);
|
||||||
|
|
||||||
// 使用AutoMapper触发MQTT服务器删除事件
|
|
||||||
var mqttServerDto = _mapper.Map<MqttServerDto>(context.MqttServerConfig);
|
|
||||||
|
|
||||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(Core.Enums.ActionChangeType.Deleted, mqttServerDto));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,14 +339,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 防止同一服务器的递归更新
|
|
||||||
if (_isProcessingUpdate.ContainsKey(e.MqttServer.Id) && _isProcessingUpdate[e.MqttServer.Id])
|
|
||||||
{
|
|
||||||
_logger.LogDebug("正在处理服务器 {MqttServerId} 的更新,跳过重复事件", e.MqttServer.Id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_isProcessingUpdate[e.MqttServer.Id] = true;
|
|
||||||
|
|
||||||
_logger.LogDebug("处理MQTT服务器变更事件: 服务器ID={MqttServerId}, 变更类型={ChangeType}, 变更属性={PropertyType}",
|
_logger.LogDebug("处理MQTT服务器变更事件: 服务器ID={MqttServerId}, 变更类型={ChangeType}, 变更属性={PropertyType}",
|
||||||
e.MqttServer.Id, e.ChangeType, e.PropertyType);
|
e.MqttServer.Id, e.ChangeType, e.PropertyType);
|
||||||
@@ -382,10 +361,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
_logger.LogError(ex, "处理MQTT服务器变更事件时发生错误: 服务器ID={MqttServerId}, 变更类型={ChangeType}",
|
_logger.LogError(ex, "处理MQTT服务器变更事件时发生错误: 服务器ID={MqttServerId}, 变更类型={ChangeType}",
|
||||||
e.MqttServer.Id, e.ChangeType);
|
e.MqttServer.Id, e.ChangeType);
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
_isProcessingUpdate.TryRemove(e.MqttServer.Id, out _);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -474,10 +449,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
await ReconnectMqttServerAsync(mqttServer.Id, CancellationToken.None);
|
await ReconnectMqttServerAsync(mqttServer.Id, CancellationToken.None);
|
||||||
break;
|
break;
|
||||||
case MqttServerPropertyType.IsActive:
|
case MqttServerPropertyType.IsActive:
|
||||||
// 检查当前激活状态和新激活状态是否一致
|
|
||||||
if (context.MqttServerConfig.IsActive != mqttServer.IsActive)
|
|
||||||
{
|
|
||||||
context.MqttServerConfig.IsActive = mqttServer.IsActive;
|
|
||||||
|
|
||||||
if (mqttServer.IsActive)
|
if (mqttServer.IsActive)
|
||||||
{
|
{
|
||||||
@@ -489,7 +460,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
// 激活状态变为false,断开服务器连接
|
// 激活状态变为false,断开服务器连接
|
||||||
await DisconnectMqttServerAsync(mqttServer.Id, CancellationToken.None);
|
await DisconnectMqttServerAsync(mqttServer.Id, CancellationToken.None);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MqttServerPropertyType.SubscribeTopic:
|
case MqttServerPropertyType.SubscribeTopic:
|
||||||
context.MqttServerConfig.SubscribeTopic = mqttServer.SubscribeTopic;
|
context.MqttServerConfig.SubscribeTopic = mqttServer.SubscribeTopic;
|
||||||
@@ -557,9 +527,6 @@ namespace DMS.Infrastructure.Services.Mqtt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理处理更新状态的字典
|
|
||||||
_isProcessingUpdate.Clear();
|
|
||||||
|
|
||||||
_logger.LogInformation("MQTT服务管理器已释放资源");
|
_logger.LogInformation("MQTT服务管理器已释放资源");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,33 +45,32 @@ namespace DMS.Infrastructure.Services.S7
|
|||||||
_deviceContexts = new ConcurrentDictionary<int, S7DeviceContext>();
|
_deviceContexts = new ConcurrentDictionary<int, S7DeviceContext>();
|
||||||
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
|
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
|
||||||
|
|
||||||
_eventService.OnVariableActiveChanged += OnVariableActiveChanged;
|
|
||||||
_eventService.OnBatchImportVariables += OnBatchImportVariables;
|
_eventService.OnBatchImportVariables += OnBatchImportVariables;
|
||||||
_eventService.OnVariableChanged += OnVariableChanged;
|
_eventService.OnVariableChanged += OnVariableChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnVariableActiveChanged(object? sender, VariablesActiveChangedEventArgs e)
|
//private void OnVariableActiveChanged(object? sender, VariablesActiveChangedEventArgs e)
|
||||||
{
|
//{
|
||||||
if (_deviceContexts.TryGetValue(e.DeviceId, out var s7DeviceContext))
|
// if (_deviceContexts.TryGetValue(e.DeviceId, out var s7DeviceContext))
|
||||||
{
|
// {
|
||||||
|
|
||||||
var variables = _appDataStorageService.Variables.Values.Where(v => e.VariableIds.Contains(v.Id))
|
// var variables = _appDataStorageService.Variables.Values.Where(v => e.VariableIds.Contains(v.Id))
|
||||||
.ToList();
|
// .ToList();
|
||||||
foreach (var variable in variables)
|
// foreach (var variable in variables)
|
||||||
{
|
// {
|
||||||
if (e.NewStatus)
|
// if (e.NewStatus)
|
||||||
{
|
// {
|
||||||
// 变量启用,从轮询列表中添加变量
|
// // 变量启用,从轮询列表中添加变量
|
||||||
s7DeviceContext.Variables.AddOrUpdate(variable.S7Address,variable, (key, oldValue) => variable);
|
// s7DeviceContext.Variables.AddOrUpdate(variable.S7Address,variable, (key, oldValue) => variable);
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
// 变量停用,从轮询列表中移除变量
|
// // 变量停用,从轮询列表中移除变量
|
||||||
s7DeviceContext.Variables.Remove(variable.S7Address, out _);
|
// s7DeviceContext.Variables.Remove(variable.S7Address, out _);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化服务管理器
|
/// 初始化服务管理器
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ public class DataEventService : IDataEventService
|
|||||||
break;
|
break;
|
||||||
case MqttServerPropertyType.IsConnect:
|
case MqttServerPropertyType.IsConnect:
|
||||||
mqttServerItem.IsConnect=e.MqttServer.IsConnect;
|
mqttServerItem.IsConnect=e.MqttServer.IsConnect;
|
||||||
_notificationService.ShowSuccess($"MQTT服务器:{mqttServerItem.ServerName},连接发生了变化,状态:{e.MqttServer.IsConnect}");
|
|
||||||
break;
|
break;
|
||||||
case MqttServerPropertyType.Username:
|
case MqttServerPropertyType.Username:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Application.Interfaces.Database;
|
using DMS.Application.Interfaces.Database;
|
||||||
|
using DMS.Application.Interfaces.Management;
|
||||||
using DMS.WPF.Interfaces;
|
using DMS.WPF.Interfaces;
|
||||||
using DMS.WPF.ViewModels.Items;
|
using DMS.WPF.ViewModels.Items;
|
||||||
|
|
||||||
@@ -16,8 +17,8 @@ public class MqttDataService : IMqttDataService
|
|||||||
{
|
{
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
private readonly IAppDataStorageService _appDataStorageService;
|
private readonly IAppDataStorageService _appDataStorageService;
|
||||||
|
private readonly IMqttManagementService _mqttManagementService;
|
||||||
private readonly IDataStorageService _dataStorageService;
|
private readonly IDataStorageService _dataStorageService;
|
||||||
private readonly IMqttAppService _mqttAppService;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -25,12 +26,12 @@ public class MqttDataService : IMqttDataService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mapper">AutoMapper 实例。</param>
|
/// <param name="mapper">AutoMapper 实例。</param>
|
||||||
/// <param name="mqttAppService">MQTT应用服务实例。</param>
|
/// <param name="mqttAppService">MQTT应用服务实例。</param>
|
||||||
public MqttDataService(IMapper mapper, IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService, IMqttAppService mqttAppService)
|
public MqttDataService(IMapper mapper, IAppDataStorageService appDataStorageService,IMqttManagementService mqttManagementService, IDataStorageService dataStorageService)
|
||||||
{
|
{
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
_appDataStorageService = appDataStorageService;
|
_appDataStorageService = appDataStorageService;
|
||||||
|
_mqttManagementService = mqttManagementService;
|
||||||
_dataStorageService = dataStorageService;
|
_dataStorageService = dataStorageService;
|
||||||
_mqttAppService = mqttAppService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -60,11 +61,8 @@ public class MqttDataService : IMqttDataService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<MqttServerItemViewModel> AddMqttServer(MqttServerItemViewModel mqttServer)
|
public async Task<MqttServerItemViewModel> AddMqttServer(MqttServerItemViewModel mqttServer)
|
||||||
{
|
{
|
||||||
var dto = _mapper.Map<MqttServerDto>(mqttServer);
|
var mqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map<MqttServerDto>(mqttServer));
|
||||||
var id = await _mqttAppService.CreateMqttServerAsync(dto);
|
var mqttServerItem = _mapper.Map<MqttServerItemViewModel>(mqttServerDto);
|
||||||
dto.Id = id;
|
|
||||||
|
|
||||||
var mqttServerItem = _mapper.Map<MqttServerItemViewModel>(dto);
|
|
||||||
_dataStorageService.MqttServers.Add(mqttServerItem.Id,mqttServerItem);
|
_dataStorageService.MqttServers.Add(mqttServerItem.Id,mqttServerItem);
|
||||||
|
|
||||||
return mqttServerItem;
|
return mqttServerItem;
|
||||||
@@ -76,7 +74,7 @@ public class MqttDataService : IMqttDataService
|
|||||||
public async Task<bool> UpdateMqttServer(MqttServerItemViewModel mqttServer)
|
public async Task<bool> UpdateMqttServer(MqttServerItemViewModel mqttServer)
|
||||||
{
|
{
|
||||||
var dto = _mapper.Map<MqttServerDto>(mqttServer);
|
var dto = _mapper.Map<MqttServerDto>(mqttServer);
|
||||||
await _mqttAppService.UpdateMqttServerAsync(dto);
|
await _mqttManagementService.UpdateMqttServerAsync(dto);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +83,7 @@ public class MqttDataService : IMqttDataService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<bool> DeleteMqttServer(MqttServerItemViewModel mqttServer)
|
public async Task<bool> DeleteMqttServer(MqttServerItemViewModel mqttServer)
|
||||||
{
|
{
|
||||||
await _mqttAppService.DeleteMqttServerAsync(mqttServer.Id);
|
await _mqttManagementService.DeleteMqttServerAsync(mqttServer.Id);
|
||||||
_dataStorageService.MqttServers.Remove(mqttServer.Id);
|
_dataStorageService.MqttServers.Remove(mqttServer.Id);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,12 +64,36 @@ public partial class MqttsViewModel : ViewModelBase
|
|||||||
_navigationService = navigationService;
|
_navigationService = navigationService;
|
||||||
_notificationService = notificationService;
|
_notificationService = notificationService;
|
||||||
|
|
||||||
// Set static services for MqttServerItemViewModel
|
|
||||||
MqttServerItemViewModel.SetServices(_wpfDataService, _notificationService);
|
|
||||||
|
|
||||||
_mqttServeise = _dataStorageService.MqttServers.ToNotifyCollectionChanged(x=>x.Value);
|
_mqttServeise = _dataStorageService.MqttServers.ToNotifyCollectionChanged(x=>x.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
public async Task ToggleIsActive(MqttServerItemViewModel mqttServerItem)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (mqttServerItem == null)
|
||||||
|
{
|
||||||
|
_notificationService.ShowError("没有选择任何MQTT服务器,请选择后再点击切换激活状态");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 更新到数据存储
|
||||||
|
await _wpfDataService.MqttDataService.UpdateMqttServer(mqttServerItem);
|
||||||
|
|
||||||
|
// 显示操作结果
|
||||||
|
var statusText = mqttServerItem.IsActive ? "已启用" : "已停用";
|
||||||
|
_notificationService.ShowSuccess($"MQTT服务器 {mqttServerItem.ServerName} 已{statusText}");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "切换MQTT服务器激活状态时发生错误");
|
||||||
|
_notificationService.ShowError($"切换MQTT服务器激活状态时发生错误:{e.Message}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加MQTT服务器命令。
|
/// 添加MQTT服务器命令。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -45,11 +45,13 @@
|
|||||||
|
|
||||||
<!-- Row 0: Header with Name and ToggleSwitch -->
|
<!-- Row 0: Header with Name and ToggleSwitch -->
|
||||||
<DockPanel Grid.Row="0" Margin="0,0,0,10">
|
<DockPanel Grid.Row="0" Margin="0,0,0,10">
|
||||||
<ui:ToggleSwitch
|
<ToggleButton
|
||||||
DockPanel.Dock="Right"
|
DockPanel.Dock="Right"
|
||||||
IsOn="{Binding IsActive}"
|
Command="{Binding DataContext.ToggleIsActiveCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
|
||||||
OffContent="停止"
|
CommandParameter="{Binding }"
|
||||||
OnContent="启动" />
|
IsChecked="{Binding IsActive}"
|
||||||
|
Style="{StaticResource ToggleButtonSwitch}" />
|
||||||
|
|
||||||
<TextBlock
|
<TextBlock
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
FontSize="20"
|
FontSize="20"
|
||||||
|
|||||||
Reference in New Issue
Block a user