opcua.common package

Submodules

opcua.common.event module

opcua.common.instanciate module

opcua.common.manage_nodes module

High level functions to create nodes

opcua.common.manage_nodes.create_data_type(parent, nodeid, bname, description=None)[source]

Create a new data type to be used in new variables, etc .. arguments are nodeid, browsename or namespace index, name

opcua.common.manage_nodes.create_folder(parent, nodeid, bname)[source]

create a child node folder arguments are nodeid, browsename or namespace index, name

opcua.common.manage_nodes.create_method(parent, *args)[source]

create a child method object This is only possible on server side!! args are nodeid, browsename, method_to_be_called, [input argument types], [output argument types] or idx, name, method_to_be_called, [input argument types], [output argument types] if argument types is specified, child nodes advertising what arguments the method uses and returns will be created a callback is a method accepting the nodeid of the parent as first argument and variants after. returns a list of variants

opcua.common.manage_nodes.create_object(parent, nodeid, bname, objecttype=None)[source]

create a child node object arguments are nodeid, browsename, [objecttype] or namespace index, name, [objecttype] if objectype is given (a NodeId) then the type node is instantiated inclusive its child nodes

opcua.common.manage_nodes.create_object_type(parent, nodeid, bname)[source]

Create a new object type to be instanciated in address space. arguments are nodeid, browsename or namespace index, name

opcua.common.manage_nodes.create_property(parent, nodeid, bname, val, varianttype=None, datatype=None)[source]

create a child node property args are nodeid, browsename, value, [variant type] or idx, name, value, [variant type]

opcua.common.manage_nodes.create_reference_type(parent, nodeid, bname, symmetric=True, inversename=None)[source]

Create a new reference type args are nodeid and browsename or idx and name

opcua.common.manage_nodes.create_variable(parent, nodeid, bname, val, varianttype=None, datatype=None)[source]

create a child node variable args are nodeid, browsename, value, [variant type], [data type] or idx, name, value, [variant type], [data type]

opcua.common.manage_nodes.create_variable_type(parent, nodeid, bname, datatype)[source]

Create a new variable type args are nodeid, browsename and datatype or idx, name and data type

opcua.common.manage_nodes.delete_nodes(server, nodes, recursive=False, delete_target_references=True)[source]

Delete specified nodes. Optionally delete recursively all nodes with a downward hierachic references to the node return the list of deleted node and the result

opcua.common.methods module

High level method related functions

opcua.common.methods.call_method(parent, methodid, *args)[source]

Call an OPC-UA method. methodid is browse name of child method or the nodeid of method as a NodeId object arguments are variants or python object convertible to variants. which may be of different types returns a list of values or a single value depending on the output of the method

opcua.common.methods.call_method_full(parent, methodid, *args)[source]

Call an OPC-UA method. methodid is browse name of child method or the nodeid of method as a NodeId object arguments are variants or python object convertible to variants. which may be of different types returns a CallMethodResult object with converted OutputArguments

opcua.common.methods.to_variant(*args)[source]
opcua.common.methods.uamethod(func)[source]

Method decorator to automatically convert arguments and output to and from variants

opcua.common.node module

High level node object, to access node attribute and browse address space

class opcua.common.node.Node(server, nodeid)[source]

Bases: object

High level node object, to access node attribute, browse and populate address space. Node objects are usefull as-is but they do not expose the entire OPC-UA protocol. Feel free to look at the code of this class and call directly UA services methods to optimize your code

add_data_type(nodeid, bname, description=None)[source]
add_folder(nodeid, bname)[source]
add_method(*args)[source]
add_object(nodeid, bname, objecttype=None)[source]
add_object_type(nodeid, bname)[source]
add_property(nodeid, bname, val, varianttype=None, datatype=None)[source]
add_reference(target, reftype, forward=True, bidirectional=True)[source]

Add reference to node

add_reference_type(nodeid, bname, symmetric=True, inversename=None)[source]
add_variable(nodeid, bname, val, varianttype=None, datatype=None)[source]
add_variable_type(nodeid, bname, datatype)[source]
call_method(methodid, *args)[source]
delete(delete_references=True, recursive=False)[source]

Delete node from address space

delete_reference(target, reftype, forward=True, bidirectional=True)[source]

