feat(opcua): compose Equipment VirtualTag plans from VirtualTag+Script rows
This commit is contained in:
@@ -175,6 +175,8 @@ public static class Phase7Composer
|
||||
/// <param name="scriptedAlarms">The scripted alarms.</param>
|
||||
/// <param name="tags">The tags.</param>
|
||||
/// <param name="namespaces">The namespaces.</param>
|
||||
/// <param name="virtualTags">The Equipment-namespace virtual (calculated) tags. <c>null</c> = none.</param>
|
||||
/// <param name="scripts">The scripts joined to <paramref name="virtualTags"/> by ScriptId for the expression. <c>null</c> = none.</param>
|
||||
/// <returns>The composition result.</returns>
|
||||
public static Phase7CompositionResult Compose(
|
||||
IReadOnlyList<UnsArea> unsAreas,
|
||||
@@ -183,8 +185,12 @@ public static class Phase7Composer
|
||||
IReadOnlyList<DriverInstance> driverInstances,
|
||||
IReadOnlyList<ScriptedAlarm> scriptedAlarms,
|
||||
IReadOnlyList<Tag> tags,
|
||||
IReadOnlyList<Namespace> namespaces)
|
||||
IReadOnlyList<Namespace> namespaces,
|
||||
IReadOnlyList<VirtualTag>? virtualTags = null,
|
||||
IReadOnlyList<Script>? scripts = null)
|
||||
{
|
||||
var vtags = virtualTags ?? Array.Empty<VirtualTag>();
|
||||
var scripts2 = scripts ?? Array.Empty<Script>();
|
||||
var areas = unsAreas
|
||||
.OrderBy(a => a.UnsAreaId, StringComparer.Ordinal)
|
||||
.Select(a => new UnsAreaProjection(a.UnsAreaId, a.Name))
|
||||
@@ -258,9 +264,31 @@ public static class Phase7Composer
|
||||
FullName: ExtractTagFullName(t.TagConfig)))
|
||||
.ToList();
|
||||
|
||||
// Equipment VirtualTags = each VirtualTag joined to its Script (by ScriptId) for the
|
||||
// expression source. DependencyRefs = the distinct ctx.GetTag("…") literals the
|
||||
// VirtualTagActor subscribes to. VirtualTag has no FolderPath today → "".
|
||||
var scriptsById = scripts2.ToDictionary(s => s.ScriptId, StringComparer.Ordinal);
|
||||
var equipmentVirtualTags = vtags
|
||||
.OrderBy(v => v.EquipmentId, StringComparer.Ordinal)
|
||||
.ThenBy(v => v.Name, StringComparer.Ordinal)
|
||||
.Select(v =>
|
||||
{
|
||||
var src = scriptsById.TryGetValue(v.ScriptId, out var s) ? s.SourceCode : string.Empty;
|
||||
return new EquipmentVirtualTagPlan(
|
||||
VirtualTagId: v.VirtualTagId,
|
||||
EquipmentId: v.EquipmentId,
|
||||
FolderPath: string.Empty,
|
||||
Name: v.Name,
|
||||
DataType: v.DataType,
|
||||
Expression: src,
|
||||
DependencyRefs: ExtractDependencyRefs(src));
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new Phase7CompositionResult(areas, lines, nodes, plans, alarms, galaxyTags)
|
||||
{
|
||||
EquipmentTags = equipmentTags,
|
||||
EquipmentVirtualTags = equipmentVirtualTags,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -290,4 +318,24 @@ public static class Phase7Composer
|
||||
catch (JsonException) { /* fall through to raw blob */ }
|
||||
return tagConfig;
|
||||
}
|
||||
|
||||
private static readonly System.Text.RegularExpressions.Regex GetTagRefRegex =
|
||||
new(@"ctx\s*\.\s*GetTag\s*\(\s*""([^""]+)""\s*\)", System.Text.RegularExpressions.RegexOptions.Compiled);
|
||||
|
||||
/// <summary>Distinct <c>ctx.GetTag("ref")</c> string literals in a VirtualTag script source,
|
||||
/// in first-seen order — the dependency refs the VirtualTagActor subscribes to.</summary>
|
||||
/// <param name="scriptSource">The VirtualTag's script source.</param>
|
||||
/// <returns>The distinct dependency refs in first-seen order.</returns>
|
||||
private static IReadOnlyList<string> ExtractDependencyRefs(string scriptSource)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(scriptSource)) return Array.Empty<string>();
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
||||
var result = new List<string>();
|
||||
foreach (System.Text.RegularExpressions.Match m in GetTagRefRegex.Matches(scriptSource))
|
||||
{
|
||||
var r = m.Groups[1].Value;
|
||||
if (seen.Add(r)) result.Add(r);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user