using Opc.Ua; namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters; /// /// Translates the node-reference strings ScadaBridge stores in configuration /// (TemplateAttribute.DataSourceReference and friends) to session-local /// s, and back again for the node browser. /// /// /// /// Why this exists. A stored ns=2;s=Foo reference hard-codes a namespace /// index, and an index is only meaningful relative to one server's namespace /// table. A server that adds, removes or reorders a namespace silently re-points every /// stored binding: best case the read fails BadNodeIdUnknown, worst case the /// index now names a different namespace whose table happens to contain a colliding /// identifier and the binding resolves to the wrong node with Good quality. The /// namespace URI is the stable identity — the index is just its position today. /// /// /// The durable form is therefore nsu=<namespace-uri>;s=<identifier>, /// resolved against the live namespace table at use time. accepts /// both forms, so existing ns= bindings keep working unchanged; /// is what the browser emits so newly-authored bindings are index-proof from the start. /// /// /// Namespace 0 is exempt: the OPC UA spec fixes it to http://opcfoundation.org/UA/, /// so standard NodeIds like i=85 are already stable and stay in their short form. /// /// public static class OpcUaNodeReference { /// /// Resolves a stored node reference — either ns=<index>;… or the durable /// nsu=<uri>;… form — to a valid for this session. /// /// The stored reference string. /// The connected session's namespace table. /// A session-local . /// /// The reference is unparseable, names a namespace URI this server does not publish, /// or points at a different server via svr=. /// public static NodeId Resolve(string reference, NamespaceTable namespaceUris) { ArgumentException.ThrowIfNullOrEmpty(reference); ArgumentNullException.ThrowIfNull(namespaceUris); ExpandedNodeId expanded; try { expanded = ExpandedNodeId.Parse(reference); } catch (Exception ex) { throw new ArgumentException( $"'{reference}' is not a valid OPC UA node reference. Expected 'ns=;s=' " + $"or the namespace-URI-qualified 'nsu=;s='.", nameof(reference), ex); } // svr= addresses a node on a *different* server via this server's server table. // Nothing downstream follows that indirection, so fail loudly rather than hand // the SDK a reference it would resolve against the wrong address space. if (expanded.ServerIndex != 0) { throw new ArgumentException( $"'{reference}' targets another server (svr={expanded.ServerIndex}); " + "ScadaBridge binds only to nodes on the connected server.", nameof(reference)); } // Maps nsu= -> the live index; leaves an ns= reference's index untouched. var nodeId = ExpandedNodeId.ToNodeId(expanded, namespaceUris); if (nodeId.IsNull) { throw new ArgumentException( $"'{reference}' names namespace URI '{expanded.NamespaceUri}', which this server " + "does not publish in its NamespaceArray. The binding likely predates a server " + "upgrade that changed its namespaces.", nameof(reference)); } return nodeId; } /// /// Renders a browse result as the durable, index-proof reference to store. /// /// The a browse returned. /// The connected session's namespace table. /// /// nsu=<uri>;s=<id> for server-specific namespaces; the plain short form for /// namespace 0. Falls back to the raw string when the namespace cannot be named. /// public static string ToDurable(ExpandedNodeId nodeId, NamespaceTable namespaceUris) { ArgumentNullException.ThrowIfNull(namespaceUris); // A browse can hand back a reference carrying a NamespaceUri or a server index // rather than a plain index. Resolving to session-local first collapses those // shapes; without it the raw ToString() can emit a string Resolve cannot re-read. var local = ExpandedNodeId.ToNodeId(nodeId, namespaceUris); if (local.IsNull) return nodeId.ToString() ?? string.Empty; // ns=0 is spec-fixed, so its short form is already durable. if (local.NamespaceIndex == 0) return local.ToString() ?? string.Empty; var uri = namespaceUris.GetString(local.NamespaceIndex); if (string.IsNullOrEmpty(uri)) return local.ToString() ?? string.Empty; return new ExpandedNodeId(local, uri).ToString() ?? string.Empty; } }