예제 #1
0
        private static void addNode(OpcuaNodeLink opcuaNodeLink)
        {
            lock (lockObject)
            {
                OpcuaNodeLink nodeLink = opcuaNodeStructure.ChildrenNodes.Where(node => (node.nodeID.Equals(opcuaNodeLink.nodeID) &&
                                                                                         (node.parentNodeId.Equals(opcuaNodeLink.parentNodeId) &&
                                                                                          (node.browseName.Equals(opcuaNodeLink.browseName))))).FirstOrDefault();
                if (nodeLink == null)
                {
                    opcuaNodeStructure.ChildrenNodes.Add(opcuaNodeLink);
                }

                /*else
                 *  opcuaNodeStructure.ChildrenNodes[nodeId.ToString()].Add(opcuaNodeLink);*/
            }
        }
예제 #2
0
        public static void connectToOPCUAServer(String opcuaServerUrl)
        {
            #region SessionConfiguration
            EndpointConfiguration ec = new EndpointConfiguration();
            EndpointDescription   endpointDescription = new EndpointDescription(opcuaServerUrl);
            configuredEndpoint = new ConfiguredEndpoint(new ConfiguredEndpointCollection(), endpointDescription);
            configuredEndpoint.Configuration.UseBinaryEncoding = true;
            configuredEndpoint.Description.UserIdentityTokens.Add(new UserTokenPolicy(UserTokenType.Anonymous));

            applicationConfiguration = new ApplicationConfiguration();
            applicationConfiguration.ApplicationType = ApplicationType.Client;
            applicationConfiguration.ApplicationName = "SAPClientSession";
            applicationConfiguration.ApplicationUri  = "SAPClientSession";
            SecurityConfiguration secConfig = new SecurityConfiguration();
            secConfig.ApplicationCertificate = GetPCoDefaultCertificate();
            applicationConfiguration.SecurityConfiguration = secConfig;
            TransportQuotas transportQuotas = new TransportQuotas();
            applicationConfiguration.TransportQuotas = transportQuotas;
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            applicationConfiguration.ClientConfiguration = clientConfiguration;
            applicationConfiguration.Validate(ApplicationType.Client);

            CertificateValidationOptions certOptions = applicationConfiguration.SecurityConfiguration.TrustedPeerCertificates.ValidationOptions;
            certOptions |= CertificateValidationOptions.SuppressCertificateExpired;
            certOptions |= CertificateValidationOptions.SuppressHostNameInvalid;

            securelyUpdateEndpointConfiguration(configuredEndpoint, applicationConfiguration);
            applicationConfiguration.CertificateValidator.CertificateValidation += CertificateValidator_CertificateValidation;

            clientSession = Session.Create(
                applicationConfiguration,
                configuredEndpoint,
                false, false,
                sessionName,
                sessionTimeout,
                identity,
                null, null);

            clientSession.ReturnDiagnostics = DiagnosticsMasks.All;

            clientSession.KeepAliveInterval = 2 * 1000;

            try
            {
                int maxNodesPerReadRuntimeInformation = Convert.ToInt32(clientSession.ReadValue(Opc.Ua.VariableIds.Server_ServerCapabilities_OperationLimits_MaxNodesPerRead).Value);
                //if (tracer.Switch.ShouldTrace(TraceEventType.Verbose))
                {
                    String message = String.Format("Retrieved operation limits for reading ({0}) from server", maxNodesPerReadRuntimeInformation);
                    //TraceUtility.LogData(tracer, TraceUtility.EventId.E2718, TraceEventType.Verbose, message);
                }
            }
            catch (Exception)
            {
                // the value is not supplied or an error occured.
                {
                    String message = String.Format("Could not retrieve operation limits for reading from server");
                }
            }

            #endregion
            opcuaNodeStructure.ChildrenNodes = new List <OpcuaNodeLink>();

            //Add root node
            OpcuaNodeLink opcuaNodeLink = new OpcuaNodeLink();
            opcuaNodeLink.nodeID               = FindCurrentNode(null, clientSession, configuredEndpoint).ToString();
            opcuaNodeLink.browseName           = "AddressRoot";
            opcuaNodeLink.parentNodeId         = String.Empty;
            opcuaNodeLink.parentNodebrowseName = String.Empty;
            opcuaNodeLink.isMethod             = opcuaNodeLink.isArgument = false;
            opcuaNodeStructure.ChildrenNodes.Add(opcuaNodeLink);

            BrowseNode(FindCurrentNode(null, clientSession, configuredEndpoint).ToString(), opcuaNodeLink.browseName);
        }
