Artifact [52d404f327]
Not logged in

Artifact 52d404f327b90a75dc58873cd394647081f294d7:

Wiki page [topcua] by chw 2020-07-30 05:05:06.
D 2020-07-30T05:05:06.495
L topcua
P 15644d8ab9938e255e6964b00119b3c31df86ef1
U chw
W 33682
<h2>opcua command</h2>

<h3>Name</h3>

<b>opcua</b> - Tcl binding to the OPC/UA implementation of <b>http://www.open62541.org</b>

<h3>Synopsis</h3>

<tt>package require topcua</tt><br>
<tt>opcua <i>cmd</i> ?<i>arg</i>?</tt><br>

<h3>Description</h3>

This command provides several operations to manage and communicate using the OPC/UA implementation of <b>http://www.open62541.org</b>. It is available on common POSIX and Windows platforms. <tt><i>cmd</i></tt> indicates which operation  to carry out. Any unique abbreviation for <tt><i>cmd</i></tt> is acceptable. The valid commands are:

<tt>opcua acl <i>handle ?user pass ...?</i></tt>

    Modifies the user/password based access control list of the server object  <tt><i>handle</i></tt>. This command must be called after the server object has been created (see <tt>opcua new server</tt>) and before it is put into operation (see <tt>opcua start</tt>).

<tt>opcua add <i>handle</i> DataType <i>nodeid parent reftype brname ?attrs?</i></tt>

    Adds an new node of node class <tt>DataType</tt> in the server  object <tt><i>handle</i></tt> and returns the node identifer. The parameter <tt><i>nodeid</i></tt> is the requested new node identifier of the  node  to  be  created. <tt><i>parent</i></tt> is the parent node identifier and <tt><i>reftype</i></tt> the reference type or node identifier of the reference between the parent and the new node. <tt><i>brname</i></tt> is the browse name (see section <b>Qualified Names</b>) of the new node. The optional <tt><i>attrs</i></tt> parameter specifies attributes for the new node in form of a dictionary (see <tt>opcua attr default</tt>). If it is omitted, default values  are used. The <tt><i>DisplayName</i></tt> attribute if left empty is preset to the name part of the browse name parameter.

<tt>opcua add <i>handle</i> Method <i>nodeid parent reftype outargs brname inargs cmd ?attrs?</i></tt>

    Adds an new node of node class <tt>Method</tt> in the server object <tt><i>handle</i></tt> and returns the node identifer. The parameter <tt><i>outargs</i></tt> describes the output arguments of the method as a list  of zero or more pairs of data type and argument name. Likewise, <tt><i>inargs</i></tt> describes the input arguments of the method. The  parameter <tt><i>cmd</i></tt> is the Tcl callback to handle the method invocation, see section <b>Method Callbacks</b> for more information. For the  other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> Namespace <i>name</i></tt>

    Adds the new namespace <tt><i>name</i></tt> to the server object <tt><i>handle</i></tt> and returns a numeric identifier for this namespace.

<tt>opcua add <i>handle</i> Object <i>nodeid parent reftype brname ?typeid attrs?</i></tt>

    Adds an new node of node class <tt>Object</tt> in the server object <tt><i>handle</i></tt> and returns the node identifer. The optional parameter <tt><i>typeid</i></tt> must be a known data type name (see  <tt>opcua  types</tt>) or a node  identifier of a data type. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> ObjectType <i>nodeid parent reftype brname ?attrs?</i></tt>

    Adds an new node of node class <tt>ObjectType</tt> in the  server object <tt><i>handle</i></tt> and returns the node identifer. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> Reference <i>srcid reftype target ?forward?</i></tt>

    Adds a reference of type <tt><i>reftype</i></tt> (see <tt>opcua reftype</tt>) between the node identifiers <tt><i>srcid</i></tt> and <tt><<i>target</i></tt> on the server object <tt><i>handle</i></tt>. The optional parameter <tt><i>forward</i></tt> must be a boolean indicating the direction of the reference (true, the default, is forward, false is inverse).

