|
1 from __future__ import print_function |
|
2 from __future__ import absolute_import |
|
3 |
|
4 import csv |
|
5 |
|
6 from opcua import Client |
|
7 from opcua import ua |
|
8 |
|
9 import wx |
|
10 import wx.lib.gizmos as gizmos # Formerly wx.gizmos in Classic |
|
11 import wx.dataview as dv |
|
12 |
|
13 |
|
14 UA_IEC_types = dict( |
|
15 # pyopcua | IEC61131| C type | sz | open62541 enum | open62541 |
|
16 Boolean = ("BOOL" , "uint8_t" , "X", "UA_TYPES_BOOLEAN", "UA_Boolean"), |
|
17 SByte = ("SINT" , "int8_t" , "B", "UA_TYPES_SBYTE" , "UA_SByte" ), |
|
18 Byte = ("USINT", "uint8_t" , "B", "UA_TYPES_BYTE" , "UA_Byte" ), |
|
19 Int16 = ("INT" , "int16_t" , "W", "UA_TYPES_INT16" , "UA_Int16" ), |
|
20 UInt16 = ("UINT" , "uint16_t", "W", "UA_TYPES_UINT16" , "UA_UInt16" ), |
|
21 Int32 = ("DINT" , "uint32_t", "D", "UA_TYPES_INT32" , "UA_Int32" ), |
|
22 UInt32 = ("UDINT", "int32_t" , "D", "UA_TYPES_UINT32" , "UA_UInt32" ), |
|
23 Int64 = ("LINT" , "int64_t" , "L", "UA_TYPES_INT64" , "UA_Int64" ), |
|
24 UInt64 = ("ULINT", "uint64_t", "L", "UA_TYPES_UINT64" , "UA_UInt64" ), |
|
25 Float = ("REAL" , "float" , "D", "UA_TYPES_FLOAT" , "UA_Float" ), |
|
26 Double = ("LREAL", "double" , "L", "UA_TYPES_DOUBLE" , "UA_Double" ), |
|
27 ) |
|
28 |
|
29 UA_NODE_ID_types = { |
|
30 "int" : "UA_NODEID_NUMERIC", |
|
31 "string": "UA_NODEID_STRING", |
|
32 "UUIS" : "UA_NODEID_UUID", |
|
33 } |
|
34 |
|
35 lstcolnames = [ "Name", "NSIdx", "IdType", "Id", "Type", "IEC"] |
|
36 lstcolwidths = [ 100, 50, 100, 100, 100, 50] |
|
37 lstcoltypess = [ str, int, str, str, str, int] |
|
38 |
|
39 directions = ["input", "output"] |
|
40 |
|
41 class OPCUASubListModel(dv.DataViewIndexListModel): |
|
42 def __init__(self, data, log): |
|
43 dv.DataViewIndexListModel.__init__(self, len(data)) |
|
44 self.data = data |
|
45 self.log = log |
|
46 |
|
47 def GetColumnType(self, col): |
|
48 return "string" |
|
49 |
|
50 def GetValueByRow(self, row, col): |
|
51 return str(self.data[row][col]) |
|
52 |
|
53 # This method is called when the user edits a data item in the view. |
|
54 def SetValueByRow(self, value, row, col): |
|
55 expectedtype = lstcoltypess[col] |
|
56 |
|
57 try: |
|
58 v = expectedtype(value) |
|
59 except ValueError: |
|
60 self.log("String {} is invalid for type {}\n".format(value,expectedtype.__name__)) |
|
61 return False |
|
62 |
|
63 if col == lstcolnames.index("IdType") and v not in UA_NODE_ID_types: |
|
64 self.log("{} is invalid for IdType\n".format(value)) |
|
65 return False |
|
66 |
|
67 self.data[row][col] = v |
|
68 return True |
|
69 |
|
70 # Report how many columns this model provides data for. |
|
71 def GetColumnCount(self): |
|
72 return len(lstcolnames) |
|
73 |
|
74 # Report the number of rows in the model |
|
75 def GetCount(self): |
|
76 #self.log.write('GetCount') |
|
77 return len(self.data) |
|
78 |
|
79 # Called to check if non-standard attributes should be used in the |
|
80 # cell at (row, col) |
|
81 def GetAttrByRow(self, row, col, attr): |
|
82 if col == 5: |
|
83 attr.SetColour('blue') |
|
84 attr.SetBold(True) |
|
85 return True |
|
86 return False |
|
87 |
|
88 |
|
89 def DeleteRows(self, rows): |
|
90 # make a copy since we'll be sorting(mutating) the list |
|
91 # use reverse order so the indexes don't change as we remove items |
|
92 rows = sorted(rows, reverse=True) |
|
93 |
|
94 for row in rows: |
|
95 # remove it from our data structure |
|
96 del self.data[row] |
|
97 # notify the view(s) using this model that it has been removed |
|
98 self.RowDeleted(row) |
|
99 |
|
100 |
|
101 def AddRow(self, value): |
|
102 v = dict(zip(lstcolnames, value)) |
|
103 |
|
104 if type(v["IEC"]) != int: |
|
105 if len(self.data) == 0: |
|
106 v["IEC"] = 0 |
|
107 else: |
|
108 iecnums = set(zip(*self.data)[lstcolnames.index("IEC")]) |
|
109 greatest = max(iecnums) |
|
110 holes = set(range(greatest)) - iecnums |
|
111 v["IEC"] = min(holes) if holes else greatest+1 |
|
112 |
|
113 if v["IdType"] not in UA_NODE_ID_types: |
|
114 self.log("Unknown IdType\n".format(value)) |
|
115 return |
|
116 |
|
117 try: |
|
118 for t,n in zip(lstcoltypess, lstcolnames): |
|
119 v[n] = t(v[n]) |
|
120 except ValueError: |
|
121 self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"])) |
|
122 return |
|
123 |
|
124 if len(self.data)>0 and v["Id"] in zip(*self.data)[lstcolnames.index("Id")]: |
|
125 self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"])) |
|
126 return |
|
127 |
|
128 self.data.append([v[n] for n in lstcolnames]) |
|
129 |
|
130 # notify views |
|
131 self.RowAppended() |
|
132 |
|
133 def ResetData(self): |
|
134 self.Reset(len(self.data)) |
|
135 |
|
136 OPCUAClientDndMagicWord = "text/beremiz-opcuaclient" |
|
137 |
|
138 class NodeDropTarget(wx.DropTarget): |
|
139 |
|
140 def __init__(self, parent): |
|
141 data = wx.CustomDataObject(OPCUAClientDndMagicWord) |
|
142 wx.DropTarget.__init__(self, data) |
|
143 self.ParentWindow = parent |
|
144 |
|
145 def OnDrop(self, x, y): |
|
146 self.ParentWindow.OnNodeDnD() |
|
147 return True |
|
148 |
|
149 class OPCUASubListPanel(wx.Panel): |
|
150 def __init__(self, parent, log, model, direction): |
|
151 self.log = log |
|
152 wx.Panel.__init__(self, parent, -1) |
|
153 |
|
154 self.dvc = dv.DataViewCtrl(self, |
|
155 style=wx.BORDER_THEME |
|
156 | dv.DV_ROW_LINES |
|
157 | dv.DV_HORIZ_RULES |
|
158 | dv.DV_VERT_RULES |
|
159 | dv.DV_MULTIPLE |
|
160 ) |
|
161 |
|
162 self.model = model |
|
163 |
|
164 self.dvc.AssociateModel(self.model) |
|
165 |
|
166 for idx,(colname,width) in enumerate(zip(lstcolnames,lstcolwidths)): |
|
167 self.dvc.AppendTextColumn(colname, idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE) |
|
168 |
|
169 DropTarget = NodeDropTarget(self) |
|
170 self.dvc.SetDropTarget(DropTarget) |
|
171 |
|
172 self.Sizer = wx.BoxSizer(wx.VERTICAL) |
|
173 |
|
174 self.direction = direction |
|
175 titlestr = direction + " variables" |
|
176 |
|
177 title = wx.StaticText(self, label = titlestr) |
|
178 |
|
179 delbt = wx.Button(self, label="Delete Row(s)") |
|
180 self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, delbt) |
|
181 |
|
182 topsizer = wx.BoxSizer(wx.HORIZONTAL) |
|
183 topsizer.Add(title, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 5) |
|
184 topsizer.Add(delbt, 0, wx.LEFT|wx.RIGHT, 5) |
|
185 self.Sizer.Add(topsizer, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5) |
|
186 self.Sizer.Add(self.dvc, 1, wx.EXPAND) |
|
187 |
|
188 |
|
189 |
|
190 def OnDeleteRows(self, evt): |
|
191 items = self.dvc.GetSelections() |
|
192 rows = [self.model.GetRow(item) for item in items] |
|
193 self.model.DeleteRows(rows) |
|
194 |
|
195 |
|
196 def OnNodeDnD(self): |
|
197 # Have to find OPC-UA client extension panel from here |
|
198 # in order to avoid keeping reference (otherwise __del__ isn't called) |
|
199 # splitter. panel. splitter |
|
200 ClientPanel = self.GetParent().GetParent().GetParent() |
|
201 nodes = ClientPanel.GetSelectedNodes() |
|
202 for node in nodes: |
|
203 cname = node.get_node_class().name |
|
204 dname = node.get_display_name().to_string() |
|
205 if cname != "Variable": |
|
206 self.log("Node {} ignored (not a variable)".format(dname)) |
|
207 continue |
|
208 |
|
209 tname = node.get_data_type_as_variant_type().name |
|
210 if tname not in UA_IEC_types: |
|
211 self.log("Node {} ignored (unsupported type)".format(dname)) |
|
212 continue |
|
213 |
|
214 access = node.get_access_level() |
|
215 if {"input":ua.AccessLevel.CurrentRead, |
|
216 "output":ua.AccessLevel.CurrentWrite}[self.direction] not in access: |
|
217 self.log("Node {} ignored because of insuficient access rights".format(dname)) |
|
218 continue |
|
219 |
|
220 nsid = node.nodeid.NamespaceIndex |
|
221 nid = node.nodeid.Identifier |
|
222 nid_type = type(nid).__name__ |
|
223 iecid = nid |
|
224 |
|
225 value = [dname, |
|
226 nsid, |
|
227 nid_type, |
|
228 nid, |
|
229 tname, |
|
230 iecid] |
|
231 self.model.AddRow(value) |
|
232 |
|
233 |
|
234 |
|
235 il = None |
|
236 fldridx = None |
|
237 fldropenidx = None |
|
238 fileidx = None |
|
239 smileidx = None |
|
240 isz = (16,16) |
|
241 |
|
242 treecolnames = [ "Name", "Class", "NSIdx", "Id"] |
|
243 treecolwidths = [ 250, 100, 50, 200] |
|
244 |
|
245 |
|
246 def prepare_image_list(): |
|
247 global il, fldridx, fldropenidx, fileidx, smileidx |
|
248 |
|
249 if il is not None: |
|
250 return |
|
251 |
|
252 il = wx.ImageList(isz[0], isz[1]) |
|
253 fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz)) |
|
254 fldropenidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz)) |
|
255 fileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz)) |
|
256 smileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_OTHER, isz)) |
|
257 |
|
258 |
|
259 class OPCUAClientPanel(wx.SplitterWindow): |
|
260 def __init__(self, parent, modeldata, log, uri_getter): |
|
261 self.log = log |
|
262 wx.SplitterWindow.__init__(self, parent, -1) |
|
263 |
|
264 self.ordered_nodes = [] |
|
265 |
|
266 self.inout_panel = wx.Panel(self) |
|
267 self.inout_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
|
268 self.inout_sizer.AddGrowableCol(0) |
|
269 self.inout_sizer.AddGrowableRow(1) |
|
270 |
|
271 self.client = None |
|
272 self.uri_getter = uri_getter |
|
273 |
|
274 self.connect_button = wx.ToggleButton(self.inout_panel, -1, "Browse Server") |
|
275 |
|
276 self.selected_splitter = wx.SplitterWindow(self.inout_panel, style=wx.SUNKEN_BORDER | wx.SP_3D) |
|
277 |
|
278 self.selected_datas = modeldata |
|
279 self.selected_models = { direction:OPCUASubListModel(self.selected_datas[direction], log) for direction in directions } |
|
280 self.selected_lists = { direction:OPCUASubListPanel( |
|
281 self.selected_splitter, log, |
|
282 self.selected_models[direction], direction) |
|
283 for direction in directions } |
|
284 |
|
285 self.selected_splitter.SplitHorizontally(*[self.selected_lists[direction] for direction in directions]+[300]) |
|
286 |
|
287 self.inout_sizer.Add(self.connect_button, flag=wx.GROW) |
|
288 self.inout_sizer.Add(self.selected_splitter, flag=wx.GROW) |
|
289 self.inout_sizer.Layout() |
|
290 self.inout_panel.SetAutoLayout(True) |
|
291 self.inout_panel.SetSizer(self.inout_sizer) |
|
292 |
|
293 self.Initialize(self.inout_panel) |
|
294 |
|
295 self.Bind(wx.EVT_TOGGLEBUTTON, self.OnConnectButton, self.connect_button) |
|
296 |
|
297 def OnClose(self): |
|
298 if self.client is not None: |
|
299 self.client.disconnect() |
|
300 self.client = None |
|
301 |
|
302 def __del__(self): |
|
303 self.OnClose() |
|
304 |
|
305 def OnConnectButton(self, event): |
|
306 if self.connect_button.GetValue(): |
|
307 |
|
308 self.tree_panel = wx.Panel(self) |
|
309 self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
|
310 self.tree_sizer.AddGrowableCol(0) |
|
311 self.tree_sizer.AddGrowableRow(0) |
|
312 |
|
313 self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle= |
|
314 gizmos.TR_DEFAULT_STYLE |
|
315 | gizmos.TR_MULTIPLE |
|
316 | gizmos.TR_FULL_ROW_HIGHLIGHT |
|
317 ) |
|
318 |
|
319 prepare_image_list() |
|
320 self.tree.SetImageList(il) |
|
321 |
|
322 for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)): |
|
323 self.tree.AddColumn(colname) |
|
324 self.tree.SetColumnWidth(idx, width) |
|
325 |
|
326 self.tree.SetMainColumn(0) |
|
327 |
|
328 self.client = Client(self.uri_getter()) |
|
329 self.client.connect() |
|
330 self.client.load_type_definitions() # load definition of server specific structures/extension objects |
|
331 rootnode = self.client.get_root_node() |
|
332 |
|
333 rootitem = self.AddNodeItem(self.tree.AddRoot, rootnode) |
|
334 |
|
335 # Populate first level so that root can be expanded |
|
336 self.CreateSubItems(rootitem) |
|
337 |
|
338 self.tree.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpand) |
|
339 |
|
340 self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeNodeSelection) |
|
341 self.tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag) |
|
342 |
|
343 self.tree.Expand(rootitem) |
|
344 |
|
345 hint = wx.StaticText(self, label = "Drag'n'drop desired variables from tree to Input or Output list") |
|
346 |
|
347 self.tree_sizer.Add(self.tree, flag=wx.GROW) |
|
348 self.tree_sizer.Add(hint, flag=wx.GROW) |
|
349 self.tree_sizer.Layout() |
|
350 self.tree_panel.SetAutoLayout(True) |
|
351 self.tree_panel.SetSizer(self.tree_sizer) |
|
352 |
|
353 self.SplitVertically(self.tree_panel, self.inout_panel, 500) |
|
354 else: |
|
355 self.client.disconnect() |
|
356 self.client = None |
|
357 self.Unsplit(self.tree_panel) |
|
358 self.tree_panel.Destroy() |
|
359 |
|
360 |
|
361 def CreateSubItems(self, item): |
|
362 node, browsed = self.tree.GetPyData(item) |
|
363 if not browsed: |
|
364 for subnode in node.get_children(): |
|
365 self.AddNodeItem(lambda n: self.tree.AppendItem(item, n), subnode) |
|
366 self.tree.SetPyData(item,(node, True)) |
|
367 |
|
368 def AddNodeItem(self, item_creation_func, node): |
|
369 nsid = node.nodeid.NamespaceIndex |
|
370 nid = node.nodeid.Identifier |
|
371 dname = node.get_display_name().to_string() |
|
372 cname = node.get_node_class().name |
|
373 |
|
374 item = item_creation_func(dname) |
|
375 |
|
376 if cname == "Variable": |
|
377 access = node.get_access_level() |
|
378 normalidx = fileidx |
|
379 r = ua.AccessLevel.CurrentRead in access |
|
380 w = ua.AccessLevel.CurrentWrite in access |
|
381 if r and w: |
|
382 ext = "RW" |
|
383 elif r: |
|
384 ext = "RO" |
|
385 elif w: |
|
386 ext = "WO" # not sure this one exist |
|
387 else: |
|
388 ext = "no access" # not sure this one exist |
|
389 cname = "Var "+node.get_data_type_as_variant_type().name+" (" + ext + ")" |
|
390 else: |
|
391 normalidx = fldridx |
|
392 |
|
393 self.tree.SetPyData(item,(node, False)) |
|
394 self.tree.SetItemText(item, cname, 1) |
|
395 self.tree.SetItemText(item, str(nsid), 2) |
|
396 self.tree.SetItemText(item, type(nid).__name__+": "+str(nid), 3) |
|
397 self.tree.SetItemImage(item, normalidx, which = wx.TreeItemIcon_Normal) |
|
398 self.tree.SetItemImage(item, fldropenidx, which = wx.TreeItemIcon_Expanded) |
|
399 |
|
400 return item |
|
401 |
|
402 def OnExpand(self, evt): |
|
403 for item in evt.GetItem().GetChildren(): |
|
404 self.CreateSubItems(item) |
|
405 |
|
406 # def OnActivate(self, evt): |
|
407 # item = evt.GetItem() |
|
408 # node, browsed = self.tree.GetPyData(item) |
|
409 |
|
410 def OnTreeNodeSelection(self, event): |
|
411 items = self.tree.GetSelections() |
|
412 items_pydata = [self.tree.GetPyData(item) for item in items] |
|
413 |
|
414 nodes = [node for node, _unused in items_pydata] |
|
415 |
|
416 # append new nodes to ordered list |
|
417 for node in nodes: |
|
418 if node not in self.ordered_nodes: |
|
419 self.ordered_nodes.append(node) |
|
420 |
|
421 # filter out vanished items |
|
422 self.ordered_nodes = [ |
|
423 node |
|
424 for node in self.ordered_nodes |
|
425 if node in nodes] |
|
426 |
|
427 def GetSelectedNodes(self): |
|
428 return self.ordered_nodes |
|
429 |
|
430 def OnTreeBeginDrag(self, event): |
|
431 """ |
|
432 Called when a drag is started in tree |
|
433 @param event: wx.TreeEvent |
|
434 """ |
|
435 if self.ordered_nodes: |
|
436 # Just send a recognizable mime-type, drop destination |
|
437 # will get python data from parent |
|
438 data = wx.CustomDataObject(OPCUAClientDndMagicWord) |
|
439 dragSource = wx.DropSource(self) |
|
440 dragSource.SetData(data) |
|
441 dragSource.DoDragDrop() |
|
442 |
|
443 def Reset(self): |
|
444 for direction in directions: |
|
445 self.selected_models[direction].ResetData() |
|
446 |
|
447 |
|
448 class OPCUAClientModel(dict): |
|
449 def __init__(self): |
|
450 for direction in directions: |
|
451 self[direction] = list() |
|
452 |
|
453 def LoadCSV(self,path): |
|
454 with open(path, 'rb') as csvfile: |
|
455 reader = csv.reader(csvfile, delimiter=',', quotechar='"') |
|
456 buf = {direction:[] for direction, _model in self.iteritems()} |
|
457 for row in reader: |
|
458 direction = row[0] |
|
459 buf[direction].append(row[1:]) |
|
460 for direction, model in self.iteritems(): |
|
461 self[direction][:] = buf[direction] |
|
462 |
|
463 def SaveCSV(self,path): |
|
464 with open(path, 'wb') as csvfile: |
|
465 for direction, data in self.iteritems(): |
|
466 writer = csv.writer(csvfile, delimiter=',', |
|
467 quotechar='"', quoting=csv.QUOTE_MINIMAL) |
|
468 for row in data: |
|
469 writer.writerow([direction] + row) |
|
470 |
|
471 def GenerateC(self, path, locstr, server_uri): |
|
472 template = """/* code generated by beremiz OPC-UA extension */ |
|
473 |
|
474 #include <open62541/client_config_default.h> |
|
475 #include <open62541/client_highlevel.h> |
|
476 #include <open62541/plugin/log_stdout.h> |
|
477 |
|
478 UA_Client *client; |
|
479 |
|
480 #define DECL_VAR(ua_type, C_type, c_loc_name) \\ |
|
481 UA_Variant *c_loc_name##_variant; \\ |
|
482 C_type c_loc_name##_buf = 0; \\ |
|
483 C_type *c_loc_name = &c_loc_name##_buf; |
|
484 |
|
485 %(decl)s |
|
486 |
|
487 #define FREE_VARIANT(ua_type, c_loc_name) \\ |
|
488 UA_Variant_delete(c_loc_name##_variant); |
|
489 |
|
490 void __cleanup_%(locstr)s(void) |
|
491 { |
|
492 UA_Client_disconnect(client); |
|
493 UA_Client_delete(client); |
|
494 %(cleanup)s |
|
495 } |
|
496 |
|
497 |
|
498 #define ALLOC_VARIANT(ua_type, c_loc_name) \\ |
|
499 c_loc_name##_variant = UA_Variant_new(); |
|
500 |
|
501 int __init_%(locstr)s(int argc,char **argv) |
|
502 { |
|
503 UA_StatusCode retval; |
|
504 client = UA_Client_new(); |
|
505 UA_ClientConfig_setDefault(UA_Client_getConfig(client)); |
|
506 %(init)s |
|
507 |
|
508 /* Connect to server */ |
|
509 retval = UA_Client_connect(client, "%(uri)s"); |
|
510 if(retval != UA_STATUSCODE_GOOD) { |
|
511 UA_Client_delete(client); |
|
512 return EXIT_FAILURE; |
|
513 } |
|
514 } |
|
515 |
|
516 #define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ |
|
517 retval = UA_Client_readValueAttribute( \\ |
|
518 client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant); \\ |
|
519 if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(c_loc_name##_variant) && \\ |
|
520 c_loc_name##_variant->type == &UA_TYPES[ua_type_enum]) { \\ |
|
521 c_loc_name##_buf = *(ua_type*)c_loc_name##_variant->data; \\ |
|
522 } |
|
523 |
|
524 void __retrieve_%(locstr)s(void) |
|
525 { |
|
526 UA_StatusCode retval; |
|
527 %(retrieve)s |
|
528 } |
|
529 |
|
530 #define WRITE_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ |
|
531 UA_Variant_setScalarCopy(c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]); \\ |
|
532 UA_Client_writeValueAttribute(client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant); |
|
533 |
|
534 void __publish_%(locstr)s(void) |
|
535 { |
|
536 %(publish)s |
|
537 } |
|
538 |
|
539 """ |
|
540 |
|
541 formatdict = dict( |
|
542 locstr = locstr, |
|
543 uri = server_uri, |
|
544 decl = "", |
|
545 cleanup = "", |
|
546 init = "", |
|
547 retrieve = "", |
|
548 publish = "" |
|
549 ) |
|
550 for direction, data in self.iteritems(): |
|
551 iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction] |
|
552 for row in data: |
|
553 name, ua_nsidx, ua_nodeid_type, ua_node_id, ua_type, iec_number = row |
|
554 iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type] |
|
555 c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number) |
|
556 ua_nodeid_type = UA_NODE_ID_types[ua_nodeid_type] |
|
557 |
|
558 formatdict["decl"] += """ |
|
559 DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals()) |
|
560 formatdict["cleanup"] += """ |
|
561 FREE_VARIANT({ua_type}, {c_loc_name})""".format(**locals()) |
|
562 formatdict["init"] +=""" |
|
563 ALLOC_VARIANT({ua_type}, {c_loc_name})""".format(**locals()) |
|
564 formatdict["retrieve"] += """ |
|
565 READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) |
|
566 formatdict["publish"] += """ |
|
567 WRITE_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) |
|
568 |
|
569 Ccode = template%formatdict |
|
570 |
|
571 return Ccode |
|
572 |
|
573 if __name__ == "__main__": |
|
574 |
|
575 import wx.lib.mixins.inspection as wit |
|
576 import sys,os |
|
577 |
|
578 app = wit.InspectableApp() |
|
579 |
|
580 frame = wx.Frame(None, -1, "OPCUA Client Test App", size=(800,600)) |
|
581 |
|
582 uri = sys.argv[1] if len(sys.argv)>1 else "opc.tcp://localhost:4840" |
|
583 |
|
584 test_panel = wx.Panel(frame) |
|
585 test_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
|
586 test_sizer.AddGrowableCol(0) |
|
587 test_sizer.AddGrowableRow(0) |
|
588 |
|
589 modeldata = OPCUAClientModel() |
|
590 |
|
591 opcuatestpanel = OPCUAClientPanel(test_panel, modeldata, print, lambda:uri) |
|
592 |
|
593 def OnGenerate(evt): |
|
594 dlg = wx.FileDialog( |
|
595 frame, message="Generate file as ...", defaultDir=os.getcwd(), |
|
596 defaultFile="", |
|
597 wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT |
|
598 ) |
|
599 |
|
600 if dlg.ShowModal() == wx.ID_OK: |
|
601 path = dlg.GetPath() |
|
602 Ccode = """ |
|
603 /* |
|
604 In case open62541 was built just aside beremiz, you can build this test with: |
|
605 gcc %s -o %s \\ |
|
606 -I ../../open62541/plugins/include/ \\ |
|
607 -I ../../open62541/build/src_generated/ \\ |
|
608 -I ../../open62541/include/ \\ |
|
609 -I ../../open62541/arch/ ../../open62541/build/bin/libopen62541.a |
|
610 */ |
|
611 |
|
612 """%(path, path[:-2]) + modeldata.GenerateC(path, "test", uri) + """ |
|
613 |
|
614 int main(int argc, char *argv[]) { |
|
615 |
|
616 __init_test(arc,argv); |
|
617 |
|
618 __retrieve_test(); |
|
619 |
|
620 __publish_test(); |
|
621 |
|
622 __cleanup_test(); |
|
623 |
|
624 return EXIT_SUCCESS; |
|
625 } |
|
626 """ |
|
627 |
|
628 with open(path, 'wb') as Cfile: |
|
629 Cfile.write(Ccode) |
|
630 |
|
631 |
|
632 dlg.Destroy() |
|
633 |
|
634 def OnLoad(evt): |
|
635 dlg = wx.FileDialog( |
|
636 frame, message="Choose a file", |
|
637 defaultDir=os.getcwd(), |
|
638 defaultFile="", |
|
639 wildcard="CSV (*.csv)|*.csv", |
|
640 style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST ) |
|
641 |
|
642 if dlg.ShowModal() == wx.ID_OK: |
|
643 path = dlg.GetPath() |
|
644 modeldata.LoadCSV(path) |
|
645 opcuatestpanel.Reset() |
|
646 |
|
647 dlg.Destroy() |
|
648 |
|
649 def OnSave(evt): |
|
650 dlg = wx.FileDialog( |
|
651 frame, message="Save file as ...", defaultDir=os.getcwd(), |
|
652 defaultFile="", |
|
653 wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT |
|
654 ) |
|
655 |
|
656 if dlg.ShowModal() == wx.ID_OK: |
|
657 path = dlg.GetPath() |
|
658 modeldata.SaveCSV(path) |
|
659 |
|
660 dlg.Destroy() |
|
661 |
|
662 test_sizer.Add(opcuatestpanel, flag=wx.GROW) |
|
663 |
|
664 testbt_sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
665 |
|
666 loadbt = wx.Button(test_panel, label="Load") |
|
667 test_panel.Bind(wx.EVT_BUTTON, OnLoad, loadbt) |
|
668 |
|
669 savebt = wx.Button(test_panel, label="Save") |
|
670 test_panel.Bind(wx.EVT_BUTTON, OnSave, savebt) |
|
671 |
|
672 genbt = wx.Button(test_panel, label="Generate") |
|
673 test_panel.Bind(wx.EVT_BUTTON, OnGenerate, genbt) |
|
674 |
|
675 testbt_sizer.Add(loadbt, 0, wx.LEFT|wx.RIGHT, 5) |
|
676 testbt_sizer.Add(savebt, 0, wx.LEFT|wx.RIGHT, 5) |
|
677 testbt_sizer.Add(genbt, 0, wx.LEFT|wx.RIGHT, 5) |
|
678 |
|
679 test_sizer.Add(testbt_sizer, flag=wx.GROW) |
|
680 test_sizer.Layout() |
|
681 test_panel.SetAutoLayout(True) |
|
682 test_panel.SetSizer(test_sizer) |
|
683 |
|
684 def OnClose(evt): |
|
685 opcuatestpanel.OnClose() |
|
686 evt.Skip() |
|
687 |
|
688 frame.Bind(wx.EVT_CLOSE, OnClose) |
|
689 |
|
690 frame.Show() |
|
691 |
|
692 app.MainLoop() |
|
693 |