From b37a1fcaf5e21bc11c76e42f3843677b3be0a004 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 2 Sep 2025 15:44:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9F=A5=E6=89=BE=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E8=8A=82=E7=82=B9=E4=B8=8B=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dialogs/ImportOpcUaDialogViewModel.cs | 95 +++++++++++++------ DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml | 16 ++++ 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/DMS.WPF/ViewModels/Dialogs/ImportOpcUaDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/ImportOpcUaDialogViewModel.cs index da7a962..47f38eb 100644 --- a/DMS.WPF/ViewModels/Dialogs/ImportOpcUaDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/ImportOpcUaDialogViewModel.cs @@ -12,6 +12,7 @@ using Opc.Ua; using Opc.Ua.Client; using System.Collections; using System.Collections.ObjectModel; +using static Dm.net.buffer.ByteArrayBuffer; namespace DMS.WPF.ViewModels.Dialogs; @@ -24,7 +25,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase _opcUaNodeVariables=new ObservableCollection(); + private ObservableCollection _opcUaNodeVariables = new ObservableCollection(); [ObservableProperty] private IList _selectedVariables = new ArrayList(); @@ -40,13 +41,16 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase(RootOpcUaNode)); + var childrens = await _opcUaService.BrowseNode(_mapper.Map(RootOpcUaNode)); RootOpcUaNode.Children = _mapper.Map>(childrens); @@ -126,10 +130,10 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase(node)); - foreach (var children in childrens) - { - var opcNodeItem = _mapper.Map(children); - if (children.NodeClass == NodeClass.Variable) - { - OpcUaNodeVariables.Add(new VariableItemViewModel - { - Name = children.DisplayName, // 修正:使用子节点的显示名称 - OpcUaNodeId = children.NodeId.ToString(), - Protocol = ProtocolType.OpcUa, - CSharpDataType=children.DataType, - IsActive = true // 默认选中 - }); - } - else - { - node.Children.Add(opcNodeItem); - } - } + CurrentOpcUaNodeItem = node; + await Browse(node); } catch (Exception ex) { - NlogHelper.Error($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex); NotificationHelper.ShowError($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex); } } + private async Task Browse(OpcUaNodeItemViewModel node, bool isScan = false) + { + var childrens = await _opcUaService.BrowseNode(_mapper.Map(node)); + foreach (var children in childrens) + { + var opcNodeItem = _mapper.Map(children); + if (children.NodeClass == NodeClass.Variable) + { + OpcUaNodeVariables.Add(new VariableItemViewModel + { + Name = children.DisplayName, // 修正:使用子节点的显示名称 + OpcUaNodeId = children.NodeId.ToString(), + Protocol = ProtocolType.OpcUa, + CSharpDataType = children.DataType, + IsActive = true // 默认选中 + }); + } + else + { + if (node.Children.FirstOrDefault(n => n.NodeId == opcNodeItem.NodeId) == null) + { + node.Children.Add(opcNodeItem); + } + if (isScan) + { + Browse(opcNodeItem); + } + } + } + } + + [RelayCommand] + private async Task FindCurrentNodeVariables() + { + try + { + if (CurrentOpcUaNodeItem == null) + { + NotificationHelper.ShowError($"请先选择左边的节点,然后再查找变量。"); + return; + } + + OpcUaNodeVariables.Clear(); + + // 加载节点的子项 + CurrentOpcUaNodeItem.IsExpanded = true; + CurrentOpcUaNodeItem.IsSelected = true; + + await Browse(CurrentOpcUaNodeItem, true); + } + catch (Exception ex) + { + NotificationHelper.ShowError($"加载 OPC UA 节点变量失败: {CurrentOpcUaNodeItem.NodeId} - {ex.Message}", ex); + } + } } \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml b/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml index aa499a6..7aa8085 100644 --- a/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml +++ b/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml @@ -47,6 +47,22 @@ Content="{Binding ConnectButtonText}" IsEnabled="{Binding IsConnectButtonEnabled}" Style="{StaticResource AccentButtonStyle}" /> +