1
|
|
|
import sys |
2
|
|
|
sys.path.insert(0, "..") |
3
|
|
|
import logging |
4
|
|
|
import time |
5
|
|
|
|
6
|
|
|
try: |
7
|
|
|
from IPython import embed |
8
|
|
|
except ImportError: |
9
|
|
|
import code |
10
|
|
|
|
11
|
|
|
def embed(): |
12
|
|
|
vars = globals() |
13
|
|
|
vars.update(locals()) |
14
|
|
|
shell = code.InteractiveConsole(vars) |
|
|
|
|
15
|
|
|
shell.interact() |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
from asyncua.sync import Client, ThreadLoop |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class SubHandler(object): |
22
|
|
|
|
23
|
|
|
""" |
24
|
|
|
Subscription Handler. To receive events from server for a subscription |
25
|
|
|
data_change and event methods are called directly from receiving thread. |
26
|
|
|
Do not do expensive, slow or network operation there. Create another |
27
|
|
|
thread if you need to do such a thing |
28
|
|
|
""" |
29
|
|
|
|
30
|
|
|
def datachange_notification(self, node, val, data): |
31
|
|
|
print("Python: New data change event", node, val) |
32
|
|
|
|
33
|
|
|
def event_notification(self, event): |
34
|
|
|
print("Python: New event", event) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
if __name__ == "__main__": |
38
|
|
|
logging.basicConfig(level=logging.WARN) |
39
|
|
|
#logger = logging.getLogger("KeepAlive") |
40
|
|
|
#logger.setLevel(logging.DEBUG) |
41
|
|
|
|
42
|
|
|
with ThreadLoop() as tloop: |
43
|
|
|
with Client("opc.tcp://localhost:4840/freeopcua/server/", tloop=tloop) as client: |
44
|
|
|
# client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user |
45
|
|
|
client.load_type_definitions() # load definition of server specific structures/extension objects |
46
|
|
|
|
47
|
|
|
# Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects |
48
|
|
|
print("Objects node is: ", client.nodes.objects) |
49
|
|
|
|
50
|
|
|
# Node objects have methods to read and write node attributes as well as browse or populate address space |
51
|
|
|
print("Children of root are: ", client.nodes.root.get_children()) |
52
|
|
|
|
53
|
|
|
# get a specific node knowing its node id |
54
|
|
|
#var = client.get_node(ua.NodeId(1002, 2)) |
55
|
|
|
#var = client.get_node("ns=3;i=2002") |
56
|
|
|
#print(var) |
57
|
|
|
#var.read_data_value() # get value of node as a DataValue object |
58
|
|
|
#var.read_value() # get value of node as a python builtin |
59
|
|
|
#var.write_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type |
60
|
|
|
#var.write_value(3.9) # set node value using implicit data type |
61
|
|
|
|
62
|
|
|
# gettting our namespace idx |
63
|
|
|
uri = "http://examples.freeopcua.github.io" |
64
|
|
|
idx = client.get_namespace_index(uri) |
65
|
|
|
|
66
|
|
|
# Now getting a variable node using its browse path |
67
|
|
|
myvar = client.nodes.root.get_child(["0:Objects", f"{idx}:MyObject", f"{idx}:MyVariable"]) |
68
|
|
|
obj = client.nodes.root.get_child(["0:Objects", f"{idx}:MyObject"]) |
69
|
|
|
print("myvar is: ", myvar) |
70
|
|
|
|
71
|
|
|
# subscribing to a variable node |
72
|
|
|
handler = SubHandler() |
73
|
|
|
sub = client.create_subscription(500, handler) |
74
|
|
|
handle = sub.subscribe_data_change(myvar) |
75
|
|
|
time.sleep(0.1) |
76
|
|
|
|
77
|
|
|
# we can also subscribe to events from server |
78
|
|
|
sub.subscribe_events() |
79
|
|
|
# sub.unsubscribe(handle) |
80
|
|
|
# sub.delete() |
81
|
|
|
|
82
|
|
|
# calling a method on server |
83
|
|
|
res = obj.call_method(f"{idx}:multiply", 3, "klk") |
84
|
|
|
print("method result is: ", res) |
85
|
|
|
|
86
|
|
|
embed() |
87
|
|
|
|