<tt>opcua add <i>handle</i> ReferenceType <i>nodeid parent reftype brname ?attrs?</i></tt>

    Adds an new node of node class <tt>ReferenceType</tt> in the server object  <tt><i>handle</i></tt> and returns the node identifer. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> Variable <i>nodeid parent reftype brname ?typeid  attrs cmd?</i></tt>

    Adds an new node of node class <tt>Variable</tt> in the server object <tt><i>handle</i></tt> and returns the node identifer. The optional parameter <tt><i>typeid</i></tt> must be a known data type name (see <tt>opcua types</tt>)  or a node identifier of a data type or an empty string for a default value. Parameter <tt><i>cmd</i></tt> is an optional data source callback which produces  (read operation) or consumes (write operation) the variable's value. See section <b>Data Source Callbacks</b> for  more information. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> VariableType <i>nodeid parent reftype brname ?typeid attrs?</i></tt>

    Adds an new node of node class <tt>VariableType</tt> in the server object <tt><i>handle</i></tt> and returns the node identifer. The optional parameter <tt><i>typeid</i></tt> must be a known data type name (see <tt>opcua types</tt>) or a node identifier of a data type or an empty string for a default value. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua add <i>handle</i> View <i>nodeid parent reftype brname ?attrs?</i></tt>

    Adds an new node of node class <tt>View</tt> in the server object <tt><i>handle</i></tt> and returns the node identifer. For the other parameters, refer to <tt>opcua add DataType</tt>.

<tt>opcua attrs ?list|default? <i>?name?</i></tt>

    Without further arguments returns a list of attribute names the <tt>opcua read</tt> and <tt>opcua write</tt> commands support, e.g. <tt>Value</tt>, <tt>NodeClass</tt>, etc. With the <tt>list</tt> keyword a list of the data types  used as attributes for creation of nodes with the <tt>opcua add</tt> command is returned. With the <tt>default</tt> keyword combined with the <tt><i>name</i></tt> of the data type a dictionary describing the default attributes of this type is returned, e.g <tt>opcua attrs default <i>DataTypeAttributes<i></tt> yields a default dictionary for creation of a <tt>DataType</tt> node.

<tt>opcua browse <i>handle nodeid ?dir refid mask ...?</i></tt>

    Performs a browse operation on the client or server object <tt><i>handle</i></tt> starting at the node <tt><i>nodeid</i></tt>. The browse direction can be specified with the <tt><i>dir</i></tt> parameter as <tt>Forward</tt>, <tt>Inverse</tt>, or <tt>Both</tt>. <tt>Forward</tt> is the default direction. The optional <tt><i>mask</i></tt> and following parameters select specific node classes <tt>Object</tt>, <tt>Variable</tt>, <tt>Method</tt>, <tt>ObjectType</tt>, <tt>VariableType</tt>, <tt>ReferenceType</tt>, <tt>DataType</tt>, and <tt>View</tt>. The result of the browse operation is a list where each item is made up of node identifier, browse name (qualified name), display name (locale and text), node class, reference node identifier, and type node identifier.

