コード例 #1
0
        public List<EasyUAMonitoredItemArguments> readConf(String[] args)
        {
            var lines = File.ReadLines(args[0]).Select(a => a.Split(';'));
            var csv = from line in lines
                      select (from piece in line
                              select piece);
            //Build a list of the nodeId's
            List<EasyUAMonitoredItemArguments> nodeList = new List<EasyUAMonitoredItemArguments>();
            foreach (var item in csv) {
                UANodeId nid = new UANodeId("ns=2;s=" + item.ToArray<String>()[0] + "." + item.ToArray<String>()[1] + "." + item.ToArray<String>()[2]);
                UAEndpointDescriptor ud = new UAEndpointDescriptor("opc.tcp://test-opc-ua.powerhub.dk:32402");

                EasyUAMonitoredItemArguments euia = new EasyUAMonitoredItemArguments(null, ud, nid);

                nodeList.Add(euia);
            }

            return nodeList;
        }
コード例 #2
0
        public List <EasyUAMonitoredItemArguments> readConf(String[] args)
        {
            var lines = File.ReadLines(args[0]).Select(a => a.Split(';'));
            var csv   = from line in lines
                        select(from piece in line
                               select piece);

            //Build a list of the nodeId's
            List <EasyUAMonitoredItemArguments> nodeList = new List <EasyUAMonitoredItemArguments>();

            foreach (var item in csv)
            {
                UANodeId             nid = new UANodeId("ns=2;s=" + item.ToArray <String>()[0] + "." + item.ToArray <String>()[1] + "." + item.ToArray <String>()[2]);
                UAEndpointDescriptor ud  = new UAEndpointDescriptor("opc.tcp://test-opc-ua.powerhub.dk:32402");

                EasyUAMonitoredItemArguments euia = new EasyUAMonitoredItemArguments(null, ud, nid);

                nodeList.Add(euia);
            }

            return(nodeList);
        }
