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>
125 lines
5.0 KiB
C#
125 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Serilog;
|
|
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
|
|
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
|
|
using ZB.MOM.WW.LmxOpcUa.Host.Metrics;
|
|
|
|
namespace ZB.MOM.WW.LmxOpcUa.Host.MxAccess
|
|
{
|
|
/// <summary>
|
|
/// Core MXAccess client implementing IMxAccessClient via IMxProxy abstraction.
|
|
/// Split across partial classes: Connection, Subscription, ReadWrite, EventHandlers, Monitor.
|
|
/// (MXA-001 through MXA-009)
|
|
/// </summary>
|
|
public sealed partial class MxAccessClient : IMxAccessClient
|
|
{
|
|
private static readonly ILogger Log = Serilog.Log.ForContext<MxAccessClient>();
|
|
private readonly ConcurrentDictionary<string, int> _addressToHandle = new(StringComparer.OrdinalIgnoreCase);
|
|
private readonly MxAccessConfiguration _config;
|
|
|
|
// Handle mappings
|
|
private readonly ConcurrentDictionary<int, string> _handleToAddress = new();
|
|
private readonly PerformanceMetrics _metrics;
|
|
private readonly SemaphoreSlim _operationSemaphore;
|
|
|
|
private readonly ConcurrentDictionary<string, ConcurrentDictionary<int, TaskCompletionSource<Vtq>>>
|
|
_pendingReadsByAddress
|
|
= new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
// Pending writes
|
|
private readonly ConcurrentDictionary<int, TaskCompletionSource<bool>> _pendingWrites = new();
|
|
|
|
private readonly IMxProxy _proxy;
|
|
|
|
private readonly StaComThread _staThread;
|
|
|
|
// Subscription storage
|
|
private readonly ConcurrentDictionary<string, Action<string, Vtq>> _storedSubscriptions
|
|
= new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
private int _connectionHandle;
|
|
private DateTime _lastProbeValueTime = DateTime.UtcNow;
|
|
private CancellationTokenSource? _monitorCts;
|
|
|
|
// Probe
|
|
private string? _probeTag;
|
|
private bool _proxyEventsAttached;
|
|
private int _reconnectCount;
|
|
private volatile ConnectionState _state = ConnectionState.Disconnected;
|
|
|
|
/// <summary>
|
|
/// Initializes a new MXAccess client around the STA thread, COM proxy abstraction, and runtime throttling settings.
|
|
/// </summary>
|
|
/// <param name="staThread">The STA thread used to marshal COM interactions.</param>
|
|
/// <param name="proxy">The COM proxy abstraction used to talk to the runtime.</param>
|
|
/// <param name="config">The runtime timeout, throttling, and reconnect settings.</param>
|
|
/// <param name="metrics">The metrics collector used to time MXAccess operations.</param>
|
|
public MxAccessClient(StaComThread staThread, IMxProxy proxy, MxAccessConfiguration config,
|
|
PerformanceMetrics metrics)
|
|
{
|
|
_staThread = staThread;
|
|
_proxy = proxy;
|
|
_config = config;
|
|
_metrics = metrics;
|
|
_operationSemaphore = new SemaphoreSlim(config.MaxConcurrentOperations, config.MaxConcurrentOperations);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current runtime connection state for the MXAccess client.
|
|
/// </summary>
|
|
public ConnectionState State => _state;
|
|
|
|
/// <summary>
|
|
/// Gets the number of active tag subscriptions currently maintained against the runtime.
|
|
/// </summary>
|
|
public int ActiveSubscriptionCount => _storedSubscriptions.Count;
|
|
|
|
/// <summary>
|
|
/// Gets the number of reconnect attempts performed since the client was created.
|
|
/// </summary>
|
|
public int ReconnectCount => _reconnectCount;
|
|
|
|
/// <summary>
|
|
/// Occurs when the MXAccess connection state changes.
|
|
/// </summary>
|
|
public event EventHandler<ConnectionStateChangedEventArgs>? ConnectionStateChanged;
|
|
|
|
/// <summary>
|
|
/// Occurs when a subscribed runtime tag publishes a new value.
|
|
/// </summary>
|
|
public event Action<string, Vtq>? OnTagValueChanged;
|
|
|
|
/// <summary>
|
|
/// Cancels monitoring and disconnects the runtime session before releasing local resources.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
try
|
|
{
|
|
_monitorCts?.Cancel();
|
|
DisconnectAsync().GetAwaiter().GetResult();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Warning(ex, "Error during MxAccessClient dispose");
|
|
}
|
|
finally
|
|
{
|
|
_operationSemaphore.Dispose();
|
|
_monitorCts?.Dispose();
|
|
}
|
|
}
|
|
|
|
private void SetState(ConnectionState newState, string message = "")
|
|
{
|
|
var previous = _state;
|
|
if (previous == newState) return;
|
|
_state = newState;
|
|
Log.Information("MxAccess state: {Previous} → {Current} {Message}", previous, newState, message);
|
|
ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(previous, newState, message));
|
|
}
|
|
}
|
|
} |