예제 #3
0
        public static void BrowseNode(String currentNode, String browseName)
        {
            BrowseResultCollection      browseResultCollection = null;
            DiagnosticInfoCollection    diagnosticInfos;
            BrowseDescriptionCollection browseDescriptionCollection = null;
            List <NodeClass>            nodeClasses = new List <NodeClass>()
            {
                NodeClass.Unspecified
            };

            foreach (NodeClass nodeClass in nodeClasses)
            {
                browseDescriptionCollection = PrepareBrowseDescriptionCollection(currentNode, (uint)nodeClass);
                clientSession.Browse(
                    null,
                    null,
                    100,
                    browseDescriptionCollection,
                    out browseResultCollection,
                    out diagnosticInfos);

                if (browseResultCollection.Where(br => br.References.Count > 0).FirstOrDefault() == null)
                {
                    continue;
                }
                int    browseResultCounter = browseResultCollection.FindIndex(br => br.References.Count > 0);
                String referenceType       = String.Empty;

                foreach (BrowseResult browseResult in browseResultCollection)
                {
                    switch (browseResultCounter)
                    {
                    case 0:
                        referenceType = "Organizes";
                        break;

                    case 1:
                        referenceType = "HasComponent";
                        break;

                    case 2:
                        referenceType = "HasChild";
                        break;

                    case 3:
                        referenceType = "HasDescription";
                        break;

                    case 4:
                        referenceType = "HasProperty";
                        break;

                    default:
                        referenceType = "HasProperty";
                        break;
                    }

                    if (browseResult.References.Count > 0)
                    {
                        foreach (ReferenceDescription referenceDescription in browseResult.References)
                        {
                            if (referenceDescription.BrowseName.NamespaceIndex != UaDefaultNamespaceIndex)
                            {
                                BrowseNode(referenceDescription.NodeId.ToString(), referenceDescription.BrowseName.ToString());
                            }

                            //TBD - If a node id has different relations with its parent node, the additional "relation" should be added
                            //OpcuaNodeList opcuaNodeSubStructure = new OpcuaNodeList();
                            //opcuaNodeSubStructure.nodeId = referenceDescription.NodeId.ToString();
                            //opcuaNodeSubStructure.browseName = referenceDescription.BrowseName.ToString();

                            OpcuaNodeLink opcuaNodeLink = new OpcuaNodeLink();

                            opcuaNodeLink.nodeID               = referenceDescription.NodeId.ToString();
                            opcuaNodeLink.browseName           = referenceDescription.BrowseName.ToString();
                            opcuaNodeLink.parentNodeId         = currentNode;
                            opcuaNodeLink.parentNodebrowseName = browseName;

                            opcuaNodeLink.referenceType = referenceType;
                            if (referenceDescription.IsForward)
                            {
                                opcuaNodeLink.BrowseDirection = BrowseDirection.Forward.ToString("G");
                            }
                            opcuaNodeLink.NodeClassMask    = nodeClass.ToString("G");
                            opcuaNodeLink.browseResultMask = BrowseResultMask.All.ToString("G");

                            //if (!opcuaNodeStructure.IsChildPresent(referenceDescription.NodeId.ToString()))
                            if (opcuaNodeStructure.ChildrenNodes == null)
                            {
                                opcuaNodeStructure.ChildrenNodes = new List <OpcuaNodeLink>();
                            }

                            addNode(opcuaNodeLink);

                            if (referenceDescription.NodeClass == NodeClass.Method)
                            {
                                Node methodNode = clientSession.ReadNode((NodeId)(referenceDescription.NodeId.ToString()));

                                browseDescriptionCollection = PrepareBrowseDescriptionCollection(referenceDescription.NodeId.ToString(), (uint)nodeClass);
                                clientSession.Browse(
                                    null,
                                    null,
                                    100,
                                    browseDescriptionCollection,
                                    out browseResultCollection,
                                    out diagnosticInfos);

                                if (browseResultCollection.Where(br => br.References.Count > 0).FirstOrDefault() == null)
                                {
                                    continue;
                                }

                                String inputArgumentsNodeId = String.Empty, outputArgumentsNodeId = String.Empty;
                                foreach (BrowseResult br in browseResultCollection)
                                {
                                    foreach (ReferenceDescription rd in br.References)
                                    {
                                        OpcuaNodeLink nodeLink = new OpcuaNodeLink();

                                        nodeLink.nodeID     = rd.NodeId.ToString();
                                        nodeLink.browseName = rd.BrowseName.ToString();

                                        if (nodeLink.browseName == "InputArguments")
                                        {
                                            inputArgumentsNodeId = nodeLink.nodeID;
                                        }
                                        else if (nodeLink.browseName == "OutputArguments")
                                        {
                                            outputArgumentsNodeId = nodeLink.nodeID;
                                        }

                                        nodeLink.parentNodeId         = referenceDescription.NodeId.ToString();
                                        nodeLink.parentNodebrowseName = referenceDescription.BrowseName.ToString();

                                        nodeLink.referenceType = referenceType;
                                        if (rd.IsForward)
                                        {
                                            opcuaNodeLink.BrowseDirection = BrowseDirection.Forward.ToString("G");
                                        }
                                        nodeLink.NodeClassMask    = nodeClass.ToString("G");
                                        nodeLink.browseResultMask = BrowseResultMask.All.ToString("G");

                                        //if (!opcuaNodeStructure.IsChildPresent(referenceDescription.NodeId.ToString()))
                                        if (opcuaNodeStructure.ChildrenNodes == null)
                                        {
                                            opcuaNodeStructure.ChildrenNodes = new List <OpcuaNodeLink>();
                                        }

                                        addNode(nodeLink);
                                    }
                                }

                                opcuaNodeLink.isMethod = true;

                                BrowsePathCollection pathsToArgs     = new BrowsePathCollection();
                                BrowsePath           pathToInputArgs = new BrowsePath();
                                pathToInputArgs.StartingNode = methodNode.NodeId;
                                pathToInputArgs.RelativePath = new RelativePath(ReferenceTypeIds.HasProperty, false, true, new QualifiedName("InputArguments"));
                                pathsToArgs.Add(pathToInputArgs);

                                BrowsePath pathToOutputArgs = new BrowsePath();
                                pathToOutputArgs.StartingNode = methodNode.NodeId;
                                pathToOutputArgs.RelativePath = new RelativePath(ReferenceTypeIds.HasProperty, false, true, new QualifiedName("OutputArguments"));
                                pathsToArgs.Add(pathToOutputArgs);

                                BrowsePathResultCollection results = null;
                                // Get the nodeId of the input argument
                                ResponseHeader responseHeader = clientSession.TranslateBrowsePathsToNodeIds(
                                    null,
                                    pathsToArgs,
                                    out results,
                                    out diagnosticInfos
                                    );

                                ArgumentCollection[] arguments = new ArgumentCollection[2];
                                for (int i = 0; i < 2; i++)
                                {
                                    arguments[i] = new ArgumentCollection();
                                    foreach (BrowsePathTarget bptarget in results[i].Targets)
                                    {
                                        DataValueCollection readResults = null;

                                        ReadValueId nodeToRead = new ReadValueId();
                                        nodeToRead.NodeId      = (NodeId)bptarget.TargetId;
                                        nodeToRead.AttributeId = Attributes.Value;
                                        ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
                                        nodesToRead.Add(nodeToRead);

                                        DiagnosticInfoCollection readDiagnoistcInfos = null;

                                        clientSession.Read(
                                            null,
                                            0,
                                            TimestampsToReturn.Neither,
                                            nodesToRead,
                                            out readResults,
                                            out readDiagnoistcInfos
                                            );

                                        ExtensionObject[] exts = (ExtensionObject[])readResults[0].Value;
                                        for (int j = 0; j < exts.Length; ++j)
                                        {
                                            ExtensionObject ext = exts[j];
                                            arguments[i].Add((Argument)ext.Body);

                                            OpcuaNodeLink nl = new OpcuaNodeLink();

                                            nl.nodeID     = ((Opc.Ua.Argument)ext.Body).BinaryEncodingId.ToString();
                                            nl.browseName = ((Opc.Ua.Argument)ext.Body).Name;

                                            if (((Opc.Ua.Argument)ext.Body).Description.Text.Equals("Input argument"))
                                            {
                                                nl.parentNodeId = inputArgumentsNodeId;
                                            }
                                            else if (((Opc.Ua.Argument)ext.Body).Description.Text.Equals("Output argument"))
                                            {
                                                nl.parentNodeId = outputArgumentsNodeId;
                                            }

                                            nl.parentNodebrowseName = referenceDescription.BrowseName.ToString();

                                            nl.referenceType = referenceType;
                                            if (referenceDescription.IsForward)
                                            {
                                                opcuaNodeLink.BrowseDirection = BrowseDirection.Forward.ToString("G");
                                            }
                                            nl.NodeClassMask    = NodeClass.Variable.ToString("G");
                                            nl.browseResultMask = BrowseResultMask.All.ToString("G");
                                            nl.isArgument       = true;

                                            //if (!opcuaNodeStructure.IsChildPresent(referenceDescription.NodeId.ToString()))
                                            if (opcuaNodeStructure.ChildrenNodes == null)
                                            {
                                                opcuaNodeStructure.ChildrenNodes = new List <OpcuaNodeLink>();
                                            }

                                            addNode(nl);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }