Surface int[]? ArrayDimensions on TwinCATTagDefinition + thread it through ITwinCATClient.ReadValueAsync / WriteValueAsync. When non-null + non-empty, AdsTwinCATClient issues a single ADS read against the symbol with clrType.MakeArrayType() and returns the flat 1-D CLR Array; for IEC TIME / DATE / DT / TOD element types we project per-element to the native TimeSpan / DateTime so consumers see consistent types regardless of rank. DiscoverAsync surfaces IsArray=true + ArrayDim=product(dims) onto DriverAttributeInfo via a new ResolveArrayShape helper. Multi-dim shapes flatten to the product on the wire — DriverAttributeInfo.ArrayDim is single-uint today and the OPC UA layer reflects rank via its own metadata. Native ADS notification subscriptions skip whole-array tags so the OPC UA layer falls through to a polled snapshot — the per-element AdsNotificationEx callback shape doesn't fit a flat array. Whole-array WRITES are out of scope for this PR — AdsTwinCATClient.WriteValueAsync returns BadNotSupported when ArrayDimensions is set. Tests: TwinCATArrayReadTests covers ResolveArrayShape (null / empty / single-dim / multi-dim flatten / non-positive defensive), DiscoverAsync emitting IsArray + ArrayDim for declared array tags, single-dim + multi-dim fake-client read fan-out, and the BadNotSupported gate on whole-array writes. Existing 137 unit tests still pass — total now 143. Closes #308
153 lines
6.0 KiB
C#
153 lines
6.0 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 List<(string symbol, TwinCATDataType type, int? bit, int[]? arrayDimensions)> ReadLog { get; } = new();
|
|
public bool ProbeResult { get; set; } = true;
|
|
|
|
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, int[]? arrayDimensions, CancellationToken ct)
|
|
{
|
|
if (ThrowOnRead) throw Exception ?? new InvalidOperationException();
|
|
ReadLog.Add((symbolPath, type, bitIndex, arrayDimensions));
|
|
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, int[]? arrayDimensions, object? value, CancellationToken ct)
|
|
{
|
|
if (ThrowOnWrite) throw Exception ?? new InvalidOperationException();
|
|
WriteLog.Add((symbolPath, type, bitIndex, value));
|
|
|
|
// Model the parent-word RMW path the production AdsTwinCATClient performs for
|
|
// bit-indexed BOOL writes so driver-level tests can assert the resulting parent state.
|
|
if (bitIndex is int bit && type == TwinCATDataType.Bool)
|
|
{
|
|
var parentPath = AdsTwinCATClient.TryGetParentSymbolPath(symbolPath);
|
|
if (parentPath is not null)
|
|
{
|
|
var current = Values.TryGetValue(parentPath, out var p) && p is not null
|
|
? Convert.ToUInt32(p) : 0u;
|
|
Values[parentPath] = AdsTwinCATClient.ApplyBit(
|
|
current, bit, Convert.ToBoolean(value));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
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;
|
|
}
|
|
}
|