コード例 #3
0
            // A node ID specifies a namespace (either by an URI or by an index), and an identifier.
            // The identifier can be numeric (an integer), string, GUID, or opaque.
            public static void Main1()
            {
                // A node ID can be specified in string form (so-called expanded text).
                // The code below specifies a namespace URI (nsu=...), and an integer identifier (i=...).
                UANodeId nodeId1 = new UANodeId("nsu=http://test.org/UA/Data/;i=10853");

                Console.WriteLine(nodeId1);


                // Similarly, with a string identifier (s=...).
                UANodeId nodeId2 = new UANodeId("nsu=http://test.org/UA/Data/;s=someIdentifier");

                Console.WriteLine(nodeId2);


                // Actually, "s=" can be omitted (not recommended, though)
                UANodeId nodeId3 = new UANodeId("nsu=http://test.org/UA/Data/;someIdentifier");

                Console.WriteLine(nodeId3);


                // Similarly, with a GUID identifier (g=...)
                UANodeId nodeId4 = new UANodeId("nsu=http://test.org/UA/Data/;g=BAEAF004-1E43-4A06-9EF0-E52010D5CD10");

                Console.WriteLine(nodeId4);


                // Similarly, with an opaque identifier (b=..., in Base64 encoding).
                UANodeId nodeId5 = new UANodeId("nsu=http://test.org/UA/Data/;b=AP8=");

                Console.WriteLine(nodeId5);


                // Namespace index can be used instead of namespace URI. The server is allowed to change the namespace
                // indices between sessions, and for this reason, you should avoid the use of namespace indices, and
                // rather use the namespace URIs whenever possible.
                UANodeId nodeId6 = new UANodeId("ns=2;i=10853");

                Console.WriteLine(nodeId6);


                // Namespace index can be also specified together with namespace URI. This is still safe, but may be
                // a bit quicker to perform, because the client can just verify the namespace URI instead of looking
                // it up.
                UANodeId nodeId7 = new UANodeId("nsu=http://test.org/UA/Data/;ns=2;i=10853");

                Console.WriteLine(nodeId7);


                // When neither namespace URI nor namespace index are given, the node ID is assumed to be in namespace
                // with index 0 and URI "http://opcfoundation.org/UA/", which is reserved by OPC UA standard. There are
                // many standard nodes that live in this reserved namespace, but no nodes specific to your servers will
                // be in the reserved namespace, and hence the need to specify the namespace with server-specific nodes.
                UANodeId nodeId8 = new UANodeId("i=2254");

                Console.WriteLine(nodeId8);


                // If you attempt to pass in a string that does not conform to the syntax rules,
                // a UANodeIdFormatException is thrown.
                try
                {
                    UANodeId nodeId9 = new UANodeId("nsu=http://test.org/UA/Data/;i=notAnInteger");
                    Console.WriteLine(nodeId9);
                }
                catch (UANodeIdFormatException nodeIdFormatException)
                {
                    Console.WriteLine(nodeIdFormatException.Message);
                }


                // There is a parser object that can be used to parse the expanded textx of node IDs.
                UANodeIdParser nodeIdParser10 = new UANodeIdParser();
                UANodeId       nodeId10       = nodeIdParser10.Parse("nsu=http://test.org/UA/Data/;i=10853");

                Console.WriteLine(nodeId10);


                // The parser can be used if you want to parse the expanded text of the node ID but do not want
                // exceptions be thrown.
                UANodeIdParser      nodeIdParser11 = new UANodeIdParser();
                UANodeId            nodeId11;
                IStringParsingError stringParsingError =
                    nodeIdParser11.TryParse("nsu=http://test.org/UA/Data/;i=notAnInteger", out nodeId11);

                if (stringParsingError == null)
                {
                    Console.WriteLine(nodeId11);
                }
                else
                {
                    Console.WriteLine(stringParsingError.Message);
                }


                // You can also use the parser if you have node IDs where you want the default namespace be different
                // from the standard "http://opcfoundation.org/UA/".
                UANodeIdParser nodeIdParser12 = new UANodeIdParser("http://test.org/UA/Data/");
                UANodeId       nodeId12       = nodeIdParser12.Parse("i=10853");

                Console.WriteLine(nodeId12);


                // The namespace URI string (or the namespace index, or both) and the identifier can be passed to the
                // constructor separately.
                UANodeId nodeId13 = new UANodeId("http://test.org/UA/Data/", 10853);

                Console.WriteLine(nodeId13);


                // You can create a "null" node ID. Such node ID does not actually identify any valid node in OPC UA, but
                // is useful as a placeholder or as a starting point for further modifications of its properties.
                UANodeId nodeId14 = new UANodeId();

                Console.WriteLine(nodeId14);


                // Properties of a node ID can be modified individually. The advantage of this approach is that you do
                // not have to care about syntax of the node ID expanded text.
                UANodeId nodeId15 = new UANodeId();

                nodeId15.NamespaceUriString = "http://test.org/UA/Data/";
                nodeId15.Identifier         = 10853;
                Console.WriteLine(nodeId15);


                // The same as above, but using an object initializer list.
                UANodeId nodeId16 = new UANodeId
                {
                    NamespaceUriString = "http://test.org/UA/Data/",
                    Identifier         = 10853
                };

                Console.WriteLine(nodeId16);


                // If you know the type of the identifier upfront, it is safer to use typed properties that correspond
                // to specific types of identifier. Here, with an integer identifier.
                UANodeId nodeId17 = new UANodeId();

                nodeId17.NamespaceUriString = "http://test.org/UA/Data/";
                nodeId17.NumericIdentifier  = 10853;
                Console.WriteLine(nodeId17);


                // Similarly, with a string identifier.
                UANodeId nodeId18 = new UANodeId();

                nodeId18.NamespaceUriString = "http://test.org/UA/Data/";
                nodeId18.StringIdentifier   = "someIdentifier";
                Console.WriteLine(nodeId18);


                // Similarly, with a GUID identifier.
                UANodeId nodeId19 = new UANodeId();

                nodeId19.NamespaceUriString = "http://test.org/UA/Data/";
                nodeId19.GuidIdentifier     = Guid.Parse("BAEAF004-1E43-4A06-9EF0-E52010D5CD10");
                Console.WriteLine(nodeId19);


                // If you have GUID in its string form, the node ID object can parse it for you.
                UANodeId nodeId20 = new UANodeId();

                nodeId20.NamespaceUriString   = "http://test.org/UA/Data/";
                nodeId20.GuidIdentifierString = "BAEAF004-1E43-4A06-9EF0-E52010D5CD10";
                Console.WriteLine(nodeId20);


                // And, with an opaque identifier.
                UANodeId nodeId21 = new UANodeId();

                nodeId21.NamespaceUriString = "http://test.org/UA/Data/";
                nodeId21.OpaqueIdentifier   = new byte[] { 0x00, 0xFF };
                Console.WriteLine(nodeId21);


                // Assigning an expanded text to a node ID parses the value being assigned and sets all corresponding
                // properties accordingly.
                UANodeId nodeId22 = new UANodeId();

                nodeId22.ExpandedText = "nsu=http://test.org/UA/Data/;i=10853";
                Console.WriteLine(nodeId22);


                // There is an implicit conversion from a string (representing the expanded text) to a node ID.
                // You can therefore use the expanded text (string) in place of any node ID object directly.
                UANodeId nodeId23 = "nsu=http://test.org/UA/Data/;i=10853";

                Console.WriteLine(nodeId23);


                // There is a copy constructor as well, creating a clone of an existing node ID.
                UANodeId nodeId24a = new UANodeId("nsu=http://test.org/UA/Data/;i=10853");

                Console.WriteLine(nodeId24a);
                UANodeId nodeId24b = new UANodeId(nodeId24a);

                Console.WriteLine(nodeId24b);


                // We have provided static classes with properties that correspond to all standard nodes specified by
                // OPC UA. You can simply refer to these node IDs in your code.
                // The class names are UADataTypeIds, UAMethodIds, UAObjectIds, UAObjectTypeIds, UAReferenceTypeIds,
                // UAVariableIds and UAVariableTypeIds.
                UANodeId nodeId25 = UAObjectIds.TypesFolder;

                Console.WriteLine(nodeId25);


                // You can also refer to any standard node using its name (in a string form).
                // Note that assigning a non-existing standard name is not allowed, and throws ArgumentException.
                UANodeId nodeId26 = new UANodeId();

                nodeId26.StandardName = "TypesFolder";
                Console.WriteLine(nodeId26);


                // When you browse for nodes in the OPC UA server, every returned node element contains a node ID that
                // you can use further.
                var client27 = new EasyUAClient();
                UANodeElementCollection nodeElementCollection27 = client27.BrowseNodes(
                    "http://opcua.demo-this.com:51211/UA/SampleServer",
                    UAObjectIds.Server,
                    new UABrowseParameters(UANodeClass.All, new[] { UAReferenceTypeIds.References }));
                UANodeId nodeId27 = nodeElementCollection27[0].NodeId;

                Console.WriteLine(nodeId27);


                // As above, but using a constructor that takes a node element as an input.
                var client28 = new EasyUAClient();
                UANodeElementCollection nodeElementCollection28 = client28.BrowseNodes(
                    "http://opcua.demo-this.com:51211/UA/SampleServer",
                    UAObjectIds.Server,
                    new UABrowseParameters(UANodeClass.All, new[] { UAReferenceTypeIds.References }));
                UANodeId nodeId28 = new UANodeId(nodeElementCollection28[0]);

                Console.WriteLine(nodeId28);


                // Or, there is an explicit conversion from a node element as well.
                var client29 = new EasyUAClient();
                UANodeElementCollection nodeElementCollection29 = client29.BrowseNodes(
                    "http://opcua.demo-this.com:51211/UA/SampleServer",
                    UAObjectIds.Server,
                    new UABrowseParameters(UANodeClass.All, new[] { UAReferenceTypeIds.References }));
                UANodeId nodeId29 = (UANodeId)nodeElementCollection29[0];

                Console.WriteLine(nodeId29);
            }
