重构MQTT事件处理和激活状态管理功能

This commit is contained in:
2025-10-05 14:45:41 +08:00
parent b96101dea6
commit 80ea47e627
13 changed files with 70 additions and 198 deletions

View File

@@ -82,7 +82,6 @@ public class DataEventService : IDataEventService
break;
case MqttServerPropertyType.IsConnect:
mqttServerItem.IsConnect=e.MqttServer.IsConnect;
_notificationService.ShowSuccess($"MQTT服务器{mqttServerItem.ServerName},连接发生了变化,状态:{e.MqttServer.IsConnect}");
break;
case MqttServerPropertyType.Username:
break;

View File

@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces.Management;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
@@ -16,8 +17,8 @@ public class MqttDataService : IMqttDataService
{
private readonly IMapper _mapper;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IMqttManagementService _mqttManagementService;
private readonly IDataStorageService _dataStorageService;
private readonly IMqttAppService _mqttAppService;
/// <summary>
@@ -25,12 +26,12 @@ public class MqttDataService : IMqttDataService
/// </summary>
/// <param name="mapper">AutoMapper 实例。</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;
_appDataStorageService = appDataStorageService;
_mqttManagementService = mqttManagementService;
_dataStorageService = dataStorageService;
_mqttAppService = mqttAppService;
}
/// <summary>
@@ -60,11 +61,8 @@ public class MqttDataService : IMqttDataService
/// </summary>
public async Task<MqttServerItemViewModel> AddMqttServer(MqttServerItemViewModel mqttServer)
{
var dto = _mapper.Map<MqttServerDto>(mqttServer);
var id = await _mqttAppService.CreateMqttServerAsync(dto);
dto.Id = id;
var mqttServerItem = _mapper.Map<MqttServerItemViewModel>(dto);
var mqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map<MqttServerDto>(mqttServer));
var mqttServerItem = _mapper.Map<MqttServerItemViewModel>(mqttServerDto);
_dataStorageService.MqttServers.Add(mqttServerItem.Id,mqttServerItem);
return mqttServerItem;
@@ -76,7 +74,7 @@ public class MqttDataService : IMqttDataService
public async Task<bool> UpdateMqttServer(MqttServerItemViewModel mqttServer)
{
var dto = _mapper.Map<MqttServerDto>(mqttServer);
await _mqttAppService.UpdateMqttServerAsync(dto);
await _mqttManagementService.UpdateMqttServerAsync(dto);
return true;
}
@@ -85,7 +83,7 @@ public class MqttDataService : IMqttDataService
/// </summary>
public async Task<bool> DeleteMqttServer(MqttServerItemViewModel mqttServer)
{
await _mqttAppService.DeleteMqttServerAsync(mqttServer.Id);
await _mqttManagementService.DeleteMqttServerAsync(mqttServer.Id);
_dataStorageService.MqttServers.Remove(mqttServer.Id);
return true;
}

View File

@@ -64,12 +64,36 @@ public partial class MqttsViewModel : ViewModelBase
_navigationService = navigationService;
_notificationService = notificationService;
// Set static services for MqttServerItemViewModel
MqttServerItemViewModel.SetServices(_wpfDataService, _notificationService);
_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>
/// 添加MQTT服务器命令。
/// </summary>

View File

@@ -45,11 +45,13 @@
<!-- Row 0: Header with Name and ToggleSwitch -->
<DockPanel Grid.Row="0" Margin="0,0,0,10">
<ui:ToggleSwitch
<ToggleButton
DockPanel.Dock="Right"
IsOn="{Binding IsActive}"
OffContent="停止"
OnContent="启动" />
Command="{Binding DataContext.ToggleIsActiveCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding }"
IsChecked="{Binding IsActive}"
Style="{StaticResource ToggleButtonSwitch}" />
<TextBlock
VerticalAlignment="Center"
FontSize="20"