Linter/formatter pass across the full codebase. Restores required partial keyword on AXAML code-behind classes that the formatter incorrectly removed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
188 lines
7.1 KiB
C#
188 lines
7.1 KiB
C#
using Opc.Ua;
|
|
using ZB.MOM.WW.LmxOpcUa.Client.Shared;
|
|
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
|
using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult;
|
|
|
|
namespace ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests.Fakes;
|
|
|
|
/// <summary>
|
|
/// Fake implementation of <see cref="IOpcUaClientService" /> for unit testing commands.
|
|
/// Records all method calls and returns configurable results.
|
|
/// </summary>
|
|
public sealed class FakeOpcUaClientService : IOpcUaClientService
|
|
{
|
|
// Track calls
|
|
public bool ConnectCalled { get; private set; }
|
|
public ConnectionSettings? LastConnectionSettings { get; private set; }
|
|
public bool DisconnectCalled { get; private set; }
|
|
public bool DisposeCalled { get; private set; }
|
|
public List<NodeId> ReadNodeIds { get; } = [];
|
|
public List<(NodeId NodeId, object Value)> WriteValues { get; } = [];
|
|
public List<NodeId?> BrowseNodeIds { get; } = [];
|
|
public List<(NodeId NodeId, int IntervalMs)> SubscribeCalls { get; } = [];
|
|
public List<NodeId> UnsubscribeCalls { get; } = [];
|
|
public List<(NodeId? SourceNodeId, int IntervalMs)> SubscribeAlarmsCalls { get; } = [];
|
|
public bool UnsubscribeAlarmsCalled { get; private set; }
|
|
public bool RequestConditionRefreshCalled { get; private set; }
|
|
public List<(NodeId NodeId, DateTime Start, DateTime End, int MaxValues)> HistoryReadRawCalls { get; } = [];
|
|
|
|
public List<(NodeId NodeId, DateTime Start, DateTime End, AggregateType Aggregate, double IntervalMs)>
|
|
HistoryReadAggregateCalls { get; } =
|
|
[];
|
|
|
|
public bool GetRedundancyInfoCalled { get; private set; }
|
|
|
|
// Configurable results
|
|
public ConnectionInfo ConnectionInfoResult { get; set; } = new(
|
|
"opc.tcp://localhost:4840",
|
|
"TestServer",
|
|
"None",
|
|
"http://opcfoundation.org/UA/SecurityPolicy#None",
|
|
"session-1",
|
|
"TestSession");
|
|
|
|
public DataValue ReadValueResult { get; set; } = new(
|
|
new Variant(42),
|
|
StatusCodes.Good,
|
|
DateTime.UtcNow,
|
|
DateTime.UtcNow);
|
|
|
|
public StatusCode WriteStatusCodeResult { get; set; } = StatusCodes.Good;
|
|
|
|
public IReadOnlyList<BrowseResult> BrowseResults { get; set; } = new List<BrowseResult>
|
|
{
|
|
new("ns=2;s=Node1", "Node1", "Object", true),
|
|
new("ns=2;s=Node2", "Node2", "Variable", false)
|
|
};
|
|
|
|
public IReadOnlyList<DataValue> HistoryReadResult { get; set; } = new List<DataValue>
|
|
{
|
|
new(new Variant(10.0), StatusCodes.Good, DateTime.UtcNow.AddHours(-1), DateTime.UtcNow),
|
|
new(new Variant(20.0), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow)
|
|
};
|
|
|
|
public RedundancyInfo RedundancyInfoResult { get; set; } = new(
|
|
"Warm", 200, ["urn:server1", "urn:server2"], "urn:app:test");
|
|
|
|
public Exception? ConnectException { get; set; }
|
|
public Exception? ReadException { get; set; }
|
|
public Exception? WriteException { get; set; }
|
|
public Exception? ConditionRefreshException { get; set; }
|
|
|
|
// IOpcUaClientService implementation
|
|
public bool IsConnected => ConnectCalled && !DisconnectCalled;
|
|
public ConnectionInfo? CurrentConnectionInfo => ConnectCalled ? ConnectionInfoResult : null;
|
|
|
|
public event EventHandler<DataChangedEventArgs>? DataChanged;
|
|
public event EventHandler<AlarmEventArgs>? AlarmEvent;
|
|
public event EventHandler<ConnectionStateChangedEventArgs>? ConnectionStateChanged;
|
|
|
|
public Task<ConnectionInfo> ConnectAsync(ConnectionSettings settings, CancellationToken ct = default)
|
|
{
|
|
ConnectCalled = true;
|
|
LastConnectionSettings = settings;
|
|
if (ConnectException != null) throw ConnectException;
|
|
return Task.FromResult(ConnectionInfoResult);
|
|
}
|
|
|
|
public Task DisconnectAsync(CancellationToken ct = default)
|
|
{
|
|
DisconnectCalled = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<DataValue> ReadValueAsync(NodeId nodeId, CancellationToken ct = default)
|
|
{
|
|
ReadNodeIds.Add(nodeId);
|
|
if (ReadException != null) throw ReadException;
|
|
return Task.FromResult(ReadValueResult);
|
|
}
|
|
|
|
public Task<StatusCode> WriteValueAsync(NodeId nodeId, object value, CancellationToken ct = default)
|
|
{
|
|
WriteValues.Add((nodeId, value));
|
|
if (WriteException != null) throw WriteException;
|
|
return Task.FromResult(WriteStatusCodeResult);
|
|
}
|
|
|
|
public Task<IReadOnlyList<BrowseResult>> BrowseAsync(NodeId? parentNodeId = null, CancellationToken ct = default)
|
|
{
|
|
BrowseNodeIds.Add(parentNodeId);
|
|
return Task.FromResult(BrowseResults);
|
|
}
|
|
|
|
public Task SubscribeAsync(NodeId nodeId, int intervalMs = 1000, CancellationToken ct = default)
|
|
{
|
|
SubscribeCalls.Add((nodeId, intervalMs));
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task UnsubscribeAsync(NodeId nodeId, CancellationToken ct = default)
|
|
{
|
|
UnsubscribeCalls.Add(nodeId);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task SubscribeAlarmsAsync(NodeId? sourceNodeId = null, int intervalMs = 1000, CancellationToken ct = default)
|
|
{
|
|
SubscribeAlarmsCalls.Add((sourceNodeId, intervalMs));
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task UnsubscribeAlarmsAsync(CancellationToken ct = default)
|
|
{
|
|
UnsubscribeAlarmsCalled = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task RequestConditionRefreshAsync(CancellationToken ct = default)
|
|
{
|
|
RequestConditionRefreshCalled = true;
|
|
if (ConditionRefreshException != null) throw ConditionRefreshException;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(
|
|
NodeId nodeId, DateTime startTime, DateTime endTime, int maxValues = 1000, CancellationToken ct = default)
|
|
{
|
|
HistoryReadRawCalls.Add((nodeId, startTime, endTime, maxValues));
|
|
return Task.FromResult(HistoryReadResult);
|
|
}
|
|
|
|
public Task<IReadOnlyList<DataValue>> HistoryReadAggregateAsync(
|
|
NodeId nodeId, DateTime startTime, DateTime endTime, AggregateType aggregate,
|
|
double intervalMs = 3600000, CancellationToken ct = default)
|
|
{
|
|
HistoryReadAggregateCalls.Add((nodeId, startTime, endTime, aggregate, intervalMs));
|
|
return Task.FromResult(HistoryReadResult);
|
|
}
|
|
|
|
public Task<RedundancyInfo> GetRedundancyInfoAsync(CancellationToken ct = default)
|
|
{
|
|
GetRedundancyInfoCalled = true;
|
|
return Task.FromResult(RedundancyInfoResult);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
DisposeCalled = true;
|
|
}
|
|
|
|
/// <summary>Raises the DataChanged event for testing subscribe commands.</summary>
|
|
public void RaiseDataChanged(string nodeId, DataValue value)
|
|
{
|
|
DataChanged?.Invoke(this, new DataChangedEventArgs(nodeId, value));
|
|
}
|
|
|
|
/// <summary>Raises the AlarmEvent for testing alarm commands.</summary>
|
|
public void RaiseAlarmEvent(AlarmEventArgs args)
|
|
{
|
|
AlarmEvent?.Invoke(this, args);
|
|
}
|
|
|
|
/// <summary>Raises the ConnectionStateChanged event for testing.</summary>
|
|
public void RaiseConnectionStateChanged(ConnectionState oldState, ConnectionState newState, string endpointUrl)
|
|
{
|
|
ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(oldState, newState, endpointUrl));
|
|
}
|
|
} |