|
1
|
|
|
import sys |
|
2
|
|
|
sys.path.insert(0, "..") |
|
3
|
|
|
import logging |
|
4
|
|
|
|
|
5
|
|
|
from opcua import Client |
|
6
|
|
|
from opcua import uaprotocol as ua |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class SubHandler(object): |
|
10
|
|
|
|
|
11
|
|
|
""" |
|
12
|
|
|
Client to subscription. It will receive events from server |
|
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
|
|
|
if __name__ == "__main__": |
|
23
|
|
|
#from IPython import embed |
|
24
|
|
|
logging.basicConfig(level=logging.WARN) |
|
25
|
|
|
client = Client("opc.tcp://192.168.56.100:49320/OPCUA/SimulationServer/") |
|
26
|
|
|
#client = Client("opc.tcp://192.168.56.100:4841/OPCUA/SimulationServer/") |
|
27
|
|
|
#client = Client("opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/") |
|
28
|
|
|
try: |
|
29
|
|
|
client.connect() |
|
30
|
|
|
root = client.get_root_node() |
|
31
|
|
|
print("Root is", root) |
|
32
|
|
|
print("childs of root are: ", root.get_children()) |
|
33
|
|
|
print("name of root is", root.get_browse_name()) |
|
34
|
|
|
objects = client.get_objects_node() |
|
35
|
|
|
print("childs og objects are: ", objects.get_children()) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
tag1 = client.get_node("ns=2;s=Channel1.Device1.Tag1") |
|
39
|
|
|
print("tag1 is: {} with value {} ".format(tag1, tag1.get_value())) |
|
40
|
|
|
tag2 = client.get_node("ns=2;s=Channel1.Device1.Tag2") |
|
41
|
|
|
print("tag2 is: {} with value {} ".format(tag2, tag2.get_value())) |
|
42
|
|
|
|
|
43
|
|
|
handler = SubHandler() |
|
44
|
|
|
sub = client.create_subscription(500, handler) |
|
45
|
|
|
handle = sub.subscribe_data_change(tag1) |
|
46
|
|
|
handle = sub.subscribe_data_change(tag2) |
|
47
|
|
|
|
|
48
|
|
|
from IPython import embed |
|
49
|
|
|
embed() |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
sub.unsubscribe(handle) |
|
53
|
|
|
sub.delete() |
|
54
|
|
|
finally: |
|
55
|
|
|
client.disconnect() |
|
56
|
|
|
|