Delete given node’s references from address space

get_access_level()[source]

Get the access level attribute of the node as a set of AccessLevel enum values.

get_array_dimensions()[source]

Read and return ArrayDimensions attribute of node

get_attribute(attr)[source]

Read one attribute of a node result code from server is checked and an exception is raised in case of error

get_attributes(attrs)[source]

Read several attributes of a node list of DataValue is returned

get_browse_name()[source]

Get browse name of a node. A browse name is a QualifiedName object composed of a string(name) and a namespace index.

get_child(path)[source]

get a child specified by its path from this node. A path might be: * a string representing a qualified name. * a qualified name * a list of string * a list of qualified names

get_children(refs=33, nodeclassmask=<NodeClass.Unspecified: 0>)[source]

Get all children of a node. By default hierarchical references and all node classes are returned. Other reference types may be given: References = 31 NonHierarchicalReferences = 32 HierarchicalReferences = 33 HasChild = 34 Organizes = 35 HasEventSource = 36 HasModellingRule = 37 HasEncoding = 38 HasDescription = 39 HasTypeDefinition = 40 GeneratesEvent = 41 Aggregates = 44 HasSubtype = 45 HasProperty = 46 HasComponent = 47 HasNotifier = 48 HasOrderedComponent = 49

get_children_descriptions(refs=33, nodeclassmask=<NodeClass.Unspecified: 0>, includesubtypes=True)[source]
get_data_type()[source]

get data type of node as NodeId

get_data_type_as_variant_type()[source]

get data type of node as VariantType This only works if node is a variable, otherwise type may not be convertible to VariantType

get_data_value()[source]

Get value of a node as a DataValue object. Only variables (and properties) have values. An exception will be generated for other node types. DataValue contain a variable value as a variant as well as server and source timestamps

get_description()[source]

get description attribute class of node

get_description_refs()[source]
get_display_name()[source]

get description attribute of node

get_encoding_refs()[source]
get_event_notifier()[source]

Get the event notifier attribute of the node as a set of EventNotifier enum values.

get_methods()[source]

return methods of node. properties are child nodes with a reference of type HasComponent and a NodeClass of Method

get_node_class()[source]

get node class attribute of node

get_parent()[source]

returns parent of the node. A Node may have several parents, the first found is returned. This method uses reverse references, a node might be missing such a link, thus we will not find its parent.

get_path(max_length=20, as_string=False)[source]

Attempt to find path of node from root node and return it as a list of Nodes. There might several possible paths to a node, this function will return one Some nodes may be missing references, so this method may return an empty list Since address space may have circular references, a max length is specified

get_properties()[source]

return properties of node. properties are child nodes with a reference of type HasProperty and a NodeClass of Variable

get_referenced_nodes(refs=31, direction=<BrowseDirection.Both: 2>, nodeclassmask=<NodeClass.Unspecified: 0>, includesubtypes=True)[source]

returns referenced nodes based on specific filter Paramters are the same as for get_references

get_references(refs=31, direction=<BrowseDirection.Both: 2>, nodeclassmask=<NodeClass.Unspecified: 0>, includesubtypes=True)[source]

returns references of the node based on specific filter defined with:

refs = ObjectId of the Reference direction = Browse direction for references nodeclassmask = filter nodes based on specific class includesubtypes = If true subtypes of the reference (ref) are also included

get_type_definition()[source]

returns type definition of the node.

get_user_access_level()[source]

Get the user access level attribute of the node as a set of AccessLevel enum values.

get_value()[source]

Get value of a node as a python type. Only variables ( and properties) have values. An exception will be generated for other node types. WARNING: on server side, this function returns a ref to object in ua database. Do not modify it if it is a mutable object unless you know what you are doing

get_value_rank()[source]

Read and return ArrayDimensions attribute of node

get_variables()[source]

return variables of node. properties are child nodes with a reference of type HasComponent and a NodeClass of Variable

history_read(details)[source]

Read raw history of a node, low-level function result code from server is checked and an exception is raised in case of error

history_read_events(details)[source]

Read event history of a node, low-level function result code from server is checked and an exception is raised in case of error

read_event_history(starttime=None, endtime=None, numvalues=0, evtypes=2041)[source]

