Completed
Pull Request — master (#184)
by Olivier
03:02
created

client-example.run()   A

Complexity

Conditions 3

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nop 0
dl 0
loc 45
rs 9.304
c 0
b 0
f 0
1
import asyncio
2
import logging
3
4
from asyncua import Client
5
6
logging.basicConfig(level=logging.INFO)
7
_logger = logging.getLogger('asyncua')
8
9
10
class SubHandler(object):
11
    """
12
    Subscription Handler. To receive events from server for a subscription
13
    data_change and event methods are called directly from receiving thread.
14
    Do not do expensive, slow or network operation there. Create another
15
    thread if you need to do such a thing
16
    """
17
    def datachange_notification(self, node, val, data):
18
        print("New data change event", node, val)
19
20
    def event_notification(self, event):
21
        print("New event", event)
22
23
24
async def main():
25
    url = "opc.tcp://localhost:4840/freeopcua/server/"
26
    async with Client(url=url) as client:
27
        _logger.info("Root node is: %r", client.nodes.root)
28
        _logger.info("Objects node is: %r", client.nodes.objects)
29
30
        # Node objects have methods to read and write node attributes as well as browse or populate address space
31
        _logger.info("Children of root are: %r", await client.nodes.root.get_children())
32
33
        uri = "http://examples.freeopcua.github.io"
34
        idx = await client.get_namespace_index(uri)
35
        _logger.info("index of our namespace is %s", idx)
36
        # get a specific node knowing its node id
37
        #var = client.get_node(ua.NodeId(1002, 2))
38
        #var = client.get_node("ns=3;i=2002")
39
        #print(var)
40
        #await var.write_data_value() # get value of node as a DataValue object
41
        #await var.read_value() # get value of node as a python builtin
42
        #await var.write_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
43
        #await var.write_value(3.9) # set node value using implicit data type
44
45
        # Now getting a variable node using its browse path
46
        myvar = await client.nodes.root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
47
        obj = await client.nodes.root.get_child(["0:Objects", "2:MyObject"])
48
        _logger.info("myvar is: %r", myvar)
49
50
        # subscribing to a variable node
51
        handler = SubHandler()
52
        sub = await client.create_subscription(500, handler)
53
        handle = await sub.subscribe_data_change(myvar)
54
        await asyncio.sleep(0.1)
55
56
        # we can also subscribe to events from server
57
        await sub.subscribe_events()
58
        # await sub.unsubscribe(handle)
59
        # await sub.delete()
60
61
        # calling a method on server
62
        res = await obj.call_method("2:multiply", 3, "klk")
63
        _logger.info("method result is: %r", res)
64
65
66
if __name__ == "__main__":
67
    asyncio.run(main())
68