internal static async Task ReadNodeAsync()

in src/Azure.IIoT.OpcUa.Publisher/src/Stack/Extensions/SessionEx.cs [1093:1216]


        internal static async Task<(NodeModel, ServiceResultModel?)> ReadNodeAsync(
            this IOpcUaSession session, RequestHeader header, NodeId nodeId, NamespaceFormat namespaceFormat,
            Opc.Ua.NodeClass? nodeClass = null, bool skipValue = true, bool? children = null,
            CancellationToken ct = default)
        {
            var results = await session.ReadNodeAttributesAsync(header, nodeId, skipValue,
                nodeClass, ct).ConfigureAwait(false);
            var lookup = results.AsLookupTable();

            var id = nodeId.AsString(session.MessageContext, namespaceFormat);
            System.Diagnostics.Debug.Assert(id != null);
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(id));
            lookup.TryGetValue(Attributes.Value, out var value);
            var nodeModel = new NodeModel
            {
                Children = children,
                NodeId = id,
                Value = value.Item1 == null ? null :
                    session.Codec.Encode(
                        value.Item1.WrappedValue, out var type),
                SourceTimestamp =
                    value.Item1?.SourceTimestamp,
                SourcePicoseconds =
                    value.Item1?.SourcePicoseconds,
                ServerTimestamp =
                    value.Item1?.ServerTimestamp,
                ServerPicoseconds =
                    value.Item1?.ServerPicoseconds,
                ErrorInfo =
                    value.Item2,
                BrowseName =
                    lookup[Attributes.BrowseName].Item1?
                        .GetValueOrDefaultEx<QualifiedName>()?
                        .AsString(session.MessageContext, namespaceFormat),
                DisplayName =
                    lookup[Attributes.DisplayName].Item1?
                        .GetValueOrDefaultEx<LocalizedText>()?
                        .ToString(),
                Description =
                    lookup[Attributes.Description].Item1?
                        .GetValueOrDefaultEx<LocalizedText>()?
                        .ToString(),
                NodeClass =
                    lookup[Attributes.NodeClass].Item1?
                        .GetValueOrDefaultEx<Opc.Ua.NodeClass>()
                        .ToServiceType(),
                AccessRestrictions = (NodeAccessRestrictions?)
                    lookup[Attributes.AccessRestrictions].Item1?
                        .GetValueOrDefaultEx<ushort?>(v => v == 0 ? null : v),
                UserWriteMask =
                    lookup[Attributes.UserWriteMask].Item1?
                        .GetValueOrDefaultEx<uint?>(),
                WriteMask =
                    lookup[Attributes.WriteMask].Item1?
                        .GetValueOrDefaultEx<uint?>(),
                DataType =
                    lookup[Attributes.DataType].Item1?
                        .GetValueOrDefaultEx<NodeId>()?
                        .AsString(session.MessageContext, namespaceFormat),
                ArrayDimensions =
                    lookup[Attributes.ArrayDimensions].Item1?
                        .GetValueOrDefaultEx<uint[]?>(),
                ValueRank = (NodeValueRank?)
                    lookup[Attributes.ValueRank].Item1?
                        .GetValueOrDefaultEx<int?>(),
                AccessLevel = (NodeAccessLevel?)
                    lookup[Attributes.AccessLevelEx].Item1?
                        .GetValueOrDefaultEx<uint?>(l =>
                        {
                            // Or both if available
                            var v = (l ?? 0) |
                            lookup[Attributes.AccessLevel].Item1?
                                .GetValueOrDefaultEx<byte?>(b => b ?? 0);
                            return v == 0 ? null : v;
                        }),
                UserAccessLevel = (NodeAccessLevel?)
                    lookup[Attributes.UserAccessLevel].Item1?
                        .GetValueOrDefaultEx<byte?>(),
                Historizing =
                    lookup[Attributes.Historizing].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                MinimumSamplingInterval =
                    lookup[Attributes.MinimumSamplingInterval].Item1?
                        .GetValueOrDefaultEx<double?>(),
                IsAbstract =
                    lookup[Attributes.IsAbstract].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                EventNotifier = (NodeEventNotifier?)
                    lookup[Attributes.EventNotifier].Item1?
                        .GetValueOrDefaultEx<byte?>(v => v == 0 ? null : v),
                DataTypeDefinition = session.Codec.Encode(
                    lookup[Attributes.DataTypeDefinition].Item1?
                        .GetValueOrDefaultEx<ExtensionObject>(), out _),
                InverseName =
                    lookup[Attributes.InverseName].Item1?
                        .GetValueOrDefaultEx<LocalizedText>()?
                        .ToString(),
                Symmetric =
                    lookup[Attributes.Symmetric].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                ContainsNoLoops =
                    lookup[Attributes.ContainsNoLoops].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                Executable =
                    lookup[Attributes.Executable].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                UserExecutable =
                    lookup[Attributes.UserExecutable].Item1?
                        .GetValueOrDefaultEx<bool?>(),
                UserRolePermissions =
                    lookup[Attributes.UserRolePermissions].Item1?
                        .GetValueOrDefaultEx<ExtensionObject[]>()?
                        .Select(ex => ex.Body)
                        .OfType<RolePermissionType>()
                        .Select(p => p.ToServiceModel(session.MessageContext, namespaceFormat)).ToList(),
                RolePermissions =
                    lookup[Attributes.RolePermissions].Item1?
                        .GetValueOrDefaultEx<ExtensionObject[]>()?
                        .Select(ex => ex.Body)
                        .OfType<RolePermissionType>()
                        .Select(p => p.ToServiceModel(session.MessageContext, namespaceFormat)).ToList()
            };
            return (nodeModel, results.ErrorInfo ?? lookup[Attributes.NodeClass].Item2);
        }