Read event history of a source node result code from server is checked and an exception is raised in case of error If numvalues is > 0 and number of events in period is > numvalues then result will be truncated

read_raw_history(starttime=None, endtime=None, numvalues=0)[source]

Read raw history of a node result code from server is checked and an exception is raised in case of error If numvalues is > 0 and number of events in period is > numvalues then result will be truncated

register()[source]

Register node for faster read and write access (if supported by server) Rmw: This call modifies the nodeid of the node, the original nodeid is available as node.basenodeid

set_array_dimensions(value)[source]

Set attribute ArrayDimensions of node make sure it has the correct data type

set_attr_bit(attr, bit)[source]
set_attribute(attributeid, datavalue)[source]

Set an attribute of a node attributeid is a member of ua.AttributeIds datavalue is a ua.DataValue object

set_data_value(value, varianttype=None)

Set value of a node. Only variables(properties) have values. An exception will be generated for other node types. value argument is either: * a python built-in type, converted to opc-ua optionnaly using the variantype argument. * a ua.Variant, varianttype is then ignored * a ua.DataValue, you then have full control over data send to server WARNING: On server side, ref to object is directly saved in our UA db, if this is a mutable object and you modfy it afterward, then the object in db will be modified without any data change event generated

set_event_notifier(values)[source]

Set the event notifier attribute.

Parameters:values – an iterable of EventNotifier enum values.
set_modelling_rule(mandatory)[source]

Add a modelling rule reference to Node. When creating a new object type, its variable and child nodes will not be instanciated if they do not have modelling rule if mandatory is None, the modelling rule is removed

set_read_only()[source]

Set a node as read-only for clients. A node is always writable on server side.

set_value(value, varianttype=None)[source]

Set value of a node. Only variables(properties) have values. An exception will be generated for other node types. value argument is either: * a python built-in type, converted to opc-ua optionnaly using the variantype argument. * a ua.Variant, varianttype is then ignored * a ua.DataValue, you then have full control over data send to server WARNING: On server side, ref to object is directly saved in our UA db, if this is a mutable object and you modfy it afterward, then the object in db will be modified without any data change event generated

set_value_rank(value)[source]

Set attribute ArrayDimensions of node

set_writable(writable=True)[source]

Set node as writable by clients. A node is always writable on server side.

unregister()[source]
unset_attr_bit(attr, bit)[source]

opcua.common.subscription module

high level interface to subscriptions

class opcua.common.subscription.DataChangeNotif(subscription_data, monitored_item)[source]

Bases: object

To be send to clients for every datachange notification from server

class opcua.common.subscription.SubHandler[source]

Bases: object

Subscription Handler. To receive events from server for a subscription This class is just a sample class. Whatever class having these methods can be used

data_change(handle, node, val, attr)[source]

Deprecated, use datachange_notification

datachange_notification(node, val, data)[source]

called for every datachange notification from server

event_notification(event)[source]

called for every event notification from server

status_change_notification(status)[source]

called for every status change notification from server

class opcua.common.subscription.Subscription(server, params, handler)[source]

Bases: object

Subscription object returned by Server or Client objects. The object represent a subscription to an opc-ua server. This is a high level class, especially subscribe_data_change and subscribe_events methods. If more control is necessary look at code and/or use create_monitored_items method.

create_monitored_items(monitored_items)[source]

low level method to have full control over subscription parameters Client handle must be unique since it will be used as key for internal registration of data

deadband_monitor(var, deadband_val, deadbandtype=1, queuesize=0, attr=<AttributeIds.Value: 13>)[source]

Method to create a subscription with a Deadband Value. Default deadband value type is absolute. Return a handle which can be used to unsubscribe :param var: Variable to which you want to subscribe :param deadband_val: Absolute float value :param deadbandtype: Default value is 1 (absolute), change to 2 for percentage deadband :param queuesize: Wanted queue size, default is 1

delete()[source]

Delete subscription on server. This is automatically done by Client and Server classes on exit

is_ready()[source]
modify_monitored_item(handle, new_samp_time, new_queuesize=0, mod_filter_val=-1)[source]

Modify a monitored item. :param handle: Handle returned when originally subscribing :param new_samp_time: New wanted sample time :param new_queuesize: New wanted queuesize, default is 0 :param mod_filter_val: New deadband filter value :return: Return a Modify Monitored Item Result

