refactor: migrate OPC UA driver and browser to SDK 2.0

This commit is contained in:
Joseph Doherty
2026-11-10 10:08:12 -05:00
parent 586ce9422d
commit 52a4476b21
10 changed files with 171 additions and 113 deletions
@@ -73,10 +73,10 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
Attributes.NodeClass, Attributes.DisplayName, Attributes.DataType, Attributes.ValueRank,
Attributes.UserAccessLevel,
];
var toRead = new ReadValueIdCollection(attributeIds.Length);
var toRead = new List<ReadValueId>(attributeIds.Length);
foreach (var id in attributeIds) toRead.Add(new ReadValueId { NodeId = resolved, AttributeId = id });
DataValueCollection values;
ArrayOf<DataValue> values;
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
@@ -97,15 +97,15 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
DataValue? At(int i) => i < values.Count && StatusCode.IsGood(values[i].StatusCode)
? values[i] : null;
var nodeClass = At(0)?.Value is int nc ? (NodeClass)nc : NodeClass.Unspecified;
var nodeClass = At(0)?.WrappedValue.AsBoxedObject() is int nc ? (NodeClass)nc : NodeClass.Unspecified;
if (nodeClass != NodeClass.Variable) return Array.Empty<AttributeInfo>();
var name = (At(1)?.Value as LocalizedText)?.Text ?? resolved.ToString() ?? nodeId;
var dataType = At(2)?.Value is NodeId dt
var name = (At(1)?.WrappedValue.AsBoxedObject() as LocalizedText?)?.Text ?? resolved.ToString() ?? nodeId;
var dataType = At(2)?.WrappedValue.AsBoxedObject() is NodeId dt
? UpstreamAttributeMapping.MapDataType(dt)
: ZB.MOM.WW.OtOpcUa.Core.Abstractions.DriverDataType.String;
var valueRank = At(3)?.Value is int vr ? vr : ValueRanks.Scalar;
var access = At(4)?.Value is byte ab ? ab : (byte)0;
var valueRank = At(3)?.WrappedValue.AsBoxedObject() is int vr ? vr : ValueRanks.Scalar;
var access = At(4)?.WrappedValue.AsBoxedObject() is byte ab ? ab : (byte)0;
return
[
@@ -126,7 +126,7 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
await _gate.WaitAsync(ct).ConfigureAwait(false);
try
{
var descriptions = new BrowseDescriptionCollection
var descriptions = new List<BrowseDescription>
{
new()
{
@@ -154,7 +154,7 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
}
var result = resp.Results[0];
var refs = result.References;
var refs = result.References.ToList();
// Follow browse continuation points so folders larger than the server's per-call
// cap aren't silently truncated.
@@ -190,7 +190,7 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
if (next.Results.Count == 0) break;
var nextResult = next.Results[0];
if (nextResult.References is { Count: > 0 })
refs.AddRange(nextResult.References);
refs.AddRange(nextResult.References.ToList());
cp = nextResult.ContinuationPoint;
}
@@ -214,8 +214,8 @@ internal sealed class OpcUaClientBrowseSession : IBrowseSession
{
var childId = ExpandedNodeId.ToNodeId(rf.NodeId, _session.NamespaceUris);
var isObject = rf.NodeClass == NodeClass.Object;
var displayName = rf.DisplayName?.Text
?? rf.BrowseName?.Name
var displayName = rf.DisplayName.Text
?? rf.BrowseName.Name
?? childId.ToString()
?? "(unnamed)";
return new BrowseNode(
@@ -101,7 +101,7 @@ public sealed class OpcUaClientDriverBrowser : IDriverBrowser
sessionName: "OtOpcUa AdminUI Browse",
(uint)opts.SessionTimeout.TotalMilliseconds,
identity,
preferredLocales: null,
preferredLocales: default,
connectCts.Token).ConfigureAwait(false);
try
@@ -224,8 +224,7 @@ public sealed class OpcUaClientDriverBrowser : IDriverBrowser
return (OpcUaUserIdentityFactory.Build(resolved), null);
}
var certificate = OpcUaUserIdentityFactory.LoadCertificate(resolved);
return (new UserIdentity(certificate), certificate);
return (OpcUaUserIdentityFactory.FromCertificate(resolved), null);
}
/// <summary>Select the endpoint matching the requested SecurityPolicy + SecurityMode pair.</summary>
@@ -240,7 +239,7 @@ public sealed class OpcUaClientDriverBrowser : IDriverBrowser
{
using var client = await DiscoveryClient.CreateAsync(
appConfig, new Uri(url), DiagnosticsMasks.None, ct).ConfigureAwait(false);
var all = await client.GetEndpointsAsync(null, ct).ConfigureAwait(false);
var all = await client.GetEndpointsAsync(default, ct).ConfigureAwait(false);
var wantedPolicy = MapPolicy(policy);
var wantedMode = mode switch
@@ -250,11 +249,11 @@ public sealed class OpcUaClientDriverBrowser : IDriverBrowser
OpcUaSecurityMode.SignAndEncrypt => MessageSecurityMode.SignAndEncrypt,
_ => throw new ArgumentOutOfRangeException(nameof(mode)),
};
var match = all.FirstOrDefault(e =>
var match = all.ToList().FirstOrDefault(e =>
e.SecurityPolicyUri == wantedPolicy && e.SecurityMode == wantedMode);
if (match is null)
{
var advertised = string.Join(", ", all.Select(e =>
var advertised = string.Join(", ", all.ToList().Select(e =>
$"{ShortName(e.SecurityPolicyUri)}/{e.SecurityMode}"));
throw new InvalidOperationException(
$"No endpoint at '{url}' matches SecurityPolicy={policy} + SecurityMode={mode}. " +
@@ -278,7 +277,7 @@ public sealed class OpcUaClientDriverBrowser : IDriverBrowser
/// <summary>Render an OPC UA security-policy URI as its short suffix for diag messages.</summary>
/// <param name="uri">Full policy URI to shorten.</param>
private static string ShortName(string uri) =>
private static string ShortName(string? uri) =>
uri?.Substring(uri.LastIndexOf('#') + 1) ?? "(null)";
}
@@ -13,7 +13,6 @@
<ProjectReference Include="..\ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Session\ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Session.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.MigrationAnalyzer" PrivateAssets="all" IncludeAssets="analyzers;build;buildtransitive" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />