2025-07-05 22:57:54 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
using DMS.Application.Events;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-09-03 18:22:01 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
2025-09-06 12:03:39 +08:00
|
|
|
|
namespace DMS.WPF.ViewModels
|
2025-07-05 22:57:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MQTT服务器详情视图模型。
|
|
|
|
|
|
/// 负责管理单个MQTT服务器的配置及其关联的变量数据。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class MqttServerDetailViewModel : ViewModelBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<MqttServerDetailViewModel> _logger;
|
|
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-09-04 19:59:35 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
private readonly IEventService _eventService;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
private readonly IMqttManagementService _mqttManagementService;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
private readonly IWPFDataService _wpfDataService;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
|
|
|
|
|
private readonly INavigationService _navigationService;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前正在编辑的MQTT服务器对象。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 18:17:56 +08:00
|
|
|
|
private MqttServerItem _currentMqtt;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 与当前MQTT服务器关联的变量数据集合。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 17:39:03 +08:00
|
|
|
|
private ObservableCollection<MqttAlias> _associatedVariables;
|
2025-07-17 17:28:12 +08:00
|
|
|
|
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger">日志服务。</param>
|
|
|
|
|
|
/// <param name="dataServices">数据服务。</param>
|
|
|
|
|
|
/// <param name="dialogService">对话框服务。</param>
|
2025-09-04 17:29:24 +08:00
|
|
|
|
/// <param name="notificationService">通知服务。</param>
|
2025-10-05 17:50:41 +08:00
|
|
|
|
/// <param name="mqttManagementService">MQTT管理服务</param>
|
|
|
|
|
|
/// <param name="navigationService">导航服务</param>
|
2025-09-09 13:35:16 +08:00
|
|
|
|
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger,
|
2025-10-05 17:50:41 +08:00
|
|
|
|
IDialogService dialogService,
|
|
|
|
|
|
INotificationService notificationService,
|
2025-10-05 19:57:58 +08:00
|
|
|
|
IEventService eventService,
|
2025-10-05 17:50:41 +08:00
|
|
|
|
IMqttManagementService mqttManagementService,
|
2025-10-05 19:57:58 +08:00
|
|
|
|
IWPFDataService wpfDataService,
|
2025-10-05 17:50:41 +08:00
|
|
|
|
IDataStorageService dataStorageService,
|
|
|
|
|
|
INavigationService navigationService)
|
2025-07-05 22:57:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_dialogService = dialogService;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService = notificationService;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
this._eventService = eventService;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
_mqttManagementService = mqttManagementService;
|
2025-10-05 19:57:58 +08:00
|
|
|
|
this._wpfDataService = wpfDataService;
|
2025-10-05 17:50:41 +08:00
|
|
|
|
this._dataStorageService = dataStorageService;
|
|
|
|
|
|
_navigationService = navigationService;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 17:50:41 +08:00
|
|
|
|
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
2025-10-05 19:57:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 编辑当前MQTT服务器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task EditMqtt()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (CurrentMqtt == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError("没有选中的MQTT服务器,无法编辑。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建编辑对话框的视图模型
|
|
|
|
|
|
var mqttDialogViewModel = new MqttDialogViewModel(CurrentMqtt)
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = "编辑MQTT服务器",
|
|
|
|
|
|
PrimaryButText = "保存修改"
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 显示对话框
|
|
|
|
|
|
var updatedMqtt = await _dialogService.ShowDialogAsync(mqttDialogViewModel);
|
|
|
|
|
|
|
|
|
|
|
|
if (updatedMqtt == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return; // 用户取消了编辑
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新MQTT服务器
|
|
|
|
|
|
var result = await _wpfDataService.MqttDataService.UpdateMqttServer(updatedMqtt);
|
|
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新当前视图模型的数据
|
|
|
|
|
|
CurrentMqtt.ServerName = updatedMqtt.ServerName;
|
|
|
|
|
|
CurrentMqtt.ServerUrl = updatedMqtt.ServerUrl;
|
|
|
|
|
|
CurrentMqtt.Port = updatedMqtt.Port;
|
|
|
|
|
|
CurrentMqtt.ClientId = updatedMqtt.ClientId;
|
|
|
|
|
|
CurrentMqtt.Username = updatedMqtt.Username;
|
|
|
|
|
|
CurrentMqtt.Password = updatedMqtt.Password;
|
|
|
|
|
|
CurrentMqtt.PublishTopic = updatedMqtt.PublishTopic;
|
|
|
|
|
|
CurrentMqtt.SubscribeTopic = updatedMqtt.SubscribeTopic;
|
|
|
|
|
|
CurrentMqtt.MessageHeader = updatedMqtt.MessageHeader;
|
|
|
|
|
|
CurrentMqtt.MessageContent = updatedMqtt.MessageContent;
|
|
|
|
|
|
CurrentMqtt.MessageFooter = updatedMqtt.MessageFooter;
|
|
|
|
|
|
|
|
|
|
|
|
_notificationService.ShowSuccess($"MQTT服务器编辑成功:{updatedMqtt.ServerName}");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError("更新MQTT服务器失败。");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "编辑MQTT服务器过程中发生错误");
|
|
|
|
|
|
_notificationService.ShowError($"编辑MQTT服务器过程中发生错误:{e.Message}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 22:57:54 +08:00
|
|
|
|
/// <summary>
|
2025-10-05 17:50:41 +08:00
|
|
|
|
/// 重新加载当前MQTT服务器数据
|
2025-07-05 22:57:54 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
2025-10-05 17:50:41 +08:00
|
|
|
|
private async Task Reload()
|
2025-07-05 22:57:54 +08:00
|
|
|
|
{
|
2025-10-05 17:50:41 +08:00
|
|
|
|
if (CurrentMqtt?.Id > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 重新加载当前MQTT服务器数据
|
|
|
|
|
|
var updatedMqtt = await _mqttManagementService.GetMqttServerByIdAsync(CurrentMqtt.Id);
|
|
|
|
|
|
if (updatedMqtt != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新CurrentMqtt的属性
|
|
|
|
|
|
CurrentMqtt.ServerName = updatedMqtt.ServerName;
|
|
|
|
|
|
CurrentMqtt.ServerUrl = updatedMqtt.ServerUrl;
|
|
|
|
|
|
CurrentMqtt.Port = updatedMqtt.Port;
|
|
|
|
|
|
CurrentMqtt.ClientId = updatedMqtt.ClientId;
|
|
|
|
|
|
CurrentMqtt.Username = updatedMqtt.Username;
|
|
|
|
|
|
CurrentMqtt.Password = updatedMqtt.Password;
|
|
|
|
|
|
CurrentMqtt.PublishTopic = updatedMqtt.PublishTopic;
|
|
|
|
|
|
CurrentMqtt.SubscribeTopic = updatedMqtt.SubscribeTopic;
|
|
|
|
|
|
CurrentMqtt.MessageHeader = updatedMqtt.MessageHeader;
|
|
|
|
|
|
CurrentMqtt.MessageContent = updatedMqtt.MessageContent;
|
|
|
|
|
|
CurrentMqtt.MessageFooter = updatedMqtt.MessageFooter;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 22:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-05 17:50:41 +08:00
|
|
|
|
/// 导航回MQTT服务器列表页面
|
2025-07-05 22:57:54 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
2025-10-05 17:50:41 +08:00
|
|
|
|
private async Task NavigateToMqtts()
|
2025-07-05 22:57:54 +08:00
|
|
|
|
{
|
2025-10-05 17:50:41 +08:00
|
|
|
|
await _navigationService.NavigateToAsync(this, new NavigationParameter(nameof(MqttsViewModel)));
|
2025-07-05 22:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-05 17:50:41 +08:00
|
|
|
|
public override Task OnNavigatedToAsync(NavigationParameter parameter)
|
2025-07-05 22:57:54 +08:00
|
|
|
|
{
|
2025-10-05 17:50:41 +08:00
|
|
|
|
if (parameter == null) return Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
if (_dataStorageService.MqttServers.TryGetValue(parameter.TargetId, out var mqttServerItem))
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentMqtt = mqttServerItem;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-05 19:57:58 +08:00
|
|
|
|
_eventService.OnVariableValueChanged += OnVariableValueChanged;
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var variableAlias=CurrentMqtt.VariableAliases.FirstOrDefault(v => v.Variable.Id == e.Variable.Id);
|
|
|
|
|
|
if (variableAlias is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
variableAlias.Variable.DisplayValue=e.Variable.DisplayValue;
|
|
|
|
|
|
variableAlias.Variable.UpdatedAt=e.Variable.UpdatedAt;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 修改变量的MQTT发送名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
2025-10-06 17:39:03 +08:00
|
|
|
|
private async Task ModifyAlias(MqttAlias variableAlias)
|
2025-10-05 19:57:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (variableAlias == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError("请选择要修改的变量项。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 创建一个用于输入新名称的简单对话框
|
|
|
|
|
|
var oldAlias = variableAlias.Alias;
|
|
|
|
|
|
InputDialogViewModel viewModel = new InputDialogViewModel("修改发送名称", "请输入新的MQTT发送名称:", oldAlias);
|
|
|
|
|
|
var dialogResult = await _dialogService.ShowDialogAsync(viewModel);
|
|
|
|
|
|
|
|
|
|
|
|
if (dialogResult != null) // 用户没有取消操作
|
|
|
|
|
|
{
|
|
|
|
|
|
var newAlias = dialogResult.Trim();
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(newAlias))
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowWarn("发送名称不能为空。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新变量的发送名称
|
|
|
|
|
|
variableAlias.Alias = newAlias;
|
|
|
|
|
|
|
|
|
|
|
|
// 保存更改到数据服务
|
2025-10-06 17:03:06 +08:00
|
|
|
|
// var result = await _wpfDataService.UpdateMqttServer(CurrentMqtt);
|
|
|
|
|
|
//
|
|
|
|
|
|
// if (result)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _notificationService.ShowSuccess($"变量 '{variableAlias.Variable.Name}' 的发送名称已更新为 '{newAlias}'");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _notificationService.ShowError("更新发送名称失败。");
|
|
|
|
|
|
// // 如果更新失败,恢复原来的值
|
|
|
|
|
|
// variableAlias.Alias = oldAlias;
|
|
|
|
|
|
// }
|
2025-10-05 19:57:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "修改变量发送名称时发生错误");
|
|
|
|
|
|
_notificationService.ShowError($"修改发送名称时发生错误:{e.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Task OnNavigatedFromAsync(NavigationParameter parameter)
|
|
|
|
|
|
{
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
2025-10-05 19:57:58 +08:00
|
|
|
|
_eventService.OnVariableValueChanged -= OnVariableValueChanged;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
2025-10-05 17:50:41 +08:00
|
|
|
|
return Task.CompletedTask;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|