From 7a2b67991cc42f01924ebb93253f670b8c168f8a Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 5 Oct 2025 17:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=201=20feat:=20=E6=94=B9=E8=BF=9BMQTT=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E8=AF=A6=E6=83=85=E9=A1=B5=E9=9D=A2UI?= =?UTF-8?q?=E5=92=8C=E5=8A=9F=E8=83=BD=20=20=20=202=20=20=20=203=20-=20?= =?UTF-8?q?=E5=B0=86MQTT=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E6=96=87=E6=9C=AC=E6=A1=86=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8F=AA=E8=AF=BB=E6=A8=A1=E5=BC=8F=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8TextBlock=E6=98=BE=E7=A4=BA=20=20=20=204=20-=20?= =?UTF-8?q?=E4=B8=BAMQTT=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E6=B7=BB=E5=8A=A0=E5=9B=BE=E6=A0=87=E5=92=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=97=B4=E8=B7=9D=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E8=A7=86=E8=A7=89=E6=95=88=E6=9E=9C=20=20=20=205=20-=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=A1=B6=E9=83=A8=E5=AF=BC=E8=88=AA=E6=A0=8F?= =?UTF-8?q?=E5=92=8CCommandBar=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=BF=94=E5=9B=9E=E5=88=97=E8=A1=A8=E3=80=81=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E7=AD=89=E6=93=8D=E4=BD=9C=20=20=20=206=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0Reload=E5=92=8CNavigateToMqtts=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=EF=BC=8C=E5=A2=9E=E5=BC=BA=E9=A1=B5=E9=9D=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20=20=20=207=20-=20=E9=87=8D=E6=9E=84ViewModel?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0MQTT=E7=AE=A1=E7=90=86=E3=80=81?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=AD=98=E5=82=A8=E5=92=8C=E5=AF=BC=E8=88=AA?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E4=BE=9D=E8=B5=96=20=20=20=208=20-=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=A1=B5=E9=9D=A2=E5=AF=BC=E8=88=AA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=EF=BC=8C=E6=A0=B9=E6=8D=AEID?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=AF=B9=E5=BA=94=E7=9A=84MQTT=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E4=BF=A1=E6=81=AF=20=20=20=209=20-=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8DockPanel=E5=92=8CScrollViewer=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=A1=B5=E9=9D=A2=E5=B8=83=E5=B1=80=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E7=94=A8=E6=88=B7=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/MqttServerDetailViewModel.cs | 141 +++---- DMS.WPF/Views/MqttServerDetailView.xaml | 366 +++++++++++++----- 2 files changed, 327 insertions(+), 180 deletions(-) diff --git a/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs b/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs index 4f75eae..c66cd5b 100644 --- a/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs +++ b/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs @@ -1,13 +1,11 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using DMS.Application.Interfaces.Management; +using DMS.Core.Models; +using DMS.WPF.Interfaces; +using DMS.WPF.ViewModels.Items; using Microsoft.Extensions.Logging; using System.Collections.ObjectModel; -using DMS.Core.Models; -using DMS.Services; -using DMS.WPF.Interfaces; -using DMS.WPF.Services; -using DMS.WPF.ViewModels; -using DMS.WPF.ViewModels.Items; namespace DMS.WPF.ViewModels { @@ -20,6 +18,9 @@ namespace DMS.WPF.ViewModels private readonly ILogger _logger; private readonly IDialogService _dialogService; private readonly INotificationService _notificationService; + private readonly IMqttManagementService _mqttManagementService; + private readonly IDataStorageService _dataStorageService; + private readonly INavigationService _navigationService; /// /// 当前正在编辑的MQTT服务器对象。 @@ -41,112 +42,74 @@ namespace DMS.WPF.ViewModels /// 数据服务。 /// 对话框服务。 /// 通知服务。 + /// MQTT管理服务 + /// 导航服务 public MqttServerDetailViewModel(ILogger logger, - IDialogService dialogService, INotificationService notificationService) + IDialogService dialogService, + INotificationService notificationService, + IMqttManagementService mqttManagementService, + IDataStorageService dataStorageService, + INavigationService navigationService) { _logger = logger; _dialogService = dialogService; _notificationService = notificationService; + _mqttManagementService = mqttManagementService; + this._dataStorageService = dataStorageService; + _navigationService = navigationService; } - public override void OnLoaded() - { - // if (CurrentMqtt.VariableMqtts != null) - // { - // AssociatedVariables =new ObservableCollection(CurrentMqtt.VariableMqtts) ; - // } - } - + /// - /// 保存MQTT服务器及其关联变量的更改。 + /// 重新加载当前MQTT服务器数据 /// [RelayCommand] - private async Task SaveChanges() + private async Task Reload() { - if (CurrentMqtt == null) return; - - // TODO: 实现保存逻辑。这可能涉及到更新Mqtt对象和更新VariableData对象。 - // 由于Mqtt和VariableData之间的关联可能在数据库中通过中间表维护, - // 这里需要根据实际的数据库操作来调整。 - // 例如,如果Mqtt对象本身包含关联的VariableData列表,则直接保存Mqtt对象即可。 - // 如果是多对多关系,可能需要更新中间表。 - - // 示例:假设Mqtt对象需要更新 - // await _dataServices.UpdateMqttAsync(CurrentMqtt); - - // 示例:假设变量数据也需要保存 - // foreach (var variable in AssociatedVariables.Where(v => v.IsModified)) - // { - // await _dataServices.UpdateVariableAsync(variable); - // } - - _notificationService.ShowInfo("MQTT服务器详情保存功能待实现。"); - _logger.LogInformation("Save changes for MQTT server detail initiated."); + 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; + } + } } /// - /// 从当前MQTT服务器中移除选定的变量。 + /// 导航回MQTT服务器列表页面 /// - /// 要移除的变量列表。 [RelayCommand] - private async Task RemoveVariables(System.Collections.IList variablesToRemove) + private async Task NavigateToMqtts() { - // if (CurrentMqtt == null || variablesToRemove == null || variablesToRemove.Count == 0) - // { - // NotificationHelper.ShowInfo("请选择要移除的变量。"); - // return; - // } - // - // var variablesList = variablesToRemove.Cast() - // .ToList(); - // - // var result = await _dialogService.ShowConfrimeDialog( - // "确认移除", $"确定要从MQTT服务器 '{CurrentMqtt.ServerName}' 中移除选定的 {variablesList.Count} 个变量吗?"); - // if (result != true) return; - // - // foreach (var variable in variablesList) // 使用ToList()避免在迭代时修改集合 - // { - // // 移除变量与当前MQTT服务器的关联 - // // variable.Mqtts?.Remove(CurrentMqtt); - // // // 标记变量为已修改,以便保存时更新数据库 - // // variable.IsModified = true; - // // AssociatedVariables.Remove(variable); - // // _logger.LogInformation($"Removed variable {variable.Name} from MQTT server {CurrentMqtt.Name}."); - // } - // - // // - // // 例如:await _dataServices.UpdateVariableDataAssociationsAsync(variablesToRemove); - // NotificationHelper.ShowSuccess("变量移除成功,请记得保存更改。"); + await _navigationService.NavigateToAsync(this, new NavigationParameter(nameof(MqttsViewModel))); } - /// - /// 添加变量到当前MQTT服务器。 - /// - [RelayCommand] - private async Task AddVariables() + public override Task OnNavigatedToAsync(NavigationParameter parameter) { - if (CurrentMqtt == null) return; + if (parameter == null) return Task.CompletedTask; - // TODO: 实现选择变量的对话框,让用户选择要添加的变量 - // 例如:var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync(); - // 这里只是一个占位符,实际需要一个UI来选择变量 - _notificationService.ShowInfo("添加变量功能待实现,需要一个变量选择对话框。"); - _logger.LogInformation("AddAsync variables to MQTT server initiated."); + if (_dataStorageService.MqttServers.TryGetValue(parameter.TargetId, out var mqttServerItem)) + { + CurrentMqtt = mqttServerItem; - // 假设我们已经通过对话框获取到了一些要添加的变量 - // List newVariables = ...; - // foreach (var variable in newVariables) - // { - // if (variable.Mqtts == null) variable.Mqtts = new List(); - // if (!variable.Mqtts.Any(m => m.Id == CurrentMqtt.Id)) - // { - // variable.Mqtts.AddAsync(CurrentMqtt); - // variable.IsModified = true; // 标记为已修改 - // AssociatedVariables.AddAsync(variable); - // } - // } - // NotificationHelper.ShowMessage("变量添加成功,请记得保存更改。", NotificationType.Success); + } + + + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/DMS.WPF/Views/MqttServerDetailView.xaml b/DMS.WPF/Views/MqttServerDetailView.xaml index 055ff87..7f803e2 100644 --- a/DMS.WPF/Views/MqttServerDetailView.xaml +++ b/DMS.WPF/Views/MqttServerDetailView.xaml @@ -3,110 +3,294 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> - - - - - - - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + -