0765eb4de3
Seventh PR of the alarms-over-gateway epic (docs/plans/alarms-over-gateway.md). Depends on PR A.1 (proto, merged) and E.1 (regen, merged). Hand-written .NET SDK methods on top of the regenerated proto types: - MxGatewayClient.AcknowledgeAlarmAsync — routes through the existing safe-unary retry pipeline (Acks are idempotent at MxAccess), maps Unauthenticated/PermissionDenied RpcExceptions to typed MxGatewayAuthenticationException / MxGatewayAuthorizationException via GrpcMxGatewayClientTransport.MapRpcException. - MxGatewayClient.QueryActiveAlarmsAsync — server-streaming IAsyncEnumerable<ActiveAlarmSnapshot> mirroring the StreamEvents pattern. - IMxGatewayClientTransport extended; GrpcMxGatewayClientTransport implements both methods using the regenerated grpc client. - FakeGatewayTransport extended with capture lists, exception queue, and reply / snapshot enqueue helpers. CLI version-string assertions updated for the GatewayProtocolVersion 2 → 3 bump from A.1. The CLI alarms verb (subscribe / acknowledge / query-active) is deferred to a follow-up — keeping this PR focused on the SDK surface that lmxopcua's GalaxyDriver consumes in PR B.2. The other-language SDKs (E.3-E.6) layer the same shape on the regen. Tests: - 6 new MxGatewayClientAlarmsTests — request shape, cancellation honor (linked-token via retry pipeline), Unauthenticated mapping, streaming snapshot enumeration, filter prefix passthrough, cancellation during enumeration. - Full client test suite: 57 passed (was 51; 6 new). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
202 lines
6.3 KiB
C#
202 lines
6.3 KiB
C#
using Grpc.Core;
|
|
using MxGateway.Contracts.Proto;
|
|
|
|
namespace MxGateway.Client;
|
|
|
|
/// <summary>
|
|
/// gRPC implementation of IMxGatewayClientTransport.
|
|
/// </summary>
|
|
internal sealed class GrpcMxGatewayClientTransport(
|
|
MxGatewayClientOptions options,
|
|
MxAccessGateway.MxAccessGatewayClient rawClient) : IMxGatewayClientTransport
|
|
{
|
|
/// <summary>
|
|
/// Gets the gateway client options.
|
|
/// </summary>
|
|
public MxGatewayClientOptions Options { get; } = options;
|
|
|
|
/// <summary>
|
|
/// Gets the underlying gRPC client.
|
|
/// </summary>
|
|
public MxAccessGateway.MxAccessGatewayClient RawClient { get; } = rawClient;
|
|
|
|
/// <inheritdoc />
|
|
MxAccessGateway.MxAccessGatewayClient? IMxGatewayClientTransport.RawClient => RawClient;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<OpenSessionReply> OpenSessionAsync(
|
|
OpenSessionRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
try
|
|
{
|
|
return await RawClient.OpenSessionAsync(request, callOptions)
|
|
.ResponseAsync
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, callOptions.CancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<CloseSessionReply> CloseSessionAsync(
|
|
CloseSessionRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
try
|
|
{
|
|
return await RawClient.CloseSessionAsync(request, callOptions)
|
|
.ResponseAsync
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, callOptions.CancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<MxCommandReply> InvokeAsync(
|
|
MxCommandRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
try
|
|
{
|
|
return await RawClient.InvokeAsync(request, callOptions)
|
|
.ResponseAsync
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, callOptions.CancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async IAsyncEnumerable<MxEvent> StreamEventsAsync(
|
|
StreamEventsRequest request,
|
|
CallOptions callOptions,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
{
|
|
CancellationToken effectiveCancellationToken = cancellationToken.CanBeCanceled
|
|
? cancellationToken
|
|
: callOptions.CancellationToken;
|
|
|
|
using AsyncServerStreamingCall<MxEvent> call = RawClient.StreamEvents(request, callOptions);
|
|
|
|
IAsyncStreamReader<MxEvent> responseStream = call.ResponseStream;
|
|
while (true)
|
|
{
|
|
MxEvent? gatewayEvent;
|
|
try
|
|
{
|
|
if (!await responseStream.MoveNext(effectiveCancellationToken).ConfigureAwait(false))
|
|
{
|
|
break;
|
|
}
|
|
|
|
gatewayEvent = responseStream.Current;
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, effectiveCancellationToken);
|
|
}
|
|
|
|
yield return gatewayEvent;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
IAsyncEnumerable<MxEvent> IMxGatewayClientTransport.StreamEventsAsync(
|
|
StreamEventsRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
return StreamEventsAsync(request, callOptions);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<AcknowledgeAlarmReply> AcknowledgeAlarmAsync(
|
|
AcknowledgeAlarmRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
try
|
|
{
|
|
return await RawClient.AcknowledgeAlarmAsync(request, callOptions)
|
|
.ResponseAsync
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, callOptions.CancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async IAsyncEnumerable<ActiveAlarmSnapshot> QueryActiveAlarmsAsync(
|
|
QueryActiveAlarmsRequest request,
|
|
CallOptions callOptions,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
{
|
|
CancellationToken effectiveCancellationToken = cancellationToken.CanBeCanceled
|
|
? cancellationToken
|
|
: callOptions.CancellationToken;
|
|
|
|
using AsyncServerStreamingCall<ActiveAlarmSnapshot> call = RawClient.QueryActiveAlarms(request, callOptions);
|
|
|
|
IAsyncStreamReader<ActiveAlarmSnapshot> responseStream = call.ResponseStream;
|
|
while (true)
|
|
{
|
|
ActiveAlarmSnapshot? snapshot;
|
|
try
|
|
{
|
|
if (!await responseStream.MoveNext(effectiveCancellationToken).ConfigureAwait(false))
|
|
{
|
|
break;
|
|
}
|
|
|
|
snapshot = responseStream.Current;
|
|
}
|
|
catch (RpcException exception)
|
|
{
|
|
throw MapRpcException(exception, effectiveCancellationToken);
|
|
}
|
|
|
|
yield return snapshot;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
IAsyncEnumerable<ActiveAlarmSnapshot> IMxGatewayClientTransport.QueryActiveAlarmsAsync(
|
|
QueryActiveAlarmsRequest request,
|
|
CallOptions callOptions)
|
|
{
|
|
return QueryActiveAlarmsAsync(request, callOptions);
|
|
}
|
|
|
|
private static Exception MapRpcException(
|
|
RpcException exception,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested || exception.StatusCode == StatusCode.Cancelled)
|
|
{
|
|
return new OperationCanceledException(
|
|
exception.Status.Detail,
|
|
exception,
|
|
cancellationToken);
|
|
}
|
|
|
|
return exception.StatusCode switch
|
|
{
|
|
StatusCode.Unauthenticated => new MxGatewayAuthenticationException(
|
|
exception.Status.Detail,
|
|
innerException: exception),
|
|
StatusCode.PermissionDenied => new MxGatewayAuthorizationException(
|
|
exception.Status.Detail,
|
|
innerException: exception),
|
|
_ => new MxGatewayException(exception.Status.Detail, exception),
|
|
};
|
|
}
|
|
}
|