Driver.TwinCAT-001 — InitializeAsync/ReinitializeAsync ignored driverConfigJson. Extracted the DTO-to-options parse into a shared TwinCATDriverFactoryExtensions.ParseOptions; InitializeAsync now re-parses driverConfigJson into a mutable _options field, so a config generation pushed via ReinitializeAsync (added/removed devices, tags, probe settings) is actually applied at runtime. Driver.TwinCAT-002 — LInt/ULInt narrowed to Int32. ToDriverDataType now maps LInt to Int64, ULInt to UInt64, UDInt to UInt32, UInt/USInt to UInt16, Int/SInt to Int16, and the IEC TIME/DATE/DT/TOD types to UInt32 (their raw UDINT counter). Removed the stale "Int64 gap" comment — no truncation or sign flips at the OPC UA encode layer. Driver.TwinCAT-007 — EnsureConnectedAsync was not thread-safe. Connect/reconnect is now serialized per device by a SemaphoreSlim (DeviceState.ConnectGate) with a double-checked connect, mirroring the S7 driver. Concurrent read/write/probe callers can no longer leak a client or race a create-vs-dispose. Driver.TwinCAT-008 — native ADS notification callbacks ran driver logic on the AMS router thread. AdsTwinCATClient now enqueues AdsNotificationEx callbacks onto a bounded Channel drained by a dedicated managed task; the router-thread callback only does a non-blocking TryWrite, so a slow consumer cannot stall ADS notification delivery process-wide. Driver.TwinCAT-013 — TwinCATDriver did not implement IRediscoverable. The driver now implements IRediscoverable; AdsTwinCATClient detects ADS 0x0702 (symbol-version-changed) on read/write paths and raises OnSymbolVersionChanged, which the driver forwards as OnRediscoveryNeeded so Core rebuilds the address space after a PLC program re-download. Adds TwinCATHighFindingsRegressionTests covering all five fixes; updates the data-type mapping assertion in TwinCATDriverTests. TwinCAT driver builds clean; 119 tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
138 lines
5.4 KiB
C#
138 lines
5.4 KiB
C#
using System.Runtime.CompilerServices;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests;
|
|
|
|
internal class FakeTwinCATClient : ITwinCATClient
|
|
{
|
|
public bool IsConnected { get; private set; }
|
|
public int ConnectCount { get; private set; }
|
|
public int DisposeCount { get; private set; }
|
|
public bool ThrowOnConnect { get; set; }
|
|
public bool ThrowOnRead { get; set; }
|
|
public bool ThrowOnWrite { get; set; }
|
|
public bool ThrowOnProbe { get; set; }
|
|
public Exception? Exception { get; set; }
|
|
public Dictionary<string, object?> Values { get; } = new(StringComparer.OrdinalIgnoreCase);
|
|
public Dictionary<string, uint> ReadStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
|
|
public Dictionary<string, uint> WriteStatuses { get; } = new(StringComparer.OrdinalIgnoreCase);
|
|
public List<(string symbol, TwinCATDataType type, int? bit, object? value)> WriteLog { get; } = new();
|
|
public bool ProbeResult { get; set; } = true;
|
|
|
|
public event EventHandler? OnSymbolVersionChanged;
|
|
|
|
/// <summary>Test hook — fire the symbol-version-changed signal as the real client would.</summary>
|
|
public void FireSymbolVersionChanged() => OnSymbolVersionChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
public virtual Task ConnectAsync(TwinCATAmsAddress address, TimeSpan timeout, CancellationToken ct)
|
|
{
|
|
ConnectCount++;
|
|
if (ThrowOnConnect) throw Exception ?? new InvalidOperationException();
|
|
IsConnected = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public virtual Task<(object? value, uint status)> ReadValueAsync(
|
|
string symbolPath, TwinCATDataType type, int? bitIndex, CancellationToken ct)
|
|
{
|
|
if (ThrowOnRead) throw Exception ?? new InvalidOperationException();
|
|
var status = ReadStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
|
|
var value = Values.TryGetValue(symbolPath, out var v) ? v : null;
|
|
return Task.FromResult((value, status));
|
|
}
|
|
|
|
public virtual Task<uint> WriteValueAsync(
|
|
string symbolPath, TwinCATDataType type, int? bitIndex, object? value, CancellationToken ct)
|
|
{
|
|
if (ThrowOnWrite) throw Exception ?? new InvalidOperationException();
|
|
WriteLog.Add((symbolPath, type, bitIndex, value));
|
|
Values[symbolPath] = value;
|
|
var status = WriteStatuses.TryGetValue(symbolPath, out var s) ? s : TwinCATStatusMapper.Good;
|
|
return Task.FromResult(status);
|
|
}
|
|
|
|
public virtual Task<bool> ProbeAsync(CancellationToken ct)
|
|
{
|
|
if (ThrowOnProbe) return Task.FromResult(false);
|
|
return Task.FromResult(ProbeResult);
|
|
}
|
|
|
|
public virtual void Dispose()
|
|
{
|
|
DisposeCount++;
|
|
IsConnected = false;
|
|
}
|
|
|
|
// ---- notification fake ----
|
|
|
|
public List<FakeNotification> Notifications { get; } = new();
|
|
public bool ThrowOnAddNotification { get; set; }
|
|
|
|
public virtual Task<ITwinCATNotificationHandle> AddNotificationAsync(
|
|
string symbolPath, TwinCATDataType type, int? bitIndex, TimeSpan cycleTime,
|
|
Action<string, object?> onChange, CancellationToken cancellationToken)
|
|
{
|
|
if (ThrowOnAddNotification)
|
|
throw Exception ?? new InvalidOperationException("fake AddNotification failure");
|
|
|
|
var reg = new FakeNotification(symbolPath, type, bitIndex, onChange, this);
|
|
Notifications.Add(reg);
|
|
return Task.FromResult<ITwinCATNotificationHandle>(reg);
|
|
}
|
|
|
|
/// <summary>Fire a change event through the registered callback for <paramref name="symbolPath"/>.</summary>
|
|
public void FireNotification(string symbolPath, object? value)
|
|
{
|
|
foreach (var n in Notifications)
|
|
if (!n.Disposed && string.Equals(n.SymbolPath, symbolPath, StringComparison.OrdinalIgnoreCase))
|
|
n.OnChange(symbolPath, value);
|
|
}
|
|
|
|
// ---- symbol browser fake ----
|
|
|
|
public List<TwinCATDiscoveredSymbol> BrowseResults { get; } = new();
|
|
public bool ThrowOnBrowse { get; set; }
|
|
|
|
public virtual async IAsyncEnumerable<TwinCATDiscoveredSymbol> BrowseSymbolsAsync(
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
if (ThrowOnBrowse) throw Exception ?? new InvalidOperationException("fake browse failure");
|
|
await Task.CompletedTask;
|
|
foreach (var sym in BrowseResults)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested) yield break;
|
|
yield return sym;
|
|
}
|
|
}
|
|
|
|
public sealed class FakeNotification(
|
|
string symbolPath, TwinCATDataType type, int? bitIndex,
|
|
Action<string, object?> onChange, FakeTwinCATClient owner) : ITwinCATNotificationHandle
|
|
{
|
|
public string SymbolPath { get; } = symbolPath;
|
|
public TwinCATDataType Type { get; } = type;
|
|
public int? BitIndex { get; } = bitIndex;
|
|
public Action<string, object?> OnChange { get; } = onChange;
|
|
public bool Disposed { get; private set; }
|
|
|
|
public void Dispose()
|
|
{
|
|
Disposed = true;
|
|
owner.Notifications.Remove(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal sealed class FakeTwinCATClientFactory : ITwinCATClientFactory
|
|
{
|
|
public List<FakeTwinCATClient> Clients { get; } = new();
|
|
public Func<FakeTwinCATClient>? Customise { get; set; }
|
|
|
|
public ITwinCATClient Create()
|
|
{
|
|
var client = Customise?.Invoke() ?? new FakeTwinCATClient();
|
|
Clients.Add(client);
|
|
return client;
|
|
}
|
|
}
|