コード例 #4
0
            public static void Main1()
            {
                // Instantiate the client object
                var easyUAClient = new EasyUAClient();

                UANodeId nodeId = null;

                byte[] eventId = null;
                var    anEvent = new ManualResetEvent(initialState: false);

                Console.WriteLine("Subscribing...");
                easyUAClient.SubscribeEvent(
                    "opc.tcp://opcua.demo-this.com:62544/Quickstarts/AlarmConditionServer",
                    UAObjectIds.Server,
                    1000,
                    new UAEventFilterBuilder(
                        UAFilterElements.Equals(
                            UABaseEventObject.Operands.NodeId,
                            new UANodeId(expandedText: "nsu=http://opcfoundation.org/Quickstarts/AlarmCondition;ns=2;s=1:Colours/EastTank?Yellow")),
                        UABaseEventObject.AllFields),
                    (sender, eventArgs) =>
                {
                    if (!eventArgs.Succeeded)
                    {
                        Console.WriteLine(eventArgs.ErrorMessageBrief);
                        return;
                    }
                    if (eventArgs.EventData != null)
                    {
                        UABaseEventObject baseEventObject = eventArgs.EventData.BaseEvent;
                        Console.WriteLine(baseEventObject);

                        // Make sure we do not catch the event more than once
                        if (anEvent.WaitOne(0))
                        {
                            return;
                        }

                        nodeId  = baseEventObject.NodeId;
                        eventId = baseEventObject.EventId;

                        anEvent.Set();
                    }
                },
                    state: null);

                Console.WriteLine("Waiting for an event for 30 seconds...");
                if (!anEvent.WaitOne(30 * 1000))
                {
                    Console.WriteLine("Event not received");
                    return;
                }

                Console.WriteLine("Acknowledging an event...");
                easyUAClient.Acknowledge(
                    "opc.tcp://opcua.demo-this.com:62544/Quickstarts/AlarmConditionServer",
                    nodeId,
                    eventId,
                    "Acknowledged by an automated example code");

                Console.WriteLine("Waiting for 5 seconds...");
                Thread.Sleep(5 * 1000);

                Console.WriteLine("Unsubscribing...");
                easyUAClient.UnsubscribeAllMonitoredItems();

                Console.WriteLine("Waiting for 5 seconds...");
                Thread.Sleep(5 * 1000);
            }