添加查找当前节点下的所有变量功能

This commit is contained in:
2025-09-02 15:44:26 +08:00
parent 14144e865c
commit b37a1fcaf5
2 changed files with 83 additions and 28 deletions

View File

@@ -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;
@@ -40,6 +41,9 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
[ObservableProperty]
private bool _isConnectButtonEnabled = true;
[ObservableProperty]
private OpcUaNodeItemViewModel _currentOpcUaNodeItem;
private Session _session;
private readonly IOpcUaService _opcUaService;
@@ -137,7 +141,17 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
// 加载节点的子项
node.IsExpanded = true;
node.IsSelected = true;
CurrentOpcUaNodeItem = node;
await Browse(node);
}
catch (Exception 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<OpcUaNode>(node));
foreach (var children in childrens)
{
@@ -154,17 +168,42 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
});
}
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)
{
NlogHelper.Error($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex);
NotificationHelper.ShowError($"加载 OPC UA 节点变量失败: {node.NodeId} - {ex.Message}", ex);
NotificationHelper.ShowError($"加载 OPC UA 节点变量失败: {CurrentOpcUaNodeItem.NodeId} - {ex.Message}", ex);
}
}
}

View File

@@ -47,6 +47,22 @@
Content="{Binding ConnectButtonText}"
IsEnabled="{Binding IsConnectButtonEnabled}"
Style="{StaticResource AccentButtonStyle}" />
<Button
x:Name="FindVariablesButton"
Margin="10,0,0,0"
Command="{Binding FindCurrentNodeVariablesCommand}"
Content="查找当前节点下的所有变量">
<Button.Style>
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
<!-- 节点树 -->