201ca44088
RawTree gains an opt-in single-select NodePickMode (any entity kind); new UnsRootPicker and DataRootPickerModal; cluster-scoped trees built from the draft snapshot. Stored kinds and validation unchanged.
131 lines
7.4 KiB
C#
131 lines
7.4 KiB
C#
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections;
|
|
|
|
/// <summary>One node of the UNS root-picker tree (Area, Line, Equipment, or an equipment-level export leaf).</summary>
|
|
/// <param name="Kind">The persisted root-kind string (<c>UnsArea</c>, <c>UnsLine</c>, <c>Equipment</c>, <c>VirtualTag</c>, <c>UnsTagReference</c>, or <c>Cluster</c>).</param>
|
|
/// <param name="EntityId">The entity id (the cluster id for <c>Cluster</c>).</param>
|
|
/// <param name="Label">The display label (a DB value; render it, never slice it).</param>
|
|
/// <param name="Children">Child nodes.</param>
|
|
public sealed record UnsPickNode(string Kind, string EntityId, string Label, IReadOnlyList<UnsPickNode> Children)
|
|
{
|
|
/// <summary>A key unique within the tree.</summary>
|
|
public string Key => Kind + "|" + EntityId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 (<c>RootKind</c> / <c>LocalParentKind</c>).
|
|
/// </summary>
|
|
public static class DataRootTrees
|
|
{
|
|
private static bool Same(string? a, string? b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
|
|
|
/// <summary>The persisted root-kind for a Raw tree node kind, or null for kinds that are not selectable.</summary>
|
|
/// <param name="kind">The tree node kind.</param>
|
|
/// <returns>The kind string, or null.</returns>
|
|
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,
|
|
};
|
|
|
|
/// <summary>Builds the Raw tree (Cluster, Folder, Driver, Device, TagGroup, Tag) for one cluster.</summary>
|
|
/// <param name="snapshot">The draft snapshot.</param>
|
|
/// <param name="clusterId">The cluster whose entities are shown.</param>
|
|
/// <param name="includeTags">Whether Tag leaves are included (export roots may be a tag; mapping parents may not).</param>
|
|
/// <returns>A single cluster root with everything eagerly materialised.</returns>
|
|
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<string, RawNode>(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<string, RawNode>(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<string, RawNode>(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;
|
|
}
|
|
|
|
/// <summary>Builds the UNS tree (Cluster, Area, Line, Equipment, optional equipment-level leaves) for one cluster.</summary>
|
|
/// <param name="snapshot">The draft snapshot.</param>
|
|
/// <param name="clusterId">The cluster whose entities are shown.</param>
|
|
/// <param name="equipmentLeaves">Whether equipment-level virtual tags / tag references are shown (export roots only).</param>
|
|
/// <returns>The cluster root node.</returns>
|
|
public static UnsPickNode BuildUns(DraftSnapshot snapshot, string clusterId, bool equipmentLeaves)
|
|
{
|
|
var areaNodes = new List<UnsPickNode>();
|
|
foreach (var area in snapshot.UnsAreas.Where(x => Same(x.ClusterId, clusterId)))
|
|
{
|
|
var lineNodes = new List<UnsPickNode>();
|
|
foreach (var line in snapshot.UnsLines.Where(x => Same(x.UnsAreaId, area.UnsAreaId)))
|
|
{
|
|
var equipmentNodes = new List<UnsPickNode>();
|
|
foreach (var e in snapshot.Equipment.Where(x => Same(x.UnsLineId, line.UnsLineId)))
|
|
{
|
|
var leaves = new List<UnsPickNode>();
|
|
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);
|
|
}
|
|
}
|