<tt>opcua call <i>handle nodeid methodid ?type value ...?</i></tt>

    Calls the method with node identifier <tt><i>methodid</i></tt> on the object with node identifier <tt><i>nodeid</i></tt> on the client or server object <tt>handle</i></tt> with parameters described by pairs of <tt><i>type</i></tt> (data type, e.g. <tt>Int32</tt> or <tt>String</tt>) and value (the parameter's value). The method's result  is  returned. The method is carried out on the server, i.e. when directly used with a server handle there's no network traffic since the method is run locally.

<tt>opcua children <i>handle nodeid</i></tt>

    Returns the child node identifiers of the given node identifier <tt><i>nodeid</i></tt> on the client or server object <tt><i>handle</i></tt>.

<tt>opcua connect <i>handle url ?user password?</i></tt>

    Connects the client object <tt><i>handle</i></tt> to the URL <tt><i>url</i></tt> using the optional credentials <tt><i>user</i></tt> and <tt><i>password</i></tt>.

<tt>opcua datasources <i>handle</i></tt>

    Returns information on data sources (variable nodes with callbacks) for the server object <tt><i>handle</i></tt>. For each data source two list elements  with node identifier and callback command are added to the result.

<tt>opcua datetime ?seconds|...|utc <i>?value?</i>?</tt>

    Returns either POSIX or OPC/UA timestamps as <b>Tcl_WideInt</b> values. If  called without further parameters the current OPC/UA local <tt>DateTime</tt> is returned. If called with the single keyword <tt>utc</tt> the current OPC/UA  <tt>UtcTime</tt> is returned. Otherwise, <tt><i>value</i></tt> is required and converted from POSIX to OPC/UA <tt>UtcTime</tt> for the keywords <tt>seconds</tt>, <tt>milliseconds</tt>, and <tt>microseconds</tt>, and from OPC/UA <tt>UtcTime</tt> to POSIX for the keywords <tt>unixseconds</tt>, <tt>unixmillis</tt>, and <tt>unixmicros</tt>, respectively.

<tt>opcua deftypes <i>handle nsuri defs</i></tt>

    Defines custom datatypes (currently only structures) in the server object <tt><i>handle</i></tt> and namespace URI <tt><i>nsuri</i></tt>. The namespace is created with the <tt>opcua add Namespace</tt> command and must exist before the <tt>opcua deftypes</tt> command is called. The parameter <tt><i>defs</i></tt> describes the structures to be created. The command does all necessary steps to create the required nodes in the server object's address space and to store an XML bytestring describing the (de)serialization for the structures as extension objects. That XML is later to be reparsed with the <tt>opcua gentypes</tt> command. For details refer to section <b>Defining Custom Data Structures</b> below.

<tt>opcua delete <i>handle</i> Node <i>nodeid ?withrefs?</i></tt>

    Deletes the node with identifier <tt><i>nodeid</i></tt> on the server object
<tt><i>handle</i></tt>. If <tt><i>withrefs</i></tt> is true, the references of  the node are deleted, too.

<tt>opcua delete <i>handle</i> Reference <i>srcid reftypeid targetid ?forward? ?bidir?</i></tt>

    Deletes the reference described by <tt><i>srcid</i></tt>, <tt><i>reftypeid</i></tt>, and <tt><i>targetid</i></tt> on the server object <tt><i>handle</i></tt>. The  boolean flag <tt><i>forward</i></tt> selects  forward or inverse direction of the reference to be deleted. The boolean flag <tt><i>bidir</i></tt> requests a bidirectional reference to be deleted. The default is to delete in forward direction only.

<tt>opcua destroy <i>handle</i></tt>

    Destroys the client or server object <tt><i>handle</i></tt> and releases  its resources, e.g. closes network connections, tears down the handle specific namespace, etc.

<tt>opcua disconnect <i>handle</i></tt>

    Disconnects the client object <tt><i>handle</i></tt>.

<tt>opcua endpoints <i>?url?</i></tt>

    Queries the local OPC/UA server <tt>opc.tcp://localhost:4840</tt> or the server specified by the <tt><i>url</i></tt> parameter for endpoints and returns a list of URLs describing the endpoints found.

<tt>opcua genstubs <i>handle ?strip stubsts ...?</i></tt>

    Generates stubs for methods in the handle specific address space derived  from the client or server object <tt><i>handle</i></tt>. The address space is traversed and browse paths and node class paths are accumulated. The resulting browse paths optionally get the prefix <tt><i>strip</i></tt> stripped off from the beginning and optionally filtered using the glob patterns following the <tt><i>strip</i></tt> parameter. If <tt><i>substs</i></tt> is not empty it
specifies pairwise regexps and substitutions which are applied on the browse
paths for the final procedure names. For all nodes matching the node class path pattern <tt>Object</tt>/<tt>Method</tt> the optional <tt>InputArguments</tt> and  <tt>OutputArguments</tt> child nodes are retrieved and stub procedures are written using the browse path and argument information.

<tt>opcua gentypes <i>handle</i></tt>

    Generates custom data type mappings using information obtained from analyzing the address space derived from the client or server object <tt><i>handle</i></tt>. This feature is highly experimental and requires the  tDOM package for parsing XML. It can create encoders/decoders for simple structure data types defined in the address space which perform a mapping from/to Tcl dictionaries. For further information, see the <b>server_types.tcl</b> and <b>client_types.tcl</b> scripts in the examples directory. If this command is used, it should be invoked prior to creating method stubs, since methods may require custom data types in their arguments.

<tt>opcua info <i>?handle?</i></tt>

    Returns the object type of <tt><i>handle</i></tt>, either <tt>client</tt> or <tt>server</tt>. If <tt><i>handle</i></tt> is omitted, a list of all known client and server object handles is returned.

<tt>opcua methods <i>handle</i></tt>

    Returns information on methods for the server object <tt><i>handle</i></tt>. For each method three list elements with node identifier, result type name, and callback command are added to the result.

<tt>opcua monitor <i>handle</i> configure <i>subid monid ?cmd mode interval?</i></tt>

    Configures the monitor <tt><i>monid</i></tt> in subscription <tt><i>subid</i></tt> on the client object <tt><i>handle</i></tt> with the provided parameters, see <tt>opcua  monitor new</tt> for further information.

<tt>opcua monitor <i>handle</i> destroy <i>subid monid</i></tt>

    Destroys the monitor <tt><i>monid</i></tt> in subscription <tt><i>subid</i></tt> on the client object <tt><i>handle</i></tt> and releases all its resources.

<tt>opcua monitor <i>handle</i> info <i>subid ?monid?</i></tt>

    Returns information on monitor <tt><i>monid</i></tt> in subscription <tt><i>subid</i></tt> on the client object <tt><i>handle</i></tt>. The result is a list of monitor type (<tt>data</tt> or <tt>event</tt>), the node identifier, the callback command, the attribute, and the interval. If <tt><i>monid</i></tt> is omitted, a list of  all monitor identifers registered in the subscription is returned.

<tt>opcua monitor <i>handle</i> new <i>subid type cmd nodeid ?attr mode interval?</i></tt>

    Creates a monitored item of <tt><i>type</i></tt> (<tt>data</tt> or <tt>event</tt>) for the node identifier <tt><i>nodeid</i></tt> in the subscription <tt><i>subid</i></tt> on the client object <tt><i>handle</i></tt>. The optional parameter <tt><i>attr</i></tt> selects the attribute of the node  to be monitored (<tt>Value</tt> is the default). The monitor mode <tt><i>mode</i></tt> must be one of <tt>Disabled</tt>, <tt>Sampling</tt>, and <tt>Reporting</tt>. The monitoring interval <tt><i>interval</i></tt> must be given as number of milliseconds, if omitted its value is derived from the subscription. The callback command parameter <tt><i>cmd</i></tt> is discussed in section <b>Monitor Callbacks</b> below. The command returns a numeric identifier of the newly created monitor.

<tt>opcua namespace <i>handle ?uri?</i></tt>

    Returns the namespace index for the namespace <tt><i>uri</i></tt> of the client or server object <tt><i>handle</i></tt> (or throws an error e.g. when the namespace doesn't exist). If <tt><i>uri</i></tt> is omitted, a list of all known namespace indices and corresponding URIs is returned.

<tt>opcua new ?client? <i>?name?</i></tt><br>
<tt>opcua new server <i>port name</i></tt>

    Creates a new client or server object and returns its handle. The <tt><i>port</i></tt> parameter must be present for server objects and specifies the server's TCP port. The optional <tt><i>name</i></tt> is the object name (the  handle). If no arguments are given to <tt>opcua new</tt> a client object with an automatic name is created. During that process the Tcl namespace <tt>::opcua::<i>name</i></tt> is created which later is used to hold method stub procedures  and other information. That namespace is tied to the life time of the client or server object. The initial access control list of a server object is empty.

<tt>opcua parent <i>handle nodeid</i></tt>

    Returns the parent node identifier of the given node identifier <tt><i>nodeid</i></tt> on the client or server object <tt><i>handle</i></tt>.

<tt>opcua ptree <i>handle ?nodeid?</i></tt>

    Returns information similar to <tt>opcua tree</tt> using the client or server object <tt><i>handle</i></tt>. The address space is traversed starting at the node identifier <tt><i>nodeid</i></tt> (the root node if omitted). The result list is made up of browse path name, node identifier, node class path, reference node identier, and type node identifier. The browse path name is a path name like notation made up of the browse names pointing to the final node as seen from the starting node. Browse names are written as qualified names, i.e. including the numeric namespace index if not in root namespace. Similarly, the node class path is a path name like notation made up of the node classes of all nodes along the path. The <tt>opcua ptree</tt> command is used internally by the <tt>opcua genstubs</tt> command in order to filter out objects and methods when creating stub Tcl commands to invoke methods on objects.

<tt>opcua read <i>handle nodeid ?attr?</i></tt>

    Performs a read operation on the client or server object <tt><i>handle</i></tt> and returns the value of attribute <tt><i>attr</i></tt> of the node identifier <tt><i>nodeid</i></tt>. If <tt><i>attr</i></tt> is omitted, it defaults to the <tt>Value</tt> attribute.

<tt>opcua reftype <i>?name?</i></tt>

    Returns the node identifier for the reference type <tt><i>name</i></tt>. When <tt><i>name</i></tt> is omitted, a list of all reference type names is returned.

<tt>opcua root</tt>

    Returns the node identifier of the root node.

<tt>opcua run <i>handle ?ms?</i></tt>

    Runs asynchronous operations (subscriptions, monitored items) on the client object <tt><i>handle</i></tt> for <tt><i>ms</i></tt> milliseconds. If <tt><i>ms</i></tt> is omitted, that duration defaults to one millisecond.

<tt>opcua sc2str <i>code</i></tt>

    Translates the numeric status code <tt><i>code</i></tt> to an error message string.

<tt>opcua servers <i>?url?</i></tt>

    Queries the local OPC/UA server <tt>opc.tcp://localhost:4840</tt> or the server specified by the <tt><i>url</i></tt> parameter for server information and returns a list made up of three elements per server with server name, server URL, and server description.

<tt>opcua start <i>handle</i></tt>

    Starts the server object <tt><i>handle</i></tt>. See section <b>Server Object And Event Loop</b> below for further information.

<tt>opcua stop <i>handle</i></tt>

    Stops the server object <tt><i>handle</i></tt>.

<tt>opcua subscription <i>handle</i> configure <i>id ?interval lifetime keepalive max prio?</i></tt>

    Configures the subscription <tt><i>id</i></tt> on the client object <tt><i>handle</i></tt>. See <tt>opcua subscription new</tt> for the optional arguments.

<tt>opcua subscription <i>handle</i> destroy <i>id</i></tt>

    Destroys the subscription <tt><i>id</i></tt> on the client object <tt><i>handle</i></tt>.

<tt>opcua subscription <i>handle</i> info <i>?id?</i></tt>

    Returns information about subscription <tt><i>id</i></tt> on the client object <tt><i>handle</i></tt> as a list of enable flag, interval, lifetime,  keepalive, and  maximum counters, and the priority value. If <tt><i>id</i></tt> is omitted, a list of all subscription identifiers of the client object is returned.

<tt>opcua subscription <i>handle</i> new <i>?flag interval lifetime keepalive max prio?</i></tt>

    Creates a new subscription (a container for monitored items, see <tt>opcua monitor</tt>) on the client object <tt><i>handle</i></tt> and returns a numeric identifier of it. The following optional parameters control properties of the subscription: <tt><i>flag</i></tt> is the initial enable state (on by default),  <tt><i>interval</i></tt>, <tt><i>lifetime</i></tt>, <tt><i>keepalive</i></tt>, and <tt><i>max</i></tt> the timing and queuing parameters, and <tt><i>prio</i></tt> the subscription's priority.

<tt>opcua subscription <i>handle</i> off <i>id</i></tt>

    Disables the subscription <tt><i>id</i></tt> on the client object <tt><i>handle</i></tt>.

<tt>opcua subscription <i>handle</i> on <i>id</i></tt>

    Enables the subscription <tt><i>id</i></tt> on the client object <tt><i>handle</i></tt>.

<tt>opcua translate <i>handle nodeid reftype target ...</i></tt>

    Performs a translate operation on the client or server object <tt><i>handle</i></tt>. The operation starts at node identifier <tt><i>nodeid</i></tt> and traverses the object tree along the references <tt><i>reftype</i></tt> and browse name <tt><i>target</i></tt>. A list made up of the node identifier, namespace URI, and server index of the final target is returned as the result. References can be preceeded with an exclamation mark in order to reverse their direction. A reference may be abbreviated as slash for <tt>HierarchicalReferences</tt> or as dot for <tt>Aggregates</tt>.

<tt>opcua tree <i>handle ?nodeid?</i></tt>

    Returns information similar to <tt>opcua browse</tt> using the client or server object <tt><i>handle</i></tt>. The address space is traversed starting at the node identifier <tt><i>nodeid</i></tt> (the root node if omitted). The result list is made up of tree level (0-based), node identifier, browse name (qualified name), display name  (locale  and  text), node class, reference node identifier, and type node identifier.

<tt>opcua type <i>handle nodeid ?attr?</i></tt>

    Performs a read operation on the client or server object <tt><i>handle</i></tt> like <tt>opcua read</tt> but instead of the attribute's value returns the type name of attribute <tt><i>attr</i></tt> of the node  identifier <tt><i>nodeid</i></tt>.  If <tt><i>attr</i></tt> is omitted, it defaults to the <tt>Value</tt> attribute.

<tt>opcua types basic|empty|list|nodeid <i>?name?</i></tt>

    Returns a list of OPC/UA type names for the <tt>basic</tt> and <tt>list</tt> subcommands. Basic types are primitives (e.g. integer numbers) for which a mapping to Tcl objects is provided. The <tt><i>empty</i></tt> subcommand requires <tt><i>name</i></tt> to be a known OPC/UA type name and produces and returns an empty value of this type, e.g. 0.0 for a floating point type. The <tt><i>nodeid</i><tt> subcommand returns the node identifier for the type name.

<tt>opcua write <i>handle nodeid ?attr? type value</i></tt>

    Performs a write operation on the client or server object <tt><i>handle</i></tt> writing <tt><i>value</i></tt> with type <tt><i>type</i></tt> into the attribute <tt><i>attr</i></tt> of the node identifier <tt><i>nodeid</i></tt>. If <tt><i>attr</i></tt> is omitted, it defaults to <tt>Value</tt>.

<h3>OPC/UA Ensemble</h3>

The current implementation uses an ensemble and namespace <tt>opcua</tt>, i.e.
the command <tt>opcua info</tt> can be alternatively written as <tt>opcua::info</tt>. Some more complex subcommands of the <tt>opcua</tt> namespace are implemented in Tcl, namely the <tt>opcua tree</tt> and <tt>opcua genstubs</tt> procedures.

<h3>Node Identifiers</h3>

Numeric node identifiers can be written as <tt>ns=N;i=I</tt> where <tt>N</tt> is  the numeric namespace, and <tt>I</tt> the numeric identifier. Likewise, string node identifiers are written as <tt>ns=N;s=S</tt> with <tt>S</tt> being the string identifier. GUID node identifiers are written as <tt>ns=N;g=G</tt> where the GUID is <tt>G</tt> with the usual format as sequence of hexadecimal numbers  and dashes. The namespace part can be left out when namespace zero is addressed. Currently, byte string node identifiers are not supported. If the format cannot  be determined (e.g. since the equal sign is missing) the fallback chosen is string node identifier in namespace zero. String named namespaces are not supported.

<h3>Qualified Names</h3>

Qualified names are used for example in the <tt>opcua browse</tt> and <tt>opcua translate</tt> operations as so called browse names. These are made up of an optional numeric namespace prefix (a number followed by a colon) and a name, e.g. <tt><i>2:MyObject</i></tt>. The namespace prefix is left out if the name refers to namespace zero.

<h3>Supported Data Types</h3>

Currently, most of the data types of namespace zero are supported and can be mapped to/from Tcl, i.e. integral and floating point numbers, strings, GUIDs, and interal extension objects (similar to structures). For the latter, dictionaries are used in both directions, i.e. for encoding, a dictionary is searched for the respective member names, for decoding, a dictionary is created from the internal representation using the member names of the data type, see <tt>opcua attrs default</tt> for example. Support for custom  data  types  is  highly experimental and underdocumented (see <tt>opcua gentypes</tt>).

<h3>Monitor Callbacks</h3>

Monitor callbacks are invoked when a monitored item (data or event) is received. The callback parameter given in <tt>opcua monitor new</tt> must have proper  list format and gets a single value (data) or a list of values (event) appended prior to invocation.

<h3>Data Source Callbacks</h3>

Data source callbacks are invoked when a <tt><i>DataValue</i></tt> is read or  written to. The callback parameter given in the node creation (<tt>opcua add Variable</tt>) must have proper list format and gets the following  parameters appended prior to invocation: the node identifier of the <tt><i>DataValue</i></tt>, the operation (either <tt>read</tt> or <tt>write</tt>), and the value attribute for write operations. For read operations the callback must return a two element list of the data type (e.g. <tt>String</tt> or <tt>Int32</tt>) and the value itself. If the callback returns the <b>TCL_BREAK</b> return code, the value is assumed to be an array and splitted into list elements which then are converted to OPC/UA data in an OPC/UA array.

<h3>Method Callbacks</h3>

Method callbacks are invoked when a <tt><i>Method</i></tt> node is called. The callback parameter given in the node creation (<tt>opcua add Method</tt>) must  have proper list format and gets the following parameters appended prior to invocation: the object node identifier, the method node identifier, and zero or more parameters as decribed in the input argument list at creation time of the <tt><i>Method</i></tt> node. The callback must return a single value which is converted to the respective OPC/UA data value according to the output argument information at creation of the <tt><i>Method</i></tt> node. Multiple output  arguments are not supported. The same rule regarding the <b>TCL_BREAK</b> return code as described in section <b>Data Source Callbacks</b> is applied to support array results.

<h3>Client Object And Event Loop</h3>

A client object obtained with <tt>opcua new client</tt> requires a running event loop only when subscriptions and/or monitored items are involved. All other operations are performed synchronously (and thus blocking). In order to receive monitored information the <tt>opcua run</tt> operation must be invoked regularly e.g. in a timed <tt>after</tt> procedure.

<h3>Client Example</h3>

<verbatim>

    package require topcua

    # create client
    opcua new client C

    # connect to server
    opcua connect C opc.tcp://localhost:4840 user pass

    # get MyNamespace
    set ns [opcua namespace C MyNamespace]

    # generate stub procs to methods in server
    # these are created in the client specific ::opcua::C namespace
    opcua genstubs C /Root/Objects/${ns}:MyObject/${ns}:

    # list all procs in client specific namespace
    puts stderr [info procs ::opcua::C::*]

    # call stubs
    puts stderr [::opcua::C::Reverse esreveR]
    puts stderr [::opcua::C::WordSplit "word\n\nsplit"]

    # read a variable
    puts stderr [opcua read C "ns=${ns};ItsTclTime"]

    # monitor callback proc
    proc monitor {data} {
        puts stderr "Monitor: $data"
    }

    # make a subscription
    set sub [opcua subscription C new 1 1000.0]

    # make a monitor
    set mon [opcua monitor C new $sub data monitor "ns=${ns};ItsTclTime"]
    puts stderr "Subscription: $sub"
    puts stderr "Monitor: $mon"

    # handle monitors for a few seconds
    set count 0
    while {$count < 600} {
        update
        opcua run C 20
        incr count
    }

    # delete monitor and subscription
    opcua monitor C destroy $sub $mon
    opcua subscription C destroy $sub

    # shut down the server using a method call
    ::opcua::C::Exit

    # destroy the client
    opcua destroy C

</verbatim>

<h3>Server Object And Event Loop</h3>

A server object obtained with <tt>opcua new server</tt> requires a running event loop as long as it is in running state (started with <tt>opcua start</tt>). It re-dispatches itself using a Tcl timer callback whose interval is controlled by the protocol timers of the OPC/UA stack implementation.

<h3>Server Example</h3>

<verbatim>

    package require topcua

    # create server
    opcua new server 4840 S

    # setup access control
    opcua acl S user pass

    # implementations of methods etc.
    namespace eval ::opcua::S {
        # method callback
        proc _reverse {obj meth string} {
            return [string reverse $string]
        }
        # method callback
        proc _wordsplit {obj meth string} {
            set w [regexp -all -inline {\S+} $string]
            # return code break makes into an array result
            return -code break $w
        }
        # method callback
        proc _exit {obj meth} {
            after 1000 [namespace current]::_real_exit
            return {}
        }
        # helper proc
        proc _real_exit {} {
            catch {
                ::opcua::stop S
                ::opcua::destroy S
            }
            exit 0
        }
        # data source callback
        proc _its_tcl_time {node op {value {}}} {
            if {$op eq "read"} {
                return [list String [clock format [clock seconds]]]
            }
            return {}
        }
    }

    # create our OPC/UA namespace
    set ns [opcua add S Namespace MyNamespace]

    # get Objects folder
    set OF [lindex [opcua translate S [opcua root] / Objects] 0]

    # create an object in our namespace in Objects folder
    set obj [opcua add S Object "ns=$ns;s=MyObject" $OF Organizes \
        "$ns:MyObject"]

    # create methods on object
    set meth [opcua add S Method "ns=$ns;s=Reverse" \
                  $obj HasComponent \
                  {String out} "$ns:Reverse" {String in} \
                  ::opcua::S::_reverse]
    set meth [opcua add S Method "ns=$ns;s=WordSplit" \
                  $obj HasComponent \
                  {String out} "$ns:WordSplit" {String in} \
                  ::opcua::S::_wordsplit]
    set meth [opcua add S Method "ns=$ns;s=Exit" \
                  $obj HasComponent \
                  {} "$ns:Exit" {} \
                  ::opcua::S::_exit]

    # create a variable in our namespace in Objects folder
    set var [opcua add S Variable "ns=$ns;s=ItsTclTime" \
                 $OF Organizes \
                 "$ns:ItsTclTime" {} {} \
                 ::opcua::S::_its_tcl_time]

    # dump methods
    puts stderr [opcua methods S]

    # generate stubs to methods in server
    # these are created in the server specific ::opcua::S namespace
    opcua genstubs S /Root/Objects/${ns}:MyObject/${ns}:

    # list all procs in server specific namespace
    puts stderr [info procs ::opcua::S::*]

    # call stubs directly on server
    puts stderr [::opcua::S::Reverse esreveR]
    puts stderr [::opcua::S::WordSplit "word\n\nsplit"]

    # read our variable
    puts stderr [opcua read S $var]

    # start server
    opcua start S

    # enter event loop
    vwait forever

</verbatim>

<h3>Defining Custom Data Structures</h3>

<verbatim>

    package require topcua

    # create server
    opcua new server 4840 S

    # create our namespace
    set NS http://www.androwish.org/TestNS/
    set nsidx [opcua add S Namespace $NS]

    # create structs
    opcua deftypes S $NS {
        struct KVPair {
            String name
            String value
        }
        struct RGB {
            UInt16 red
            UInt16 green
            UInt16 blue
        }
        struct NamedColor {
            String name
            RGB color
        }
    }

    # import type defs
    opcua gentypes S

    # make some variables using the structs from above
    set OF [lindex [opcua translate S [opcua root] / Objects] 0]
    foreach {name type} {
        X1 KVPair
        X2 RGB
        X3 NamedColor
    } {
        set att [opcua attrs default VariableAttributes]
        dict set att dataType [opcua types nodeid S $type]
        dict set att value [list $type [opcua types empty S $type]]
        opcua add S Variable "ns=${nsidx};s=$name" $OF Organizes \
            "${nsidx}:$name" {} $att
    }

    # start server
    opcua start S

    # enter event loop
    vwait forever

</verbatim>

Z e2f1afa429db13abdd276b1285c6d8ac