添加查找当前节点下的所有变量功能
This commit is contained in:
@@ -12,6 +12,7 @@ using Opc.Ua;
|
|||||||
using Opc.Ua.Client;
|
using Opc.Ua.Client;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using static Dm.net.buffer.ByteArrayBuffer;
|
||||||
|
|
||||||
namespace DMS.WPF.ViewModels.Dialogs;
|
namespace DMS.WPF.ViewModels.Dialogs;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
|
|||||||
private OpcUaNodeItemViewModel _rootOpcUaNode;
|
private OpcUaNodeItemViewModel _rootOpcUaNode;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private ObservableCollection<VariableItemViewModel> _opcUaNodeVariables=new ObservableCollection<VariableItemViewModel>();
|
private ObservableCollection<VariableItemViewModel> _opcUaNodeVariables = new ObservableCollection<VariableItemViewModel>();
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private IList _selectedVariables = new ArrayList();
|
private IList _selectedVariables = new ArrayList();
|
||||||
@@ -40,13 +41,16 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
|
|||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool _isConnectButtonEnabled = true;
|
private bool _isConnectButtonEnabled = true;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
private OpcUaNodeItemViewModel _currentOpcUaNodeItem;
|
||||||
|
|
||||||
private Session _session;
|
private Session _session;
|
||||||
|
|
||||||
private readonly IOpcUaService _opcUaService;
|
private readonly IOpcUaService _opcUaService;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
private CancellationTokenSource _cancellationTokenSource;
|
private CancellationTokenSource _cancellationTokenSource;
|
||||||
|
|
||||||
public ImportOpcUaDialogViewModel(IOpcUaService opcUaService,IMapper mapper)
|
public ImportOpcUaDialogViewModel(IOpcUaService opcUaService, IMapper mapper)
|
||||||
{
|
{
|
||||||
this._opcUaService = opcUaService;
|
this._opcUaService = opcUaService;
|
||||||
this._mapper = mapper;
|
this._mapper = mapper;
|
||||||
@@ -68,7 +72,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
|
|||||||
|
|
||||||
if (_opcUaService.IsConnected)
|
if (_opcUaService.IsConnected)
|
||||||
{
|
{
|
||||||
IsConnected=true;
|
IsConnected = true;
|
||||||
ConnectButtonText = "已连接";
|
ConnectButtonText = "已连接";
|
||||||
IsConnectButtonEnabled = false;
|
IsConnectButtonEnabled = false;
|
||||||
}
|
}
|
||||||
@@ -76,7 +80,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
|
|||||||
|
|
||||||
// 浏览根节点
|
// 浏览根节点
|
||||||
|
|
||||||
var childrens= await _opcUaService.BrowseNode(_mapper.Map<OpcUaNode>(RootOpcUaNode));
|
var childrens = await _opcUaService.BrowseNode(_mapper.Map<OpcUaNode>(RootOpcUaNode));
|
||||||
RootOpcUaNode.Children = _mapper.Map<ObservableCollection<OpcUaNodeItemViewModel>>(childrens);
|
RootOpcUaNode.Children = _mapper.Map<ObservableCollection<OpcUaNodeItemViewModel>>(childrens);
|
||||||
|
|
||||||
|
|
||||||
@@ -137,34 +141,69 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
|
|||||||
// 加载节点的子项
|
// 加载节点的子项
|
||||||
node.IsExpanded = true;
|
node.IsExpanded = true;
|
||||||
node.IsSelected = true;
|
node.IsSelected = true;
|
||||||
|
CurrentOpcUaNodeItem = node;
|
||||||
var childrens = await _opcUaService.BrowseNode(_mapper.Map<OpcUaNode>(node));
|
await Browse(node);
|
||||||
foreach (var children in childrens)
|
|
||||||
{
|
|
||||||
var opcNodeItem = _mapper.Map<OpcUaNodeItemViewModel>(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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 节点变量失败: {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)
|
||||||
|
{
|
||||||
|
var opcNodeItem = _mapper.Map<OpcUaNodeItemViewModel>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -47,6 +47,22 @@
|
|||||||
Content="{Binding ConnectButtonText}"
|
Content="{Binding ConnectButtonText}"
|
||||||
IsEnabled="{Binding IsConnectButtonEnabled}"
|
IsEnabled="{Binding IsConnectButtonEnabled}"
|
||||||
Style="{StaticResource AccentButtonStyle}" />
|
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>
|
</StackPanel>
|
||||||
|
|
||||||
<!-- 节点树 -->
|
<!-- 节点树 -->
|
||||||
|
|||||||
Reference in New Issue
Block a user