using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using ZB.MOM.WW.OtOpcUa.Client.Shared;
using ZB.MOM.WW.OtOpcUa.Client.UI.Services;
namespace ZB.MOM.WW.OtOpcUa.Client.UI.ViewModels;
///
/// ViewModel for the OPC UA browse tree panel.
///
public class BrowseTreeViewModel : ObservableObject
{
private readonly IUiDispatcher _dispatcher;
private readonly IOpcUaClientService _service;
public BrowseTreeViewModel(IOpcUaClientService service, IUiDispatcher dispatcher)
{
_service = service;
_dispatcher = dispatcher;
}
/// Top-level nodes in the browse tree.
public ObservableCollection RootNodes { get; } = [];
///
/// Loads root nodes by browsing with a null parent.
///
public async Task LoadRootsAsync()
{
var results = await _service.BrowseAsync();
_dispatcher.Post(() =>
{
RootNodes.Clear();
foreach (var result in results)
RootNodes.Add(new TreeNodeViewModel(
result.NodeId,
result.DisplayName,
result.NodeClass,
result.HasChildren,
_service,
_dispatcher));
});
}
///
/// Clears all root nodes from the tree.
///
public void Clear()
{
RootNodes.Clear();
}
}