AOI-aware browse paths: AOI instances now fan out under directional sub-folders (Inputs/, Outputs/, InOut/) instead of a flat layout. The sub-folders only appear when at least one member carries a non-Local AoiQualifier, so plain UDT tags keep the pre-2.6 flat structure. - Add AoiQualifier enum (Local / Input / Output / InOut) + new property on AbCipStructureMember (defaults to Local). - L5K parser learns ADD_ON_INSTRUCTION_DEFINITION blocks; PARAMETER entries' Usage attribute flows through L5kMember.Usage. - L5X parser captures the Usage attribute on <Parameter> elements. - L5kIngest maps Usage strings (Input/Output/InOut) to AoiQualifier; null + unknown values map to Local. - AbCipDriver.DiscoverAsync groups directional members under Inputs / Outputs / InOut sub-folders when any member is non-Local. - Tests for L5K AOI block parsing, L5X Usage capture, ingest mapping (both formats), and AOI-vs-plain UDT discovery fan-out. Closes #234
295 lines
13 KiB
C#
295 lines
13 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class AbCipUdtMemberTests
|
|
{
|
|
[Fact]
|
|
public async Task UDT_with_declared_members_fans_out_to_member_variables()
|
|
{
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition(
|
|
Name: "Motor1",
|
|
DeviceHostAddress: "ab://10.0.0.5/1,0",
|
|
TagPath: "Motor1",
|
|
DataType: AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Speed", AbCipDataType.DInt),
|
|
new AbCipStructureMember("Running", AbCipDataType.Bool, Writable: false),
|
|
new AbCipStructureMember("SetPoint", AbCipDataType.Real, WriteIdempotent: true),
|
|
]),
|
|
],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
builder.Folders.ShouldContain(f => f.BrowseName == "Motor1");
|
|
var variables = builder.Variables.Select(v => (v.BrowseName, v.Info.FullName)).ToList();
|
|
variables.ShouldContain(("Speed", "Motor1.Speed"));
|
|
variables.ShouldContain(("Running", "Motor1.Running"));
|
|
variables.ShouldContain(("SetPoint", "Motor1.SetPoint"));
|
|
|
|
builder.Variables.Single(v => v.BrowseName == "Running").Info.SecurityClass
|
|
.ShouldBe(SecurityClassification.ViewOnly);
|
|
builder.Variables.Single(v => v.BrowseName == "SetPoint").Info.WriteIdempotent
|
|
.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UDT_members_resolvable_for_read_via_synthesised_full_reference()
|
|
{
|
|
var factory = new FakeAbCipTagFactory
|
|
{
|
|
Customise = p => p.TagName switch
|
|
{
|
|
"Motor1.Speed" => new FakeAbCipTag(p) { Value = 1800 },
|
|
"Motor1.Running" => new FakeAbCipTag(p) { Value = true },
|
|
_ => new FakeAbCipTag(p),
|
|
},
|
|
};
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Speed", AbCipDataType.DInt),
|
|
new AbCipStructureMember("Running", AbCipDataType.Bool),
|
|
]),
|
|
],
|
|
}, "drv-1", factory);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var snapshots = await drv.ReadAsync(["Motor1.Speed", "Motor1.Running"], CancellationToken.None);
|
|
|
|
snapshots[0].Value.ShouldBe(1800);
|
|
snapshots[0].StatusCode.ShouldBe(AbCipStatusMapper.Good);
|
|
snapshots[1].Value.ShouldBe(true);
|
|
snapshots[1].StatusCode.ShouldBe(AbCipStatusMapper.Good);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UDT_member_write_routes_through_synthesised_tagpath()
|
|
{
|
|
var factory = new FakeAbCipTagFactory();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("SetPoint", AbCipDataType.Real),
|
|
]),
|
|
],
|
|
}, "drv-1", factory);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var results = await drv.WriteAsync(
|
|
[new WriteRequest("Motor1.SetPoint", 42.5f)], CancellationToken.None);
|
|
|
|
results.Single().StatusCode.ShouldBe(AbCipStatusMapper.Good);
|
|
factory.Tags["Motor1.SetPoint"].Value.ShouldBe(42.5f);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UDT_member_read_write_honours_member_Writable_flag()
|
|
{
|
|
var factory = new FakeAbCipTagFactory();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Status", AbCipDataType.DInt, Writable: false),
|
|
]),
|
|
],
|
|
}, "drv-1", factory);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var results = await drv.WriteAsync(
|
|
[new WriteRequest("Motor1.Status", 1)], CancellationToken.None);
|
|
|
|
results.Single().StatusCode.ShouldBe(AbCipStatusMapper.BadNotWritable);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Structure_tag_without_members_is_emitted_as_single_variable()
|
|
{
|
|
// Fallback path: a Structure tag with no declared Members still appears as a Variable so
|
|
// downstream configuration can address it manually. This matches the "black box" note in
|
|
// AbCipTagDefinition's docstring.
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags = [new AbCipTagDefinition("OpaqueUdt", "ab://10.0.0.5/1,0", "OpaqueUdt", AbCipDataType.Structure)],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
builder.Variables.ShouldContain(v => v.BrowseName == "OpaqueUdt");
|
|
builder.Folders.ShouldNotContain(f => f.BrowseName == "OpaqueUdt");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Empty_Members_list_is_treated_like_null()
|
|
{
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags = [new AbCipTagDefinition("EmptyUdt", "ab://10.0.0.5/1,0", "E", AbCipDataType.Structure, Members: [])],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
builder.Folders.ShouldNotContain(f => f.BrowseName == "EmptyUdt");
|
|
builder.Variables.ShouldContain(v => v.BrowseName == "EmptyUdt");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AOI_typed_tag_groups_members_under_directional_subfolders()
|
|
{
|
|
// PR abcip-2.6 — when any member carries a non-Local AoiQualifier, the tag is treated
|
|
// as an AOI instance: Input / Output / InOut members get grouped under sub-folders so
|
|
// the browse tree mirrors Studio 5000's AOI parameter tabs. Plain UDT tags (every member
|
|
// Local) keep the pre-2.6 flat layout.
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition(
|
|
Name: "Valve_001",
|
|
DeviceHostAddress: "ab://10.0.0.5/1,0",
|
|
TagPath: "Valve_001",
|
|
DataType: AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Cmd", AbCipDataType.Bool, AoiQualifier: AoiQualifier.Input),
|
|
new AbCipStructureMember("Status", AbCipDataType.DInt, Writable: false, AoiQualifier: AoiQualifier.Output),
|
|
new AbCipStructureMember("Buffer", AbCipDataType.DInt, AoiQualifier: AoiQualifier.InOut),
|
|
new AbCipStructureMember("LocalVar", AbCipDataType.DInt, AoiQualifier: AoiQualifier.Local),
|
|
]),
|
|
],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
// Sub-folders for each directional bucket land in the recorder; the AOI parent folder
|
|
// and the Local member's lack of a sub-folder confirm only directional members get
|
|
// bucketed. Folder names are intentionally simple (Inputs / Outputs / InOut) — clients
|
|
// that browse "Valve_001/Inputs/Cmd" see exactly that path.
|
|
builder.Folders.Select(f => f.BrowseName).ShouldContain("Valve_001");
|
|
builder.Folders.Select(f => f.BrowseName).ShouldContain("Inputs");
|
|
builder.Folders.Select(f => f.BrowseName).ShouldContain("Outputs");
|
|
builder.Folders.Select(f => f.BrowseName).ShouldContain("InOut");
|
|
|
|
// Variables emitted under the right full names — full reference still {Tag}.{Member}
|
|
// so the read/write paths stay unchanged from the flat-UDT case.
|
|
var variables = builder.Variables.Select(v => (v.BrowseName, v.Info.FullName)).ToList();
|
|
variables.ShouldContain(("Cmd", "Valve_001.Cmd"));
|
|
variables.ShouldContain(("Status", "Valve_001.Status"));
|
|
variables.ShouldContain(("Buffer", "Valve_001.Buffer"));
|
|
variables.ShouldContain(("LocalVar", "Valve_001.LocalVar"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Plain_UDT_keeps_flat_layout_when_every_member_is_Local()
|
|
{
|
|
// Plain UDTs (no Usage attributes anywhere) stay on the pre-2.6 flat layout — no
|
|
// Inputs/Outputs/InOut sub-folders should appear since there are no directional members.
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition("Tank1", "ab://10.0.0.5/1,0", "Tank1", AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Level", AbCipDataType.Real),
|
|
new AbCipStructureMember("Pressure", AbCipDataType.Real),
|
|
]),
|
|
],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
builder.Folders.Select(f => f.BrowseName).ShouldNotContain("Inputs");
|
|
builder.Folders.Select(f => f.BrowseName).ShouldNotContain("Outputs");
|
|
builder.Folders.Select(f => f.BrowseName).ShouldNotContain("InOut");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UDT_members_mixed_with_flat_tags_coexist()
|
|
{
|
|
var builder = new RecordingBuilder();
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
|
|
Tags =
|
|
[
|
|
new AbCipTagDefinition("FlatA", "ab://10.0.0.5/1,0", "A", AbCipDataType.DInt),
|
|
new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
|
|
Members:
|
|
[
|
|
new AbCipStructureMember("Speed", AbCipDataType.DInt),
|
|
]),
|
|
new AbCipTagDefinition("FlatB", "ab://10.0.0.5/1,0", "B", AbCipDataType.Real),
|
|
],
|
|
}, "drv-1");
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.DiscoverAsync(builder, CancellationToken.None);
|
|
|
|
builder.Variables.Select(v => v.BrowseName).ShouldBe(["FlatA", "Speed", "FlatB"], ignoreOrder: true);
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
private sealed class RecordingBuilder : IAddressSpaceBuilder
|
|
{
|
|
public List<(string BrowseName, string DisplayName)> Folders { get; } = new();
|
|
public List<(string BrowseName, DriverAttributeInfo Info)> Variables { get; } = new();
|
|
|
|
public IAddressSpaceBuilder Folder(string browseName, string displayName)
|
|
{ Folders.Add((browseName, displayName)); return this; }
|
|
|
|
public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo info)
|
|
{ Variables.Add((browseName, info)); return new Handle(info.FullName); }
|
|
|
|
public void AddProperty(string _, DriverDataType __, object? ___) { }
|
|
|
|
private sealed class Handle(string fullRef) : IVariableHandle
|
|
{
|
|
public string FullReference => fullRef;
|
|
public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) => new NullSink();
|
|
}
|
|
private sealed class NullSink : IAlarmConditionSink { public void OnTransition(AlarmEventArgs args) { } }
|
|
}
|
|
}
|