fix(modbus): equipment tags carry unitId; ResolveHost keys per-unit via the resolver (CONV-4)

This commit is contained in:
Joseph Doherty
2026-07-13 12:29:13 -04:00
parent 9262dd25b7
commit e69d3b3b0f
4 changed files with 84 additions and 4 deletions
@@ -55,10 +55,14 @@ public static class ModbusEquipmentTagParser
// "writable" defaults to true when absent (today's value) — makes read-only equipment tags
// authorable (UNDER-6). Node-level authz remains the effective write gate.
var writable = TagConfigJson.ReadWritable(root);
// CONV-4: optional per-tag unitId (0255). Absent ⇒ null ⇒ the driver-level UnitId via
// ModbusDriver.ResolveUnitId, so multi-unit equipment tags key their own per-host breaker.
// Scope: unitId ONLY — the remaining parser-strictness gaps are R2-11's.
var unitId = ReadOptionalByte(root, "unitId");
def = new ModbusTagDefinition(
Name: reference, Region: region, Address: (ushort)address, DataType: dataType,
Writable: writable, ByteOrder: byteOrder, BitIndex: bitIndex, StringLength: stringLength,
ArrayCount: arrayCount);
ArrayCount: arrayCount, UnitId: unitId);
return true;
}
catch (JsonException) { return false; }
@@ -109,6 +113,10 @@ public static class ModbusEquipmentTagParser
=> o.TryGetProperty(name, out var e) && e.ValueKind == JsonValueKind.Number
&& e.TryGetInt32(out var v) ? v : 0;
private static byte? ReadOptionalByte(JsonElement o, string name)
=> o.TryGetProperty(name, out var e) && e.ValueKind == JsonValueKind.Number
&& e.TryGetInt32(out var v) && v is >= 0 and <= 255 ? (byte)v : null;
private static bool ReadBool(JsonElement o, string name)
=> o.TryGetProperty(name, out var e) && e.ValueKind == JsonValueKind.True;
}
@@ -140,10 +140,19 @@ public sealed class ModbusDriver
WriteHealth(new DriverHealth(DriverState.Degraded, current.LastSuccessfulRead, ex.Message));
}
/// <inheritdoc />
/// <summary>
/// Resolve a reference to the per-slave host key (<c>host:port/unitN</c>) that keys its
/// per-host resilience (bulkhead / circuit breaker). CONV-4: routes through
/// <see cref="EquipmentTagRefResolver{TDef}"/> so an equipment-tag reference (raw TagConfig
/// JSON) keys its OWN authored unit — via the tag's optional <c>unitId</c> or the driver
/// default — rather than the single-slave fallback. The resolver caches parses, so this adds
/// no per-call cost after first use.
/// </summary>
/// <param name="fullReference">The tag reference (authored name or equipment-tag JSON).</param>
/// <returns>The per-slave host key.</returns>
public string ResolveHost(string fullReference)
{
if (_tagsByName.TryGetValue(fullReference, out var tag))
if (_resolver.TryResolve(fullReference, out var tag))
return BuildSlaveHostName(ResolveUnitId(tag));
// Unknown reference — fall back to driver-instance host (single-slave behaviour).
return HostName;