--- a/features.py Mon Oct 18 12:34:30 2021 +0200
+++ b/features.py Mon Oct 18 12:40:53 2021 +0200
@@ -15,6 +15,7 @@
('SVGHMI', 'svghmi.SVGHMILibrary', False)]
+ ('opcua', _('OPC-UA client'), _('Map OPC-UA server as located variables'), 'opc_ua.OPCUAClient'), ('canfestival', _('CANopen support'), _('Map located variables over CANopen'), 'canfestival.canfestival.RootClass'),
('bacnet', _('Bacnet support'), _('Map located variables over Bacnet'), 'bacnet.bacnet.RootClass'),
('etherlab', _('EtherCAT master'), _('Map located variables over EtherCAT'), 'etherlab.etherlab.RootClass'),
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/opc_ua/opcua_client_maker.py Mon Oct 18 12:40:53 2021 +0200
@@ -0,0 +1,693 @@
+from __future__ import print_function +from __future__ import absolute_import +from opcua import Client +import wx.lib.gizmos as gizmos # Formerly wx.gizmos in Classic +import wx.dataview as dv +# pyopcua | IEC61131| C type | sz | open62541 enum | open62541 + Boolean = ("BOOL" , "uint8_t" , "X", "UA_TYPES_BOOLEAN", "UA_Boolean"), + SByte = ("SINT" , "int8_t" , "B", "UA_TYPES_SBYTE" , "UA_SByte" ), + Byte = ("USINT", "uint8_t" , "B", "UA_TYPES_BYTE" , "UA_Byte" ), + Int16 = ("INT" , "int16_t" , "W", "UA_TYPES_INT16" , "UA_Int16" ), + UInt16 = ("UINT" , "uint16_t", "W", "UA_TYPES_UINT16" , "UA_UInt16" ), + Int32 = ("DINT" , "uint32_t", "D", "UA_TYPES_INT32" , "UA_Int32" ), + UInt32 = ("UDINT", "int32_t" , "D", "UA_TYPES_UINT32" , "UA_UInt32" ), + Int64 = ("LINT" , "int64_t" , "L", "UA_TYPES_INT64" , "UA_Int64" ), + UInt64 = ("ULINT", "uint64_t", "L", "UA_TYPES_UINT64" , "UA_UInt64" ), + Float = ("REAL" , "float" , "D", "UA_TYPES_FLOAT" , "UA_Float" ), + Double = ("LREAL", "double" , "L", "UA_TYPES_DOUBLE" , "UA_Double" ), + "int" : "UA_NODEID_NUMERIC", + "string": "UA_NODEID_STRING", + "UUIS" : "UA_NODEID_UUID", +lstcolnames = [ "Name", "NSIdx", "IdType", "Id", "Type", "IEC"] +lstcolwidths = [ 100, 50, 100, 100, 100, 50] +lstcoltypess = [ str, int, str, str, str, int] +directions = ["input", "output"] +class OPCUASubListModel(dv.DataViewIndexListModel): + def __init__(self, data, log): + dv.DataViewIndexListModel.__init__(self, len(data)) + def GetColumnType(self, col): + def GetValueByRow(self, row, col): + return str(self.data[row][col]) + # This method is called when the user edits a data item in the view. + def SetValueByRow(self, value, row, col): + expectedtype = lstcoltypess[col] + v = expectedtype(value) + self.log("String {} is invalid for type {}\n".format(value,expectedtype.__name__)) + if col == lstcolnames.index("IdType") and v not in UA_NODE_ID_types: + self.log("{} is invalid for IdType\n".format(value)) + self.data[row][col] = v + # Report how many columns this model provides data for. + def GetColumnCount(self): + return len(lstcolnames) + # Report the number of rows in the model + #self.log.write('GetCount') + # Called to check if non-standard attributes should be used in the + def GetAttrByRow(self, row, col, attr): + def DeleteRows(self, rows): + # make a copy since we'll be sorting(mutating) the list + # use reverse order so the indexes don't change as we remove items + rows = sorted(rows, reverse=True) + # remove it from our data structure + # notify the view(s) using this model that it has been removed + def AddRow(self, value): + v = dict(zip(lstcolnames, value)) + if type(v["IEC"]) != int: + if len(self.data) == 0: + iecnums = set(zip(*self.data)[lstcolnames.index("IEC")]) + greatest = max(iecnums) + holes = set(range(greatest)) - iecnums + v["IEC"] = min(holes) if holes else greatest+1 + if v["IdType"] not in UA_NODE_ID_types: + self.log("Unknown IdType\n".format(value)) + for t,n in zip(lstcoltypess, lstcolnames): + self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"])) + if len(self.data)>0 and v["Id"] in zip(*self.data)[lstcolnames.index("Id")]: + self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"])) + self.data.append([v[n] for n in lstcolnames]) + self.Reset(len(self.data)) +OPCUAClientDndMagicWord = "text/beremiz-opcuaclient" +class NodeDropTarget(wx.DropTarget): + def __init__(self, parent): + data = wx.CustomDataObject(OPCUAClientDndMagicWord) + wx.DropTarget.__init__(self, data) + self.ParentWindow = parent + def OnDrop(self, x, y): + self.ParentWindow.OnNodeDnD() +class OPCUASubListPanel(wx.Panel): + def __init__(self, parent, log, model, direction): + wx.Panel.__init__(self, parent, -1) + self.dvc = dv.DataViewCtrl(self, + self.dvc.AssociateModel(self.model) + for idx,(colname,width) in enumerate(zip(lstcolnames,lstcolwidths)): + self.dvc.AppendTextColumn(colname, idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE) + DropTarget = NodeDropTarget(self) + self.dvc.SetDropTarget(DropTarget) + self.Sizer = wx.BoxSizer(wx.VERTICAL) + self.direction = direction + titlestr = direction + " variables" + title = wx.StaticText(self, label = titlestr) + delbt = wx.Button(self, label="Delete Row(s)") + self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, delbt) + topsizer = wx.BoxSizer(wx.HORIZONTAL) + topsizer.Add(title, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 5) + topsizer.Add(delbt, 0, wx.LEFT|wx.RIGHT, 5) + self.Sizer.Add(topsizer, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5) + self.Sizer.Add(self.dvc, 1, wx.EXPAND) + def OnDeleteRows(self, evt): + items = self.dvc.GetSelections() + rows = [self.model.GetRow(item) for item in items] + self.model.DeleteRows(rows) + # Have to find OPC-UA client extension panel from here + # in order to avoid keeping reference (otherwise __del__ isn't called) + # splitter. panel. splitter + ClientPanel = self.GetParent().GetParent().GetParent() + nodes = ClientPanel.GetSelectedNodes() + cname = node.get_node_class().name + dname = node.get_display_name().to_string() + if cname != "Variable": + self.log("Node {} ignored (not a variable)".format(dname)) + tname = node.get_data_type_as_variant_type().name + if tname not in UA_IEC_types: + self.log("Node {} ignored (unsupported type)".format(dname)) + access = node.get_access_level() + if {"input":ua.AccessLevel.CurrentRead, + "output":ua.AccessLevel.CurrentWrite}[self.direction] not in access: + self.log("Node {} ignored because of insuficient access rights".format(dname)) + nsid = node.nodeid.NamespaceIndex + nid = node.nodeid.Identifier + nid_type = type(nid).__name__ + self.model.AddRow(value) +treecolnames = [ "Name", "Class", "NSIdx", "Id"] +treecolwidths = [ 250, 100, 50, 200] +def prepare_image_list(): + global il, fldridx, fldropenidx, fileidx, smileidx + il = wx.ImageList(isz[0], isz[1]) + fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz)) + fldropenidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz)) + fileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz)) + smileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_OTHER, isz)) +class OPCUAClientPanel(wx.SplitterWindow): + def __init__(self, parent, modeldata, log, uri_getter): + wx.SplitterWindow.__init__(self, parent, -1) + self.ordered_nodes = [] + self.inout_panel = wx.Panel(self) + self.inout_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) + self.inout_sizer.AddGrowableCol(0) + self.inout_sizer.AddGrowableRow(1) + self.uri_getter = uri_getter + self.connect_button = wx.ToggleButton(self.inout_panel, -1, "Browse Server") + self.selected_splitter = wx.SplitterWindow(self.inout_panel, style=wx.SUNKEN_BORDER | wx.SP_3D) + self.selected_datas = modeldata + self.selected_models = { direction:OPCUASubListModel(self.selected_datas[direction], log) for direction in directions } + self.selected_lists = { direction:OPCUASubListPanel( + self.selected_splitter, log, + self.selected_models[direction], direction) + for direction in directions } + self.selected_splitter.SplitHorizontally(*[self.selected_lists[direction] for direction in directions]+[300]) + self.inout_sizer.Add(self.connect_button, flag=wx.GROW) + self.inout_sizer.Add(self.selected_splitter, flag=wx.GROW) + self.inout_sizer.Layout() + self.inout_panel.SetAutoLayout(True) + self.inout_panel.SetSizer(self.inout_sizer) + self.Initialize(self.inout_panel) + self.Bind(wx.EVT_TOGGLEBUTTON, self.OnConnectButton, self.connect_button) + if self.client is not None: + self.client.disconnect() + def OnConnectButton(self, event): + if self.connect_button.GetValue(): + self.tree_panel = wx.Panel(self) + self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) + self.tree_sizer.AddGrowableCol(0) + self.tree_sizer.AddGrowableRow(0) + self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle= + gizmos.TR_DEFAULT_STYLE + | gizmos.TR_FULL_ROW_HIGHLIGHT + self.tree.SetImageList(il) + for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)): + self.tree.AddColumn(colname) + self.tree.SetColumnWidth(idx, width) + self.tree.SetMainColumn(0) + self.client = Client(self.uri_getter()) + self.client.load_type_definitions() # load definition of server specific structures/extension objects + rootnode = self.client.get_root_node() + rootitem = self.AddNodeItem(self.tree.AddRoot, rootnode) + # Populate first level so that root can be expanded + self.CreateSubItems(rootitem) + self.tree.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpand) + self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeNodeSelection) + self.tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag) + self.tree.Expand(rootitem) + hint = wx.StaticText(self, label = "Drag'n'drop desired variables from tree to Input or Output list") + self.tree_sizer.Add(self.tree, flag=wx.GROW) + self.tree_sizer.Add(hint, flag=wx.GROW) + self.tree_sizer.Layout() + self.tree_panel.SetAutoLayout(True) + self.tree_panel.SetSizer(self.tree_sizer) + self.SplitVertically(self.tree_panel, self.inout_panel, 500) + self.client.disconnect() + self.Unsplit(self.tree_panel) + self.tree_panel.Destroy() + def CreateSubItems(self, item): + node, browsed = self.tree.GetPyData(item) + for subnode in node.get_children(): + self.AddNodeItem(lambda n: self.tree.AppendItem(item, n), subnode) + self.tree.SetPyData(item,(node, True)) + def AddNodeItem(self, item_creation_func, node): + nsid = node.nodeid.NamespaceIndex + nid = node.nodeid.Identifier + dname = node.get_display_name().to_string() + cname = node.get_node_class().name + item = item_creation_func(dname) + if cname == "Variable": + access = node.get_access_level() + r = ua.AccessLevel.CurrentRead in access + w = ua.AccessLevel.CurrentWrite in access + ext = "WO" # not sure this one exist + ext = "no access" # not sure this one exist + cname = "Var "+node.get_data_type_as_variant_type().name+" (" + ext + ")" + self.tree.SetPyData(item,(node, False)) + self.tree.SetItemText(item, cname, 1) + self.tree.SetItemText(item, str(nsid), 2) + self.tree.SetItemText(item, type(nid).__name__+": "+str(nid), 3) + self.tree.SetItemImage(item, normalidx, which = wx.TreeItemIcon_Normal) + self.tree.SetItemImage(item, fldropenidx, which = wx.TreeItemIcon_Expanded) + def OnExpand(self, evt): + for item in evt.GetItem().GetChildren(): + self.CreateSubItems(item) + # def OnActivate(self, evt): + # node, browsed = self.tree.GetPyData(item) + def OnTreeNodeSelection(self, event): + items = self.tree.GetSelections() + items_pydata = [self.tree.GetPyData(item) for item in items] + nodes = [node for node, _unused in items_pydata] + # append new nodes to ordered list + if node not in self.ordered_nodes: + self.ordered_nodes.append(node) + # filter out vanished items + for node in self.ordered_nodes + def GetSelectedNodes(self): + return self.ordered_nodes + def OnTreeBeginDrag(self, event): + Called when a drag is started in tree + @param event: wx.TreeEvent + # Just send a recognizable mime-type, drop destination + # will get python data from parent + data = wx.CustomDataObject(OPCUAClientDndMagicWord) + dragSource = wx.DropSource(self) + dragSource.SetData(data) + dragSource.DoDragDrop() + for direction in directions: + self.selected_models[direction].ResetData() +class OPCUAClientModel(dict): + for direction in directions: + self[direction] = list() + def LoadCSV(self,path): + with open(path, 'rb') as csvfile: + reader = csv.reader(csvfile, delimiter=',', quotechar='"') + buf = {direction:[] for direction, _model in self.iteritems()} + buf[direction].append(row[1:]) + for direction, model in self.iteritems(): + self[direction][:] = buf[direction] + def SaveCSV(self,path): + with open(path, 'wb') as csvfile: + for direction, data in self.iteritems(): + writer = csv.writer(csvfile, delimiter=',', + quotechar='"', quoting=csv.QUOTE_MINIMAL) + writer.writerow([direction] + row) + def GenerateC(self, path, locstr, server_uri): + template = """/* code generated by beremiz OPC-UA extension */ +#include <open62541/client_config_default.h> +#include <open62541/client_highlevel.h> +#include <open62541/plugin/log_stdout.h> +#define DECL_VAR(ua_type, C_type, c_loc_name) \\ +UA_Variant *c_loc_name##_variant; \\ +C_type c_loc_name##_buf = 0; \\ +C_type *c_loc_name = &c_loc_name##_buf; +#define FREE_VARIANT(ua_type, c_loc_name) \\ + UA_Variant_delete(c_loc_name##_variant); +void __cleanup_%(locstr)s(void) + UA_Client_disconnect(client); + UA_Client_delete(client); +#define ALLOC_VARIANT(ua_type, c_loc_name) \\ + c_loc_name##_variant = UA_Variant_new(); +int __init_%(locstr)s(int argc,char **argv) + client = UA_Client_new(); + UA_ClientConfig_setDefault(UA_Client_getConfig(client)); + /* Connect to server */ + retval = UA_Client_connect(client, "%(uri)s"); + if(retval != UA_STATUSCODE_GOOD) { + UA_Client_delete(client); +#define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ + retval = UA_Client_readValueAttribute( \\ + client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant); \\ + if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(c_loc_name##_variant) && \\ + c_loc_name##_variant->type == &UA_TYPES[ua_type_enum]) { \\ + c_loc_name##_buf = *(ua_type*)c_loc_name##_variant->data; \\ +void __retrieve_%(locstr)s(void) +#define WRITE_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ + UA_Variant_setScalarCopy(c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]); \\ + UA_Client_writeValueAttribute(client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant); +void __publish_%(locstr)s(void) + for direction, data in self.iteritems(): + iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction] + name, ua_nsidx, ua_nodeid_type, ua_node_id, ua_type, iec_number = row + iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type] + c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number) + ua_nodeid_type = UA_NODE_ID_types[ua_nodeid_type] + formatdict["decl"] += """ +DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals()) + formatdict["cleanup"] += """ + FREE_VARIANT({ua_type}, {c_loc_name})""".format(**locals()) + formatdict["init"] +=""" + ALLOC_VARIANT({ua_type}, {c_loc_name})""".format(**locals()) + formatdict["retrieve"] += """ + READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) + formatdict["publish"] += """ + WRITE_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) + Ccode = template%formatdict +if __name__ == "__main__": + import wx.lib.mixins.inspection as wit + app = wit.InspectableApp() + frame = wx.Frame(None, -1, "OPCUA Client Test App", size=(800,600)) + uri = sys.argv[1] if len(sys.argv)>1 else "opc.tcp://localhost:4840" + test_panel = wx.Panel(frame) + test_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) + test_sizer.AddGrowableCol(0) + test_sizer.AddGrowableRow(0) + modeldata = OPCUAClientModel() + opcuatestpanel = OPCUAClientPanel(test_panel, modeldata, print, lambda:uri) + frame, message="Generate file as ...", defaultDir=os.getcwd(), + wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT + if dlg.ShowModal() == wx.ID_OK: +In case open62541 was built just aside beremiz, you can build this test with: + -I ../../open62541/plugins/include/ \\ + -I ../../open62541/build/src_generated/ \\ + -I ../../open62541/include/ \\ + -I ../../open62541/arch/ ../../open62541/build/bin/libopen62541.a +"""%(path, path[:-2]) + modeldata.GenerateC(path, "test", uri) + """ +int main(int argc, char *argv[]) { + with open(path, 'wb') as Cfile: + frame, message="Choose a file", + defaultDir=os.getcwd(), + wildcard="CSV (*.csv)|*.csv", + style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST ) + if dlg.ShowModal() == wx.ID_OK: + modeldata.LoadCSV(path) + frame, message="Save file as ...", defaultDir=os.getcwd(), + wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT + if dlg.ShowModal() == wx.ID_OK: + modeldata.SaveCSV(path) + test_sizer.Add(opcuatestpanel, flag=wx.GROW) + testbt_sizer = wx.BoxSizer(wx.HORIZONTAL) + loadbt = wx.Button(test_panel, label="Load") + test_panel.Bind(wx.EVT_BUTTON, OnLoad, loadbt) + savebt = wx.Button(test_panel, label="Save") + test_panel.Bind(wx.EVT_BUTTON, OnSave, savebt) + genbt = wx.Button(test_panel, label="Generate") + test_panel.Bind(wx.EVT_BUTTON, OnGenerate, genbt) + testbt_sizer.Add(loadbt, 0, wx.LEFT|wx.RIGHT, 5) + testbt_sizer.Add(savebt, 0, wx.LEFT|wx.RIGHT, 5) + testbt_sizer.Add(genbt, 0, wx.LEFT|wx.RIGHT, 5) + test_sizer.Add(testbt_sizer, flag=wx.GROW) + test_panel.SetAutoLayout(True) + test_panel.SetSizer(test_sizer) + opcuatestpanel.OnClose() + frame.Bind(wx.EVT_CLOSE, OnClose)