Total Complexity | 4 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import sys |
||
2 | import asyncio |
||
3 | sys.path.insert(0, "..") |
||
4 | import logging |
||
5 | |||
6 | from asyncua import Client, ua |
||
7 | |||
8 | |||
9 | class SubHandler(object): |
||
10 | |||
11 | """ |
||
12 | Subscription Handler. To receive events from server for a subscription |
||
13 | """ |
||
14 | |||
15 | def datachange_notification(self, node, val, data): |
||
16 | print("Python: New data change event", node, val) |
||
17 | |||
18 | def event_notification(self, event): |
||
19 | print("Python: New event", event) |
||
20 | |||
21 | |||
22 | async def main(): |
||
23 | url = "opc.tcp://localhost:53530/OPCUA/SimulationServer/" |
||
24 | # url = "opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/" |
||
25 | async with Client(url=url) as client: |
||
26 | print("Root children are", await client.nodes.root.get_children()) |
||
27 | |||
28 | tag1 = await client.get_node("ns=2;s=Channel1.Device1.Tag1") |
||
29 | print(f"tag1 is: {tag1} with value {await tag1.read_value()} ") |
||
30 | tag2 = await client.get_node("ns=2;s=Channel1.Device1.Tag2") |
||
31 | print(f"tag2 is: {tag2} with value {await tag2.read_value()} ") |
||
32 | |||
33 | handler = SubHandler() |
||
34 | sub = await client.create_subscription(500, handler) |
||
35 | handle1 = await sub.subscribe_data_change(tag1) |
||
36 | handle2 = await sub.subscribe_data_change(tag2) |
||
37 | |||
38 | # await sub.unsubscribe(handle1) |
||
39 | # await sub.unsubscribe(handle2) |
||
40 | # await sub.delete() |
||
41 | |||
42 | if __name__ == "__main__": |
||
43 | logging.basicConfig(level=logging.WARN) |
||
44 | asyncio.run(main()) |
||
45 |