feat(dcl): replace hand-rolled LmxProxy gRPC client with real LmxProxyClient library
Switches from v1 string-based proto stubs to the production LmxProxyClient (v2 native TypedValue protocol) via project reference. Deletes 6k+ lines of generated proto code. Preserves ILmxProxyClient adapter interface for testability.
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ScadaLink.Commons.Interfaces.Protocol;
|
||||
using ScadaLink.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.LmxProxy.Client.Domain;
|
||||
using QualityCode = ScadaLink.Commons.Interfaces.Protocol.QualityCode;
|
||||
using WriteResult = ScadaLink.Commons.Interfaces.Protocol.WriteResult;
|
||||
|
||||
namespace ScadaLink.DataConnectionLayer.Adapters;
|
||||
|
||||
/// <summary>
|
||||
/// LmxProxy adapter implementing IDataConnection.
|
||||
/// Maps IDataConnection operations to the LmxProxy SDK client.
|
||||
/// Maps IDataConnection operations to the real LmxProxy SDK client
|
||||
/// via the <see cref="ILmxProxyClient"/> abstraction.
|
||||
///
|
||||
/// LmxProxy-specific behavior:
|
||||
/// - Session-based connection with automatic 30s keep-alive (managed by SDK)
|
||||
/// - gRPC streaming for subscriptions via ILmxSubscription handles
|
||||
/// - API key authentication via x-api-key gRPC metadata header
|
||||
/// - Native TypedValue writes (v2 protocol)
|
||||
/// </summary>
|
||||
public class LmxProxyDataConnection : IDataConnection
|
||||
{
|
||||
@@ -41,11 +46,10 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
_port = port;
|
||||
connectionDetails.TryGetValue("ApiKey", out var apiKey);
|
||||
|
||||
var samplingIntervalMs = connectionDetails.TryGetValue("SamplingIntervalMs", out var sampStr) && int.TryParse(sampStr, out var samp) ? samp : 0;
|
||||
var useTls = connectionDetails.TryGetValue("UseTls", out var tlsStr) && bool.TryParse(tlsStr, out var tls) && tls;
|
||||
|
||||
_status = ConnectionHealth.Connecting;
|
||||
_client = _clientFactory.Create(_host, _port, apiKey, samplingIntervalMs, useTls);
|
||||
_client = _clientFactory.Create(_host, _port, apiKey, useTls);
|
||||
|
||||
await _client.ConnectAsync(cancellationToken);
|
||||
_status = ConnectionHealth.Connected;
|
||||
@@ -72,9 +76,9 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
{
|
||||
var vtq = await _client!.ReadAsync(tagPath, cancellationToken);
|
||||
var quality = MapQuality(vtq.Quality);
|
||||
var tagValue = new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.TimestampUtc, TimeSpan.Zero));
|
||||
var tagValue = new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.Timestamp, TimeSpan.Zero));
|
||||
|
||||
return vtq.Quality == LmxQuality.Bad
|
||||
return vtq.Quality.IsBad()
|
||||
? new ReadResult(false, tagValue, "LmxProxy read returned bad quality")
|
||||
: new ReadResult(true, tagValue, null);
|
||||
}
|
||||
@@ -96,8 +100,8 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
foreach (var (tag, vtq) in vtqs)
|
||||
{
|
||||
var quality = MapQuality(vtq.Quality);
|
||||
var tagValue = new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.TimestampUtc, TimeSpan.Zero));
|
||||
results[tag] = vtq.Quality == LmxQuality.Bad
|
||||
var tagValue = new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.Timestamp, TimeSpan.Zero));
|
||||
results[tag] = vtq.Quality.IsBad()
|
||||
? new ReadResult(false, tagValue, "LmxProxy read returned bad quality")
|
||||
: new ReadResult(true, tagValue, null);
|
||||
}
|
||||
@@ -111,7 +115,7 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
|
||||
try
|
||||
{
|
||||
await _client!.WriteAsync(tagPath, value!, cancellationToken);
|
||||
await _client!.WriteAsync(tagPath, ToTypedValue(value), cancellationToken);
|
||||
return new WriteResult(true, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -126,9 +130,8 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
|
||||
try
|
||||
{
|
||||
var nonNullValues = values.Where(kv => kv.Value != null)
|
||||
.ToDictionary(kv => kv.Key, kv => kv.Value!);
|
||||
await _client!.WriteBatchAsync(nonNullValues, cancellationToken);
|
||||
var typedValues = values.ToDictionary(kv => kv.Key, kv => ToTypedValue(kv.Value));
|
||||
await _client!.WriteBatchAsync(typedValues, cancellationToken);
|
||||
|
||||
return values.Keys.ToDictionary(k => k, _ => new WriteResult(true, null))
|
||||
as IReadOnlyDictionary<string, WriteResult>;
|
||||
@@ -174,11 +177,11 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
(path, vtq) =>
|
||||
{
|
||||
var quality = MapQuality(vtq.Quality);
|
||||
callback(path, new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.TimestampUtc, TimeSpan.Zero)));
|
||||
callback(path, new TagValue(vtq.Value, quality, new DateTimeOffset(vtq.Timestamp, TimeSpan.Zero)));
|
||||
},
|
||||
onStreamError: () =>
|
||||
onStreamError: ex =>
|
||||
{
|
||||
_logger.LogWarning("LmxProxy subscription stream ended unexpectedly for {TagPath}", tagPath);
|
||||
_logger.LogWarning(ex, "LmxProxy subscription stream ended unexpectedly for {TagPath}", tagPath);
|
||||
RaiseDisconnected();
|
||||
},
|
||||
cancellationToken);
|
||||
@@ -219,10 +222,6 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
throw new InvalidOperationException("LmxProxy client is not connected.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks the connection as disconnected and fires the Disconnected event once.
|
||||
/// Thread-safe: only the first caller triggers the event.
|
||||
/// </summary>
|
||||
private void RaiseDisconnected()
|
||||
{
|
||||
if (_disconnectFired) return;
|
||||
@@ -232,11 +231,23 @@ public class LmxProxyDataConnection : IDataConnection
|
||||
Disconnected?.Invoke();
|
||||
}
|
||||
|
||||
private static QualityCode MapQuality(LmxQuality quality) => quality switch
|
||||
private static QualityCode MapQuality(Quality quality)
|
||||
{
|
||||
LmxQuality.Good => QualityCode.Good,
|
||||
LmxQuality.Uncertain => QualityCode.Uncertain,
|
||||
LmxQuality.Bad => QualityCode.Bad,
|
||||
_ => QualityCode.Bad
|
||||
if (quality.IsGood()) return QualityCode.Good;
|
||||
if (quality.IsUncertain()) return QualityCode.Uncertain;
|
||||
return QualityCode.Bad;
|
||||
}
|
||||
|
||||
private static TypedValue ToTypedValue(object? value) => value switch
|
||||
{
|
||||
bool b => new TypedValue { BoolValue = b },
|
||||
int i => new TypedValue { Int32Value = i },
|
||||
long l => new TypedValue { Int64Value = l },
|
||||
float f => new TypedValue { FloatValue = f },
|
||||
double d => new TypedValue { DoubleValue = d },
|
||||
string s => new TypedValue { StringValue = s },
|
||||
DateTime dt => new TypedValue { DatetimeValue = dt.ToUniversalTime().Ticks },
|
||||
null => new TypedValue { StringValue = string.Empty },
|
||||
_ => new TypedValue { StringValue = value.ToString() ?? string.Empty }
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user