using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections;
/// One node of the UNS root-picker tree (Area, Line, Equipment, or an equipment-level export leaf).
/// The persisted root-kind string (UnsArea, UnsLine, Equipment, VirtualTag, UnsTagReference, or Cluster).
/// The entity id (the cluster id for Cluster).
/// The display label (a DB value; render it, never slice it).
/// Child nodes.
public sealed record UnsPickNode(string Kind, string EntityId, string Label, IReadOnlyList Children)
{
/// A key unique within the tree.
public string Key => Kind + "|" + EntityId;
}
///
/// Builds the cluster-scoped root-picker trees (#1077) from a draft snapshot. Scoping is structural: only the
/// named cluster's entities are reachable from the returned roots. The kind strings are the ones the
/// data-export / mapping editors already persist (RootKind / LocalParentKind).
///
public static class DataRootTrees
{
private static bool Same(string? a, string? b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
/// The persisted root-kind for a Raw tree node kind, or null for kinds that are not selectable.
/// The tree node kind.
/// The kind string, or null.
public static string? RootKind(RawNodeKind kind) => kind switch
{
RawNodeKind.Cluster => "Cluster",
RawNodeKind.Folder => "RawFolder",
RawNodeKind.Driver => "RawDriver",
RawNodeKind.Device => "RawDevice",
RawNodeKind.TagGroup => "RawTagGroup",
RawNodeKind.Tag => "RawTag",
_ => null,
};
/// Builds the Raw tree (Cluster, Folder, Driver, Device, TagGroup, Tag) for one cluster.
/// The draft snapshot.
/// The cluster whose entities are shown.
/// Whether Tag leaves are included (export roots may be a tag; mapping parents may not).
/// A single cluster root with everything eagerly materialised.
public static RawNode BuildRaw(DraftSnapshot snapshot, string clusterId, bool includeTags = true)
{
var root = new RawNode { Kind = RawNodeKind.Cluster, Key = "cluster:" + clusterId, DisplayName = "Cluster root: " + clusterId, ClusterId = clusterId, EntityId = clusterId, Expanded = true };
RawNode Make(RawNodeKind kind, string prefix, string id, string name) =>
new() { Kind = kind, Key = prefix + id, DisplayName = name, ClusterId = clusterId, EntityId = id };
var folders = snapshot.RawFolders.Where(x => Same(x.ClusterId, clusterId)).ToList();
var folderNodes = folders.ToDictionary(x => x.RawFolderId, x => Make(RawNodeKind.Folder, "folder:", x.RawFolderId, x.Name), StringComparer.OrdinalIgnoreCase);
foreach (var f in folders)
{
var self = folderNodes[f.RawFolderId];
var parent = f.ParentRawFolderId is { } p && folderNodes.TryGetValue(p, out var pn) && pn != self ? pn : root;
parent.Children.Add(self);
}
var drivers = snapshot.DriverInstances.Where(x => Same(x.ClusterId, clusterId)).ToList();
var driverNodes = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var d in drivers)
{
var node = Make(RawNodeKind.Driver, "drv:", d.DriverInstanceId, d.Name);
driverNodes[d.DriverInstanceId] = node;
(d.RawFolderId is { } fid && folderNodes.TryGetValue(fid, out var fn) ? fn : root).Children.Add(node);
}
var deviceNodes = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var d in snapshot.Devices.Where(x => driverNodes.ContainsKey(x.DriverInstanceId)))
{
var node = Make(RawNodeKind.Device, "dev:", d.DeviceId, d.Name);
deviceNodes[d.DeviceId] = node;
driverNodes[d.DriverInstanceId].Children.Add(node);
}
var groupNodes = new Dictionary(StringComparer.OrdinalIgnoreCase);
var groups = snapshot.TagGroups.Where(x => deviceNodes.ContainsKey(x.DeviceId)).ToList();
foreach (var g in groups) groupNodes[g.TagGroupId] = Make(RawNodeKind.TagGroup, "grp:", g.TagGroupId, g.Name);
foreach (var g in groups)
{
var self = groupNodes[g.TagGroupId];
var parent = g.ParentTagGroupId is { } pid && groupNodes.TryGetValue(pid, out var gp) && gp != self ? gp : deviceNodes[g.DeviceId];
parent.Children.Add(self);
}
if (includeTags)
{
foreach (var t in snapshot.Tags.Where(x => deviceNodes.ContainsKey(x.DeviceId)))
{
var parent = t.TagGroupId is { } gid && groupNodes.TryGetValue(gid, out var g) ? g : deviceNodes[t.DeviceId];
parent.Children.Add(Make(RawNodeKind.Tag, "tag:", t.TagId, t.Name));
}
}
foreach (var n in folderNodes.Values.Concat(driverNodes.Values).Concat(deviceNodes.Values).Concat(groupNodes.Values).Append(root)) n.ChildCount = n.Children.Count;
return root;
}
/// Builds the UNS tree (Cluster, Area, Line, Equipment, optional equipment-level leaves) for one cluster.
/// The draft snapshot.
/// The cluster whose entities are shown.
/// Whether equipment-level virtual tags / tag references are shown (export roots only).
/// The cluster root node.
public static UnsPickNode BuildUns(DraftSnapshot snapshot, string clusterId, bool equipmentLeaves)
{
var areaNodes = new List();
foreach (var area in snapshot.UnsAreas.Where(x => Same(x.ClusterId, clusterId)))
{
var lineNodes = new List();
foreach (var line in snapshot.UnsLines.Where(x => Same(x.UnsAreaId, area.UnsAreaId)))
{
var equipmentNodes = new List();
foreach (var e in snapshot.Equipment.Where(x => Same(x.UnsLineId, line.UnsLineId)))
{
var leaves = new List();
if (equipmentLeaves)
{
leaves.AddRange(snapshot.VirtualTags.Where(x => Same(x.EquipmentId, e.EquipmentId)).Select(x => new UnsPickNode("VirtualTag", x.VirtualTagId, "Virtual tag: " + x.Name, [])));
leaves.AddRange(snapshot.UnsTagReferences.Where(x => Same(x.EquipmentId, e.EquipmentId)).Select(x => new UnsPickNode("UnsTagReference", x.UnsTagReferenceId, "Tag reference: " + x.UnsTagReferenceId, [])));
}
equipmentNodes.Add(new UnsPickNode("Equipment", e.EquipmentId, e.Name, leaves));
}
lineNodes.Add(new UnsPickNode("UnsLine", line.UnsLineId, line.Name, equipmentNodes));
}
areaNodes.Add(new UnsPickNode("UnsArea", area.UnsAreaId, area.Name, lineNodes));
}
return new UnsPickNode("Cluster", clusterId, "Cluster root: " + clusterId, areaNodes);
}
}