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