Exemplo n.º 1
0
        public async Task BrowseObjects()
        {
            var channel = new UaTcpSessionChannel(
                localDescription,
                certificateStore,
                new AnonymousIdentity(),
                EndpointUrl,
                SecurityPolicyUris.None,
                loggerFactory: loggerFactory);

            await channel.OpenAsync();

            var rds           = new List <ReferenceDescription>();
            var browseRequest = new BrowseRequest {
                NodesToBrowse = new[] { new BrowseDescription {
                                            NodeId = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ObjectsFolder), channel.NamespaceUris), ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HierarchicalReferences), ResultMask = (uint)BrowseResultMask.TargetInfo, NodeClassMask = (uint)NodeClass.Unspecified, BrowseDirection = BrowseDirection.Forward, IncludeSubtypes = true
                                        } }, RequestedMaxReferencesPerNode = 1000
            };
            var browseResponse = await channel.BrowseAsync(browseRequest).ConfigureAwait(false);

            rds.AddRange(browseResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));
            var continuationPoints = browseResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();

            while (continuationPoints.Length > 0)
            {
                var browseNextRequest = new BrowseNextRequest {
                    ContinuationPoints = continuationPoints, ReleaseContinuationPoints = false
                };
                var browseNextResponse = await channel.BrowseNextAsync(browseNextRequest);

                rds.AddRange(browseNextResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));
                continuationPoints = browseNextResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();
            }

            rds
            .Should().NotBeEmpty();

            logger.LogInformation("+ Objects, 0:Objects, Object");
            foreach (var rd in rds)
            {
                logger.LogInformation("   + {0}, {1}, {2}", rd.DisplayName, rd.BrowseName, rd.NodeClass);
            }

            logger.LogInformation($"Closing session '{channel.SessionId}'.");
            await channel.CloseAsync();
        }
Exemplo n.º 2
0
        public async Task BrowseRoot()
        {
            var channel = new UaTcpSessionChannel(
                this.localDescription,
                this.certificateStore,
                new AnonymousIdentity(),
                EndpointUrl,
                SecurityPolicyUris.None,
                loggerFactory: this.loggerFactory);

            await channel.OpenAsync();

            var rds           = new List <ReferenceDescription>();
            var browseRequest = new BrowseRequest {
                NodesToBrowse = new[] { new BrowseDescription {
                                            NodeId = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.RootFolder), channel.NamespaceUris), ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HierarchicalReferences), ResultMask = (uint)BrowseResultMask.TargetInfo, NodeClassMask = (uint)NodeClass.Unspecified, BrowseDirection = BrowseDirection.Forward, IncludeSubtypes = true
                                        } }, RequestedMaxReferencesPerNode = 1000
            };
            var browseResponse = await channel.BrowseAsync(browseRequest).ConfigureAwait(false);

            rds.AddRange(browseResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));
            var continuationPoints = browseResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();

            while (continuationPoints.Length > 0)
            {
                var browseNextRequest = new BrowseNextRequest {
                    ContinuationPoints = continuationPoints, ReleaseContinuationPoints = false
                };
                var browseNextResponse = await channel.BrowseNextAsync(browseNextRequest);

                rds.AddRange(browseNextResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));
                continuationPoints = browseNextResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();
            }

            Assert.IsTrue(rds.Count == 3);

            Console.WriteLine($"Closing session '{channel.SessionId}'.");
            await channel.CloseAsync();
        }
