Merge driver follow-ups B: MQTT ISO-8601 DateTime writes (#1044), FOCAS short payload BadTypeMismatch (#1036), AbCip alloc trims (#1032), AbLegacy address memo + bounded RMW gates + identity eviction (#1031), Sql docs (#1042/#1043)
This commit is contained in:
@@ -51,6 +51,13 @@ public sealed class RequestScheduler : IAsyncDisposable
|
||||
/// <summary>This request's node in the scheduler queue, or null once dequeued.</summary>
|
||||
public LinkedListNode<Pending>? Node { get; set; }
|
||||
|
||||
/// <summary>The scheduler this request was queued on; the cancellation callback's state (P-24: passing
|
||||
/// this object as the state avoids boxing a tuple per cancellable request).</summary>
|
||||
public RequestScheduler? Owner { get; init; }
|
||||
|
||||
/// <summary>The caller's token, reported as the cancellation cause.</summary>
|
||||
public CancellationToken Token { get; init; }
|
||||
|
||||
/// <summary>Stores the waiter's registration; disposes it at once if the waiter already completed.</summary>
|
||||
/// <param name="registration">The cancellation registration to track.</param>
|
||||
/// <param name="owner">The scheduler whose live-registration count is maintained.</param>
|
||||
@@ -171,7 +178,11 @@ public sealed class RequestScheduler : IAsyncDisposable
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
if (ct.IsCancellationRequested) return Task.FromCanceled<CipReply>(ct);
|
||||
|
||||
var p = new Pending(request, exclusive || expectedReplyBytes < 0, Math.Max(expectedReplyBytes, UnknownReply));
|
||||
var p = new Pending(request, exclusive || expectedReplyBytes < 0, Math.Max(expectedReplyBytes, UnknownReply))
|
||||
{
|
||||
Owner = this,
|
||||
Token = ct,
|
||||
};
|
||||
lock (_lock)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
@@ -183,14 +194,15 @@ public sealed class RequestScheduler : IAsyncDisposable
|
||||
Interlocked.Increment(ref _liveRegistrations);
|
||||
var registration = ct.Register(static state =>
|
||||
{
|
||||
var (self, pending, token) = ((RequestScheduler, Pending, CancellationToken))state!;
|
||||
var pending = (Pending)state!;
|
||||
var self = pending.Owner!;
|
||||
Interlocked.Increment(ref self._cancellationCallbacksRun);
|
||||
lock (self._lock)
|
||||
{
|
||||
if (pending.Node?.List is not null) self._queue.Remove(pending.Node);
|
||||
}
|
||||
pending.Cancel(token, self);
|
||||
}, (this, p, ct));
|
||||
pending.Cancel(pending.Token, self);
|
||||
}, p);
|
||||
p.SetRegistration(registration, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ public sealed class LogixTagClient(RequestScheduler scheduler, ICipChannel chann
|
||||
byte[] path, ushort elements, CancellationToken ct, int elementBytes = 0)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(path);
|
||||
var reply = await _scheduler.SendAsync(LogixServices.ReadTagFragmented(path, elements, 0), ct,
|
||||
var reply = await _scheduler.SendAsync(FirstReadRequest(path, elements), ct,
|
||||
expectedReplyBytes: ExpectedReadReplyBytes(elements, elementBytes, 0)).ConfigureAwait(false);
|
||||
var (type, handle, first) = LogixServices.ParseReadReply(reply); // throws on any status but 0x00/0x06
|
||||
|
||||
@@ -96,6 +96,27 @@ public sealed class LogixTagClient(RequestScheduler scheduler, ICipChannel chann
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One cached offset-0 read request: the constant bytes a steady poll resends every cycle (P-24).</summary>
|
||||
private sealed record FirstRead(byte[] Path, ushort Elements, byte[] Request);
|
||||
|
||||
private volatile FirstRead? _firstRead;
|
||||
|
||||
/// <summary>
|
||||
/// The offset-0 Read Tag Fragmented request for <paramref name="path"/>. A tag's runtime reads the same encoded
|
||||
/// path (the same array instance) with the same element count every poll, so the request is built once and
|
||||
/// reused; a different path instance or count rebuilds it. The request bytes are never mutated after queueing
|
||||
/// (the scheduler only reads them), so sharing one array across successive reads is safe.
|
||||
/// </summary>
|
||||
private byte[] FirstReadRequest(byte[] path, ushort elements)
|
||||
{
|
||||
var cached = _firstRead;
|
||||
if (cached is not null && ReferenceEquals(cached.Path, path) && cached.Elements == elements)
|
||||
return cached.Request;
|
||||
var request = LogixServices.ReadTagFragmented(path, elements, 0);
|
||||
_firstRead = new FirstRead(path, elements, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/// <summary>Appends <paramref name="chunk"/> to a pooled buffer, growing it (rent bigger, copy, return) as needed.</summary>
|
||||
private static void Append(ref byte[] buffer, ref int count, ReadOnlySpan<byte> chunk)
|
||||
{
|
||||
|
||||
@@ -535,7 +535,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
results[i] = new DataValueSnapshot(null, AbLegacyStatusMapper.BadConfigurationError, null, now);
|
||||
continue;
|
||||
}
|
||||
var parsed = AbLegacyAddress.TryParse(def.Address, device.Options.PlcFamily);
|
||||
var parsed = device.ParseAddress(def);
|
||||
if (parsed is null)
|
||||
{
|
||||
WarnMalformedOnce(device, def);
|
||||
@@ -686,6 +686,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
{
|
||||
var def = item.Def;
|
||||
var reference = item.Reference;
|
||||
IAbLegacyTagRuntime? usedRuntime = null; // the handle this call actually used (identity-checked eviction)
|
||||
try
|
||||
{
|
||||
if (item.Parsed.IsIndirect)
|
||||
@@ -733,9 +734,10 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
return (OpVerdict.Neutral, null);
|
||||
}
|
||||
runtime = live;
|
||||
usedRuntime = live;
|
||||
await runtime.ReadAsync(ct).ConfigureAwait(false);
|
||||
status = runtime.GetStatus();
|
||||
var parsed = AbLegacyAddress.TryParse(def.Address, device.Options.PlcFamily);
|
||||
var parsed = device.ParseAddress(def);
|
||||
if (status != 0)
|
||||
value = null;
|
||||
else if (IsArrayTag(def))
|
||||
@@ -771,8 +773,9 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
WarnRefusalOnce(device, def, "read", $"unclassifiable reply {(AbStatus)status}", results[item.Index].StatusCode);
|
||||
return (OpVerdict.Opaque, $"unclassifiable reply {(AbStatus)status} reading {reference}");
|
||||
}
|
||||
// Evict the stale handle so the next call re-creates it (mirrors AbCip).
|
||||
EvictRuntime(device, def.Name);
|
||||
// Evict the stale handle so the next call re-creates it (mirrors AbCip) — identity-checked: a sibling
|
||||
// that reconnected under the gate after we released it must not lose its fresh handle.
|
||||
EvictUsedRuntime(device, def.Name, usedRuntime);
|
||||
if (!device.FirstNonZeroStatusLogged)
|
||||
{
|
||||
device.FirstNonZeroStatusLogged = true;
|
||||
@@ -828,7 +831,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Transport exception — evict so the next read creates a fresh handle (mirrors AbCip).
|
||||
EvictRuntime(device, def.Name);
|
||||
EvictUsedRuntime(device, def.Name, usedRuntime);
|
||||
results[item.Index] = new DataValueSnapshot(null, AbLegacyStatusMapper.BadCommunicationError, null, now);
|
||||
return (OpVerdict.Fault, ex.Message);
|
||||
}
|
||||
@@ -920,9 +923,10 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
continue;
|
||||
}
|
||||
|
||||
IAbLegacyTagRuntime? usedRuntime = null; // the handle this write actually used
|
||||
try
|
||||
{
|
||||
var parsed = AbLegacyAddress.TryParse(def.Address, device.Options.PlcFamily);
|
||||
var parsed = device.ParseAddress(def);
|
||||
|
||||
// A malformed address is a config error, not a comms fault (BadNodeIdUnknown, warned once per tag).
|
||||
if (parsed is null)
|
||||
@@ -1052,6 +1056,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
continue; // the index moved under us (N3): not written, no health evidence
|
||||
}
|
||||
runtime = live;
|
||||
usedRuntime = live;
|
||||
if (arrayValue is not null) runtime.EncodeArray(arrayFileType, arrayValue);
|
||||
else runtime.EncodeValue(def.DataType, parsed.BitIndex, w.Value);
|
||||
await runtime.WriteAsync(cancellationToken).ConfigureAwait(false);
|
||||
@@ -1064,7 +1069,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
|
||||
results[i] = new WriteResult(status == 0 ? AbLegacyStatusMapper.Good : AbLegacyStatusMapper.MapStatus(status));
|
||||
// A no-reply status evicts the stale handle so the next call re-creates it (mirrors AbCip).
|
||||
if (IsNoReply((AbStatus)status) && status != 0) EvictRuntime(device, def.Name);
|
||||
if (IsNoReply((AbStatus)status) && status != 0) EvictUsedRuntime(device, def.Name, usedRuntime);
|
||||
RecordWriteStatus(device, def, status, ref outcome);
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
@@ -1111,7 +1116,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Transport exception — evict so the next write/read creates a fresh handle (mirrors AbCip).
|
||||
EvictRuntime(device, def.Name);
|
||||
EvictUsedRuntime(device, def.Name, usedRuntime);
|
||||
device.RecordCommsFault(Clock());
|
||||
results[i] = new WriteResult(AbLegacyStatusMapper.BadCommunicationError);
|
||||
outcome.FaultMessage ??= ex.Message;
|
||||
@@ -1371,14 +1376,12 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
device.IndirectLastParent[indirectName] = parentName;
|
||||
if (previous is not null && !string.Equals(previous, parentName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var oldGate = device.GetRmwLock(previous);
|
||||
await oldGate.WaitAsync(ct).ConfigureAwait(false);
|
||||
try { EvictParentRuntime(device, previous); } finally { oldGate.Release(); }
|
||||
var oldHold = await device.EnterRmwAsync(previous, ct).ConfigureAwait(false);
|
||||
try { EvictParentRuntime(device, previous); } finally { oldHold.Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
var rmwLock = device.GetRmwLock(parentName);
|
||||
await rmwLock.WaitAsync(ct).ConfigureAwait(false);
|
||||
var rmwHold = await device.EnterRmwAsync(parentName, ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var parentRuntime = await EnsureParentRuntimeAsync(device, parentName, ct).ConfigureAwait(false);
|
||||
@@ -1410,7 +1413,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
}
|
||||
finally
|
||||
{
|
||||
rmwLock.Release();
|
||||
rmwHold.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1501,8 +1504,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
|
||||
// A bit-addressed index reads its parent word and decodes the bit, like the RMW path does.
|
||||
var runtimeName = (inner with { BitIndex = null }).ToLibplctagName();
|
||||
var gate = device.GetRmwLock(runtimeName);
|
||||
await gate.WaitAsync(ct).ConfigureAwait(false);
|
||||
var gate = await device.EnterRmwAsync(runtimeName, ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
IAbLegacyTagRuntime runtime;
|
||||
@@ -1562,7 +1564,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
}
|
||||
finally
|
||||
{
|
||||
gate.Release();
|
||||
gate.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1741,7 +1743,11 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
private void WarnRefusalOnce(DeviceState device, AbLegacyTagDefinition def, string operation, string reason,
|
||||
uint status = AbLegacyStatusMapper.BadNotSupported)
|
||||
{
|
||||
if (!device.RefusalLogged.TryAdd($"{operation}:{def.Name}", 0)) return;
|
||||
// Keyed by the AUTHORED tag: an indirect resolution's name ("tag=>N7:42") changes with the index value, so
|
||||
// keying on it grew the set (and re-warned) per value a sweeping index took (review 2026-12-02 P-23, L7).
|
||||
var arrow = def.Name.IndexOf("=>", StringComparison.Ordinal);
|
||||
var authored = arrow > 0 ? def.Name[..arrow] : def.Name;
|
||||
if (!device.RefusalLogged.TryAdd($"{operation}:{authored}", 0)) return;
|
||||
// The status the operation is actually answered with (review 07 r0 M8) — not always BadNotSupported.
|
||||
_logger.LogWarning(
|
||||
"AbLegacy {Operation} refused (0x{Status:X8}). Driver={DriverInstanceId} Device={DeviceHostAddress} Reference={Reference} Address={Address} Reason={Reason}",
|
||||
@@ -1764,7 +1770,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
{
|
||||
if (device.Runtimes.TryGetValue(def.Name, out var existing)) return existing;
|
||||
|
||||
var parsed = AbLegacyAddress.TryParse(def.Address, device.Options.PlcFamily)
|
||||
var parsed = device.ParseAddress(def)
|
||||
?? throw new InvalidOperationException(
|
||||
$"AbLegacy tag '{def.Name}' has malformed Address '{def.Address}' for PLC family {device.Options.PlcFamily}.");
|
||||
var createParams = CreateParamsFor(device, def, parsed);
|
||||
@@ -1813,6 +1819,16 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evicts the handle an operation used and saw fail — by identity when the operation got as far as holding one
|
||||
/// (a sibling's fresh reconnect is left alone), by name only when it failed before it held any (#1031).
|
||||
/// </summary>
|
||||
private static void EvictUsedRuntime(DeviceState device, string tagName, IAbLegacyTagRuntime? used)
|
||||
{
|
||||
if (used is not null) EvictRuntime(device, tagName, used);
|
||||
else EvictRuntime(device, tagName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="EvictRuntime(DeviceState, string)"/>, but only if the cached runtime for <paramref name="tagName"/>
|
||||
/// is still <paramref name="expected"/> — the one the caller actually used and saw fail. Without the identity
|
||||
@@ -1976,15 +1992,102 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
}
|
||||
}
|
||||
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, SemaphoreSlim> _rmwLocks = new();
|
||||
internal sealed class RmwGate
|
||||
{
|
||||
public readonly SemaphoreSlim Semaphore = new(1, 1);
|
||||
public int Users; // holders + waiters, under _rmwGatesLock
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, RmwGate> _rmwGates = new(StringComparer.Ordinal);
|
||||
private readonly object _rmwGatesLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates the read-modify-write lock for the specified parent name.
|
||||
/// Takes the read-modify-write gate of a parent word. The gate is reference-counted (holders + waiters) and
|
||||
/// removed the moment its last user leaves, so a sweeping indirect bit-write index no longer leaves one
|
||||
/// <see cref="SemaphoreSlim"/> behind per word it ever resolved (review 2026-12-02 P-23, residual L7).
|
||||
/// Removal at zero users is safe: nobody holds or awaits the gate, so a later caller's fresh gate cannot
|
||||
/// race another RMW of the same word — the gate a waiter already counted on is never removed.
|
||||
/// </summary>
|
||||
/// <param name="parentName">The parent name.</param>
|
||||
/// <returns>The semaphore slim for the parent.</returns>
|
||||
public SemaphoreSlim GetRmwLock(string parentName) =>
|
||||
_rmwLocks.GetOrAdd(parentName, _ => new SemaphoreSlim(1, 1));
|
||||
/// <param name="parentName">The parent word's runtime key.</param>
|
||||
/// <param name="ct">Cancels the wait.</param>
|
||||
/// <returns>The held gate; dispose it to release.</returns>
|
||||
public async Task<RmwHold> EnterRmwAsync(string parentName, CancellationToken ct)
|
||||
{
|
||||
RmwGate gate;
|
||||
lock (_rmwGatesLock)
|
||||
{
|
||||
if (!_rmwGates.TryGetValue(parentName, out gate!)) _rmwGates[parentName] = gate = new RmwGate();
|
||||
gate.Users++;
|
||||
}
|
||||
try
|
||||
{
|
||||
await gate.Semaphore.WaitAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Leave(parentName, gate);
|
||||
throw;
|
||||
}
|
||||
return new RmwHold(this, parentName, gate);
|
||||
}
|
||||
|
||||
private void Leave(string parentName, RmwGate gate)
|
||||
{
|
||||
lock (_rmwGatesLock)
|
||||
{
|
||||
if (--gate.Users == 0
|
||||
&& _rmwGates.TryGetValue(parentName, out var current) && ReferenceEquals(current, gate))
|
||||
_rmwGates.Remove(parentName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A held read-modify-write gate; <see cref="Dispose"/> releases it (once).</summary>
|
||||
public sealed class RmwHold
|
||||
{
|
||||
private DeviceState? _owner;
|
||||
private readonly string _name;
|
||||
private readonly RmwGate _gate;
|
||||
|
||||
internal RmwHold(DeviceState owner, string name, RmwGate gate)
|
||||
{
|
||||
_owner = owner;
|
||||
_name = name;
|
||||
_gate = gate;
|
||||
}
|
||||
|
||||
/// <summary>Releases the gate; further calls are no-ops.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
var owner = Interlocked.Exchange(ref _owner, null);
|
||||
if (owner is null) return;
|
||||
_gate.Semaphore.Release();
|
||||
owner.Leave(_name, _gate);
|
||||
}
|
||||
}
|
||||
|
||||
// Parsed address per AUTHORED tag (P-24): every poll used to re-parse each address two or three times.
|
||||
// Bounded by the authored tag set — an indirect resolution's synthetic name never enters it. A cached
|
||||
// entry is honoured only while it was parsed from the same address text and family.
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, (string Address, AbLegacyPlcFamily Family, AbLegacyAddress? Parsed)> _parsedAddresses
|
||||
= new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// The parsed form of <paramref name="def"/>'s address under this device's PLC family, memoised per
|
||||
/// authored tag. A resolved indirect definition (its name carries <c>=></c>) is parsed afresh, uncached.
|
||||
/// </summary>
|
||||
/// <param name="def">The tag definition.</param>
|
||||
/// <returns>The parsed address, or <see langword="null"/> when it is malformed for the family.</returns>
|
||||
public AbLegacyAddress? ParseAddress(AbLegacyTagDefinition def)
|
||||
{
|
||||
var family = Options.PlcFamily;
|
||||
if (def.Name.Contains("=>", StringComparison.Ordinal)) return AbLegacyAddress.TryParse(def.Address, family);
|
||||
if (_parsedAddresses.TryGetValue(def.Name, out var c) && c.Family == family
|
||||
&& string.Equals(c.Address, def.Address, StringComparison.Ordinal))
|
||||
return c.Parsed;
|
||||
var parsed = AbLegacyAddress.TryParse(def.Address, family);
|
||||
_parsedAddresses[def.Name] = (def.Address, family, parsed);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, SemaphoreSlim> _runtimeLocks = new();
|
||||
|
||||
@@ -2069,7 +2172,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
}
|
||||
|
||||
/// <summary>The number of per-key gates currently held (runtime + RMW). Tests only.</summary>
|
||||
internal int GateCount => _runtimeLocks.Count + _rmwLocks.Count;
|
||||
internal int GateCount { get { lock (_rmwGatesLock) return _runtimeLocks.Count + _rmwGates.Count; } }
|
||||
|
||||
/// <summary>Test seam: invoked with the runtime key just before an operation waits on its gate (after the
|
||||
/// handle was fetched), so a test can act at exactly that point instead of sleeping.</summary>
|
||||
@@ -2129,6 +2232,7 @@ public sealed partial class AbLegacyDriver : IDriver, IReadable, IWritable, ITag
|
||||
if (ParentRuntimes.TryRemove(key, out var r))
|
||||
try { r.Dispose(); } catch { /* best-effort */ }
|
||||
IndirectLastName.Clear();
|
||||
_parsedAddresses.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1362,7 +1362,7 @@ public sealed class FocasWireClient : IAsyncDisposable, IDisposable
|
||||
private static void RequireLength(byte[] payload, int length, string call)
|
||||
{
|
||||
if (payload.Length < length)
|
||||
throw new FocasWireException($"{call} returned {payload.Length} bytes; expected at least {length}.", rc: null);
|
||||
throw new FocasWirePayloadException($"{call} returned {payload.Length} bytes; expected at least {length}.");
|
||||
}
|
||||
|
||||
private static bool IsTransientException(Exception exception)
|
||||
|
||||
@@ -62,3 +62,19 @@ public class FocasWireException : Exception
|
||||
IsTransient = isTransient;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A well-formed reply whose payload is too short to decode (#1036). The CNC answered and the session is
|
||||
/// intact, so this is a decode refusal for the one tag - not a comms fault - and maps to
|
||||
/// <c>BadTypeMismatch</c> rather than <c>BadCommunicationError</c> (which would degrade driver health and feed
|
||||
/// the #527 watchdog over a shape problem that no reconnect can fix).
|
||||
/// </summary>
|
||||
public sealed class FocasWirePayloadException : FocasWireException
|
||||
{
|
||||
/// <summary>Initializes a new short/malformed-payload exception.</summary>
|
||||
/// <param name="message">The exception message.</param>
|
||||
public FocasWirePayloadException(string message)
|
||||
: base(message, rc: null)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ public sealed class WireFocasClient : IFocasClient
|
||||
}
|
||||
catch (FocasWireException ex)
|
||||
{
|
||||
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
|
||||
return (null, MapWireFailure(ex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +419,7 @@ public sealed class WireFocasClient : IFocasClient
|
||||
}
|
||||
catch (FocasWireException ex)
|
||||
{
|
||||
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
|
||||
return (null, MapWireFailure(ex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,6 +460,20 @@ public sealed class WireFocasClient : IFocasClient
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The status a wire failure publishes: the CNC's own rc when it sent one; <c>BadTypeMismatch</c> for a reply
|
||||
/// whose payload is too short to decode (#1036 - the session is intact and the CNC answered, so a shape problem
|
||||
/// is a per-tag refusal, never a comms fault); otherwise <c>BadCommunicationError</c>.
|
||||
/// </summary>
|
||||
/// <param name="ex">The wire failure.</param>
|
||||
/// <returns>The OPC UA status to publish for the tag.</returns>
|
||||
internal static uint MapWireFailure(FocasWireException ex)
|
||||
=> ex.Rc is short rc
|
||||
? FocasStatusMapper.MapFocasReturn(rc)
|
||||
: ex is FocasWirePayloadException
|
||||
? FocasStatusMapper.BadTypeMismatch
|
||||
: FocasStatusMapper.BadCommunicationError;
|
||||
|
||||
private async Task<(object? value, uint status)> ReadMacroAsync(
|
||||
FocasAddress address, FocasDataType type, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -472,7 +486,7 @@ public sealed class WireFocasClient : IFocasClient
|
||||
}
|
||||
catch (FocasWireException ex)
|
||||
{
|
||||
return (null, ex.Rc is short rc ? FocasStatusMapper.MapFocasReturn(rc) : FocasStatusMapper.BadCommunicationError);
|
||||
return (null, MapWireFailure(ex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -192,6 +192,14 @@ public static class MqttWriteEncoder
|
||||
text = value switch
|
||||
{
|
||||
bool b => b ? "true" : "false",
|
||||
// #1044: a DateTime through IFormattable published "08/10/2026 12:30:45" (month-first, no
|
||||
// zone) — ambiguous and unparseable by most devices. ISO-8601 UTC round-trip form, with the
|
||||
// same Unspecified-is-UTC rule the Sparkplug DateTime encode uses so two nodes of a
|
||||
// redundant pair publish the same text for the same authored value.
|
||||
DateTime dt => (dt.Kind == DateTimeKind.Unspecified
|
||||
? DateTime.SpecifyKind(dt, DateTimeKind.Utc)
|
||||
: dt.ToUniversalTime()).ToString("O", CultureInfo.InvariantCulture),
|
||||
DateTimeOffset dto => dto.UtcDateTime.ToString("O", CultureInfo.InvariantCulture),
|
||||
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
|
||||
_ => value.ToString() ?? "",
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user