721
Python OPC-UA Documentation Release 1.0 Olivier Roulet-Dubonnet Apr 14, 2018

Python OPC-UA Documentation - Read the Docs · PDF filePython OPC-UA Documentation, Release 1.0 if the opcua-server has problems with the default options. These methods will be called

Embed Size (px)

Citation preview

  • Python OPC-UA DocumentationRelease 1.0

    Olivier Roulet-Dubonnet

    Apr 14, 2018

  • Contents

    1 OPC-UA Server Class 3

    2 OPC-UA Client Class 9

    3 High level Functions and Node Class 13

    4 Subscription Class 19

    5 opcua package 215.1 Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.2 Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4435.3 opcua.tools module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4435.4 Module contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444

    6 Indices and tables 445

    Python Module Index 447

    i

  • ii

  • Python OPC-UA Documentation, Release 1.0

    Pure Python OPC-UA / IEC 62541 Client and Server Python 2, 3 and pypy . http://freeopcua.github.io/, https://github.com/FreeOpcUa/python-opcua

    OPC-UA implementation is quasi complete and has been tested against many different OPC-UA stacks. API offersboth a low level interface to send and receive all UA defined structures and high level classes allowing to write a serveror a client in a few lines. It is easy to mix high level objects and low level UA calls in one application.

    Most code is autogenerated from xml specification using same code as the one that is used for freeopcua C++ clientand server, thus adding missing functionnality to client and server shoud be trivial.

    Some documentation is available at http://python-opcua.readthedocs.org/en/latest/

    A simple GUI client is available: https://github.com/FreeOpcUa/opcua-client-gui

    Examples: https://github.com/FreeOpcUa/python-opcua/tree/master/examples

    Minimal client example: https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-minimal.py Min-imal server example: https://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-minimal.py

    Contents:

    Contents 1

    http://freeopcua.github.io/https://github.com/FreeOpcUa/python-opcuahttps://github.com/FreeOpcUa/python-opcuahttp://python-opcua.readthedocs.org/en/latest/https://github.com/FreeOpcUa/opcua-client-guihttps://github.com/FreeOpcUa/python-opcua/tree/master/exampleshttps://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-minimal.pyhttps://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-minimal.py

  • Python OPC-UA Documentation, Release 1.0

    2 Contents

  • CHAPTER 1

    OPC-UA Server Class

    class opcua.server.server.Server(shelffile=None, iserver=None)High level Server class

    This class creates an opcua server with default values

    Create your own namespace and then populate your server address space using use the get_root() or get_objects()to get Node objects. and get_event_object() to fire events. Then start server. See example_server.py All methodsare threadsafe

    If you need more flexibility you call directly the Ua Service methods on the iserver or iserver.isession objectmembers.

    During startup the standard address space will be constructed, which may be time-consuming when running aserver on a less powerful device (e.g. a Raspberry Pi). In order to improve startup performance, a optional pathto a cache file can be passed to the server constructor. If the parameter is defined, the address space will beloaded from the cache file or the file will be created if it does not exist yet. As a result the first startup will beeven slower due to the cache file generation but all further start ups will be significantly faster.

    Variables

    product_uri (uri)

    name (string)

    default_timeout (InternalServer) timeout in milliseconds for sessions andsecure channel

    iserver internal server object

    bserver (BinaryServer) binary protocol server

    nodes (Shortcuts) shortcuts to common nodes

    allow_remote_admin(allow)Enable or disable the builtin Admin user from network clients

    create_custom_data_type(idx, name, basetype=24, properties=None)

    create_custom_event_type(idx, name, basetype=2041, properties=None)

    3

  • Python OPC-UA Documentation, Release 1.0

    create_custom_object_type(idx, name, basetype=58, properties=None, variables=None, meth-ods=None)

    create_custom_variable_type(idx, name, basetype=62, properties=None, variables=None,methods=None)

    create_subscription(period, handler)Create a subscription. returns a Subscription object which allow to subscribe to events or data on server

    dehistorize_node_data_change(node)Stop historizing supplied nodes; see history module Args:

    node: node or list of nodes that can be historized (UA variables/properties)

    Returns:

    dehistorize_node_event(node)Stop historizing events from node (typically a UA object); see history module Args:

    node: node or list of nodes that can be historized (UA objects)

    Returns:

    delete_nodes(nodes, recursive=False)

    disable_clock(val=True)for debugging you may want to disable clock that write every second to address space

    export_xml(nodes, path)Export defined nodes to xml

    export_xml_by_ns(path, namespaces=None)Export nodes of one or more namespaces to an XML file. Namespaces used by nodes are always exportedfor consistency. Args:

    server: opc ua server to use path: name of the xml file to write namespaces: list of string uris orint indexes of the namespace to export, if not provide all ns are used except 0

    Returns:

    find_servers(uris=None)find_servers. mainly implemented for symmetry with client

    get_client_to_discovery(url=opc.tcp://localhost:4840)Create a client to discovery server and return it

    get_endpoints()

    get_event_generator(etype=None, source=2253)Returns an event object using an event type from address space. Use this object to fire events

    get_namespace_array()get all namespace defined in server

    get_namespace_index(uri)get index of a namespace using its uri

    get_node(nodeid)Get a specific node using NodeId object or a string representing a NodeId

    get_objects_node()Get Objects node of server. Returns a Node object.

    get_root_node()Get Root node of server. Returns a Node object.

    4 Chapter 1. OPC-UA Server Class

  • Python OPC-UA Documentation, Release 1.0

    get_server_node()Get Server node of server. Returns a Node object.

    historize_node_data_change(node, period=datetime.timedelta(7), count=0)Start historizing supplied nodes; see history module Args:

    node: node or list of nodes that can be historized (variables/properties) period: time delta to storethe history; older data will be deleted from the storage count: number of changes to store in thehistory

    Returns:

    historize_node_event(node, period=datetime.timedelta(7), count=0)Start historizing events from node (typically a UA object); see history module Args:

    node: node or list of nodes that can be historized (UA objects) period: time delta to store thehistory; older data will be deleted from the storage count: number of events to store in the history

    Returns:

    import_xml(path)Import nodes defined in xml

    link_method(node, callback)Link a python function to a UA method in the address space; required when a UA method has beenimported to the address space via XML; the python executable must be linked manually Args:

    node: UA method node callback: python function that the UA method will call

    Returns:

    load_certificate(path)load server certificate from file, either pem or der

    load_private_key(path)

    load_type_definitions(nodes=None)

    register_namespace(uri)Register a new namespace. Nodes should in custom namespace, not 0.

    register_to_discovery(url=opc.tcp://localhost:4840, period=60)Register to an OPC-UA Discovery server. Registering must be renewed at least every 10 minutes, so thismethod will use our asyncio thread to re-register every period seconds if period is 0 registration is notautomatically renewed

    set_application_uri(uri)Set application/server URI. This uri is supposed to be unique. If you intent to register your server to adiscovery server, it really should be unique in your system! default is : urn:freeopcua:python:server

    set_endpoint(url)

    set_server_name(name)

    start()Start to listen on network

    stop()Stop server

    subscribe_server_callback(event, handle)

    unregister_to_discovery(url=opc.tcp://localhost:4840)stop registration thread

    5

    urn:freeopcua:python:server

  • Python OPC-UA Documentation, Release 1.0

    unsubscribe_server_callback(event, handle)

    class opcua.server.internal_server.InternalServer(shelffile=None)

    add_endpoint(endpoint)

    create_session(name, user=, external=False)

    disable_history_data_change(node)Set attribute Historizing of node to False and stop storing data for history

    disable_history_event(source)Set attribute History Read of node to False and stop storing data for history

    dump_address_space(path)Dump current address space to path

    enable_history_data_change(node, period=datetime.timedelta(7), count=0)Set attribute Historizing of node to True and start storing data for history

    enable_history_event(source, period=datetime.timedelta(7), count=0)Set attribute History Read of object events to True and start storing data for history

    find_servers(params)

    get_endpoints(params=None, sockname=None)

    get_new_channel_id()

    load_address_space(path)Load address space from path

    load_standard_address_space(shelffile=None)

    register_server(server, conf=None)

    register_server2(params)

    setup_nodes()Set up some nodes as defined by spec

    start()

    stop()

    subscribe_server_callback(event, handl