Exemplo n.º 3
0
        private async Task <IList <ReferenceDescription> > BrowseTree(NodeId tree)
        {
            var browseRequest = new BrowseRequest {
                NodesToBrowse = new[] {
                    new BrowseDescription {
                        NodeId          = tree,
                        ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HierarchicalReferences),
                        ResultMask      = (uint)BrowseResultMask.TargetInfo,
                        NodeClassMask   = (uint)NodeClass.Object | (int)NodeClass.Variable,
                        BrowseDirection = BrowseDirection.Forward,
                        IncludeSubtypes = true
                    }
                },
                RequestedMaxReferencesPerNode = 1000
            };

            var            rds            = new List <ReferenceDescription>();
            BrowseResponse browseResponse = await connection.BrowseAsync(browseRequest);

            rds.AddRange(browseResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));

            var continuationPoints = browseResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();

            while (continuationPoints.Length > 0 && connection != null)
            {
                var browseNextRequest = new BrowseNextRequest {
                    ContinuationPoints = continuationPoints, ReleaseContinuationPoints = false
                };
                var browseNextResponse = await connection.BrowseNextAsync(browseNextRequest);

                rds.AddRange(browseNextResponse.Results.Where(result => result.References != null).SelectMany(result => result.References));
                continuationPoints = browseNextResponse.Results.Select(br => br.ContinuationPoint).Where(cp => cp != null).ToArray();
            }

            return(rds);
        }
        public async Task BrowseObjectFolder()
        {
            // describe this client application.
            var clientDescription = new ApplicationDescription
            {
                ApplicationName = "Workstation.UaClient.FeatureTests",
                ApplicationUri  = $"urn:{System.Net.Dns.GetHostName()}:Workstation.UaClient.FeatureTests",
                ApplicationType = ApplicationType.Client
            };

            // place to store certificates
            var certificateStore = new DirectoryStore("./pki");

            // create a 'UaTcpSessionChannel', a client-side channel that opens a 'session' with the server.
            var channel = new UaTcpSessionChannel(
                clientDescription,
                certificateStore,
                new AnonymousIdentity(),
                "opc.tcp://localhost:48010"); // the endpoint of Unified Automation's UaCPPServer.

            try
            {
                // try opening a session and reading a few nodes.
                await channel.OpenAsync();

                Console.WriteLine($"Opened session with endpoint '{channel.RemoteEndpoint.EndpointUrl}'.");
                Console.WriteLine($"SecurityPolicy: '{channel.RemoteEndpoint.SecurityPolicyUri}'.");
                Console.WriteLine($"SecurityMode: '{channel.RemoteEndpoint.SecurityMode}'.");
                Console.WriteLine($"UserIdentityToken: '{channel.UserIdentity}'.");

                // build a BrowseRequest. See 'OPC UA Spec Part 4' section 5.8.2
                var browseRequest = new BrowseRequest
                {
                    NodesToBrowse = new[] {
                        new BrowseDescription {
                            // gather references of this nodeid.
                            NodeId = NodeId.Parse(ObjectIds.ObjectsFolder),
                            // include just 'Forward' references
                            BrowseDirection = BrowseDirection.Forward,
                            // include 'HierarchicalReferences'
                            ReferenceTypeId = NodeId.Parse(ReferenceTypeIds.HierarchicalReferences),
                            // include 'HierarchicalReferences' and all subtypes of 'HierarchicalReferences'
                            IncludeSubtypes = true,
                            // include all classes of node
                            NodeClassMask = (uint)NodeClass.Unspecified,
                            // return reference descriptions with all the fields filled out
                            ResultMask = (uint)BrowseResultMask.All,
                        }
                    },
                    RequestedMaxReferencesPerNode = 1000
                };

                // send the request to the server
                var browseResponse = await channel.BrowseAsync(browseRequest).ConfigureAwait(false);

                Console.WriteLine("\n+ Objects, 0:Objects, Object, i=85");

                Assert.IsNotNull(browseResponse.Results[0].References);
                foreach (var rd in browseResponse.Results[0].References)
                {
                    Console.WriteLine("   + {0}, {1}, {2}, {3}", rd.DisplayName, rd.BrowseName, rd.NodeClass, rd.NodeId);
                }

                // it is good practice to be prepared to receive a continuationPoint.
                // ContinuationPoints are returned when the server has more information
                // than can be delivered in current response.
                // To test this code, you can reduce the above RequestedMaxReferencesPerNode
                // to 1.
                var cp = browseResponse.Results[0].ContinuationPoint;
                while (cp != null)
                {
                    var browseNextRequest = new BrowseNextRequest {
                        ContinuationPoints = new[] { cp }, ReleaseContinuationPoints = false
                    };
                    var browseNextResponse = await channel.BrowseNextAsync(browseNextRequest);

                    Assert.IsNotNull(browseNextResponse.Results[0].References);
                    foreach (var rd in browseNextResponse.Results[0].References)
                    {
                        Console.WriteLine("   + {0}, {1}, {2}", rd.DisplayName, rd.BrowseName, rd.NodeClass);
                    }
                    cp = browseNextResponse.Results[0].ContinuationPoint;
                }

                Console.WriteLine($"\nClosing session '{channel.SessionId}'.");
                await channel.CloseAsync();
            }
            catch (Exception ex)
            {
                await channel.AbortAsync();

                Console.WriteLine(ex.Message);
            }
        }