1
|
|
|
|
2
|
|
|
from opcua import ua |
3
|
|
|
from opcua import Client |
4
|
|
|
from opcua import Node |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class UaClient(object): |
8
|
|
|
""" |
9
|
|
|
OPC-Ua client specialized for the need of GUI client |
10
|
|
|
return exactly whant GUI needs, no customization possible |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
def __init__(self): |
14
|
|
|
self.client = None |
15
|
|
|
self._connected = False |
16
|
|
|
self._datachange_sub = None |
17
|
|
|
self._event_sub = None |
18
|
|
|
self._subs_dc = {} |
19
|
|
|
self._subs_ev = {} |
20
|
|
|
|
21
|
|
|
def get_node(self, nodeid): |
22
|
|
|
return self.client.get_node(nodeid) |
23
|
|
|
|
24
|
|
|
def connect(self, uri): |
25
|
|
|
self.disconnect() |
26
|
|
|
print("Connecting to ", uri) |
27
|
|
|
self.client = Client(uri) |
28
|
|
|
self.client.connect() |
29
|
|
|
self._connected = True |
30
|
|
|
|
31
|
|
|
def disconnect(self): |
32
|
|
|
if self._connected: |
33
|
|
|
print("Disconnecting from server") |
34
|
|
|
self._subs_dc = {} |
35
|
|
|
self._subs_ev = {} |
36
|
|
|
self._connected = False |
37
|
|
|
self._subscription = None |
38
|
|
|
self.client.disconnect() |
39
|
|
|
self.client = None |
40
|
|
|
|
41
|
|
|
def subscribe_datachange(self, node, handler): |
42
|
|
|
if not self._datachange_sub: |
43
|
|
|
self._datachange_sub = self.client.create_subscription(500, handler) |
44
|
|
|
handle = self._datachange_sub.subscribe_data_change(node) |
45
|
|
|
self._subs_dc[node.nodeid] = handle |
46
|
|
|
return handle |
47
|
|
|
|
48
|
|
|
def unsubscribe_datachange(self, node): |
49
|
|
|
self._datachange_sub.unsubscribe(self._subs_dc[node.nodeid]) |
50
|
|
|
|
51
|
|
|
def subscribe_events(self, node, handler): |
52
|
|
|
if not self._event_sub: |
53
|
|
|
print("subscirbing with handler: ", handler, dir(handler)) |
54
|
|
|
self._event_sub = self.client.create_subscription(500, handler) |
55
|
|
|
handle = self._event_sub.subscribe_events(node) |
56
|
|
|
self._subs_ev[node.nodeid] = handle |
57
|
|
|
return handle |
58
|
|
|
|
59
|
|
|
def unsubscribe_events(self, node): |
60
|
|
|
self._event_sub.unsubscribe(self._subs_ev[node.nodeid]) |
61
|
|
|
|
62
|
|
|
def get_root_desc(self): |
63
|
|
|
node = self.client.get_root_node() |
64
|
|
|
attrs = node.get_attributes([ua.AttributeIds.DisplayName, ua.AttributeIds.BrowseName, ua.AttributeIds.NodeId, ua.AttributeIds.NodeClass]) |
65
|
|
|
desc = ua.ReferenceDescription() |
66
|
|
|
desc.DisplayName = attrs[0].Value.Value |
67
|
|
|
desc.BrowseName = attrs[1].Value.Value |
68
|
|
|
desc.NodeId = attrs[2].Value.Value |
69
|
|
|
desc.NodeClass = attrs[3].Value.Value |
70
|
|
|
desc.TypeDefinition = ua.TwoByteNodeId(ua.ObjectIds.FolderType) |
71
|
|
|
return desc |
72
|
|
|
|
73
|
|
|
def get_node_attrs(self, node): |
74
|
|
|
if not isinstance(node, Node): |
75
|
|
|
node = self.client.get_node(node) |
76
|
|
|
attrs = node.get_attributes([ua.AttributeIds.DisplayName, ua.AttributeIds.BrowseName, ua.AttributeIds.NodeId]) |
77
|
|
|
return node, [attr.Value.Value.to_string() for attr in attrs] |
78
|
|
|
|
79
|
|
|
def get_children(self, node): |
80
|
|
|
descs = node.get_children_descriptions() |
81
|
|
|
descs.sort(key=lambda x: x.BrowseName) |
82
|
|
|
return descs |
83
|
|
|
|
84
|
|
|
def get_all_attrs(self, node): |
85
|
|
|
names = [] |
86
|
|
|
vals = [] |
87
|
|
|
for name, val in ua.AttributeIds.__dict__.items(): |
88
|
|
|
if not name.startswith("_"): |
89
|
|
|
names.append(name) |
90
|
|
|
vals.append(val) |
91
|
|
|
|
92
|
|
|
attrs = node.get_attributes(vals) |
93
|
|
|
res = [] |
94
|
|
|
for idx, name in enumerate(names): |
95
|
|
|
if attrs[idx].StatusCode.is_good(): |
96
|
|
|
res.append((name, attrs[idx])) |
97
|
|
|
res.sort() |
98
|
|
|
return res |
99
|
|
|
|
100
|
|
|
def get_all_refs(self, node): |
101
|
|
|
return node.get_children_descriptions(refs=ua.ObjectIds.References) |
102
|
|
|
|