Group all 69 projects into category subfolders under src/ and tests/ so the Rider Solution Explorer mirrors the module structure. Folders: Core, Server, Drivers (with a nested Driver CLIs subfolder), Client, Tooling. - Move every project folder on disk with git mv (history preserved as renames). - Recompute relative paths in 57 .csproj files: cross-category ProjectReferences, the lib/ HintPath+None refs in Driver.Historian.Wonderware, and the external mxaccessgw refs in Driver.Galaxy and its test project. - Rebuild ZB.MOM.WW.OtOpcUa.slnx with nested solution folders. - Re-prefix project paths in functional scripts (e2e, compliance, smoke SQL, integration, install). Build green (0 errors); unit tests pass. Docs left for a separate pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
102 lines
4.5 KiB
C#
102 lines
4.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
|
|
|
|
/// <summary>
|
|
/// #142 multi-unit-ID gateway support: per-tag UnitId override + IPerCallHostResolver +
|
|
/// wire-level routing of UnitId in the MBAP header per-PDU.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ModbusMultiUnitTests
|
|
{
|
|
private sealed class UnitCapturingTransport : IModbusTransport
|
|
{
|
|
public readonly List<byte> SeenUnitIds = new();
|
|
public Task ConnectAsync(CancellationToken ct) => Task.CompletedTask;
|
|
public Task<byte[]> SendAsync(byte unitId, byte[] pdu, CancellationToken ct)
|
|
{
|
|
SeenUnitIds.Add(unitId);
|
|
switch (pdu[0])
|
|
{
|
|
case 0x03: case 0x04:
|
|
{
|
|
var qty = (ushort)((pdu[3] << 8) | pdu[4]);
|
|
var resp = new byte[2 + qty * 2];
|
|
resp[0] = pdu[0]; resp[1] = (byte)(qty * 2);
|
|
return Task.FromResult(resp);
|
|
}
|
|
default: return Task.FromResult(new byte[] { pdu[0], 0, 0 });
|
|
}
|
|
}
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PerTag_UnitId_Routes_To_Correct_Slave_In_MBAP()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var tagSlave1 = new ModbusTagDefinition("S1Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 1);
|
|
var tagSlave5 = new ModbusTagDefinition("S5Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 5);
|
|
var opts = new ModbusDriverOptions { Host = "f", UnitId = 99, Tags = [tagSlave1, tagSlave5],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.ReadAsync(["S1Temp", "S5Temp"], CancellationToken.None);
|
|
|
|
// Two reads: one for slave 1, one for slave 5. Driver-level UnitId=99 must NOT appear.
|
|
fake.SeenUnitIds.ShouldContain((byte)1);
|
|
fake.SeenUnitIds.ShouldContain((byte)5);
|
|
fake.SeenUnitIds.ShouldNotContain((byte)99);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Tag_Without_UnitId_Falls_Back_To_DriverLevel()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var tag = new ModbusTagDefinition("T", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16); // no UnitId override
|
|
var opts = new ModbusDriverOptions { Host = "f", UnitId = 7, Tags = [tag],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.ReadAsync(["T"], CancellationToken.None);
|
|
|
|
fake.SeenUnitIds.ShouldContain((byte)7);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IPerCallHostResolver_Returns_Per_Slave_Host_String()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var t1 = new ModbusTagDefinition("S1Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 1);
|
|
var t5 = new ModbusTagDefinition("S5Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 5);
|
|
var opts = new ModbusDriverOptions { Host = "10.1.2.3", Port = 502, Tags = [t1, t5],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
// The pipeline keys breakers on these strings; distinct slave IDs must produce distinct
|
|
// host strings so per-PLC isolation works.
|
|
var resolver = (IPerCallHostResolver)drv;
|
|
resolver.ResolveHost("S1Temp").ShouldBe("10.1.2.3:502/unit1");
|
|
resolver.ResolveHost("S5Temp").ShouldBe("10.1.2.3:502/unit5");
|
|
resolver.ResolveHost("S1Temp").ShouldNotBe(resolver.ResolveHost("S5Temp"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IPerCallHostResolver_Unknown_Tag_Falls_Back_To_HostName()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var opts = new ModbusDriverOptions { Host = "10.1.2.3", Port = 502, Tags = [],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var resolver = (IPerCallHostResolver)drv;
|
|
resolver.ResolveHost("never-defined").ShouldBe("10.1.2.3:502");
|
|
}
|
|
}
|