publish_callback(publishresult)[source]
ready_callback(response)[source]
reconciliate(monitored_items)[source]

Reconciliate client monitored_items with its server counterpart. :param monitored_items_srv: monitored items handles from server :return: Number of mi added and deleted to the client subscription

subscribe_data_change(nodes, attr=<AttributeIds.Value: 13>, queuesize=0)[source]

Subscribe for data change events for a node or list of nodes. default attribute is Value. Return a handle which can be used to unsubscribe If more control is necessary use create_monitored_items method

subscribe_events(sourcenode=2253, evtypes=2041, evfilter=None, queuesize=0)[source]

Subscribe to events from a node. Default node is Server node. In most servers the server node is the only one you can subscribe to. if evtypes is not provided, evtype defaults to BaseEventType if evtypes is a list or tuple of custom event types, the events will be filtered to the supplied types Return a handle which can be used to unsubscribe

unsubscribe(handle)[source]

unsubscribe to datachange or events using the handle returned while subscribing if you delete subscription, you do not need to unsubscribe

class opcua.common.subscription.SubscriptionItemData[source]

Bases: object

To store useful data from a monitored item

opcua.common.uaerrors module

opcua.common.utils module

Helper function and classes that do not rely on opcua library. Helper function and classes depending on ua object are in ua_utils.py

class opcua.common.utils.Buffer(data, start_pos=0, size=-1)[source]

Bases: object

alternative to io.BytesIO making debug easier and added a few conveniance methods

copy(size=-1)[source]

return a shadow copy, optionnaly only copy ‘size’ bytes

read(size)[source]

read and pop number of bytes for buffer

skip(size)[source]

skip size bytes in buffer

exception opcua.common.utils.NotEnoughData[source]

Bases: opcua.ua.uaerrors._base.UaError

exception opcua.common.utils.ServiceError(code)[source]

Bases: opcua.ua.uaerrors._base.UaError

exception opcua.common.utils.SocketClosedException[source]

Bases: opcua.ua.uaerrors._base.UaError

class opcua.common.utils.SocketWrapper(sock)[source]

Bases: object

wrapper to make it possible to have same api for normal sockets, socket from asyncio, StringIO, etc….

read(size)[source]

Receive up to size bytes from socket

write(data)[source]
class opcua.common.utils.ThreadLoop[source]

Bases: threading.Thread

run an asyncio loop in a thread

call_later(delay, callback)[source]

threadsafe call_later from asyncio

call_soon(callback)[source]
close()[source]
create_server(proto, hostname, port)[source]
create_task(coro, cb=None)[source]

threadsafe create_task from asyncio

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

run_coro_and_wait(coro)[source]
run_until_complete(coro)[source]

threadsafe run_until_completed from asyncio

start()[source]

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

stop()[source]

stop subscription loop, thus the subscription thread

opcua.common.utils.create_nonce(size=32)[source]

opcua.common.xmlimporter module

add nodes defined in XML to address space format is the one from opc-ua specification

class opcua.common.xmlimporter.XmlImporter(server)[source]

Bases: object

add_datatype(obj)[source]
add_method(obj)[source]
add_object(obj)[source]
add_object_type(obj)[source]
add_reference_type(obj)[source]
add_variable(obj)[source]
add_variable_type(obj)[source]
import_xml(xmlpath=None, xmlstring=None)[source]

import xml and return added nodes

make_objects(node_datas)[source]
to_nodeid(nodeid)[source]

opcua.common.xmlparser module

parse xml file from opcua-spec

class opcua.common.xmlparser.ExtObj[source]

Bases: object

class opcua.common.xmlparser.NodeData[source]

Bases: object

class opcua.common.xmlparser.RefStruct[source]

Bases: object

class opcua.common.xmlparser.XMLParser(xmlpath=None, xmlstring=None)[source]

Bases: object

get_aliases()[source]

Return the used node aliases in this import file

get_node_datas()[source]
get_used_namespaces()[source]

Return the used namespace uris in this import file

opcua.common.xmlparser.ua_type_to_python(val, uatype_as_str)[source]

Converts a string value to a python value according to ua_utils.

Module contents