kinsamanka@3750: kinsamanka@3750: edouard@3337: edouard@3337: import csv edouard@3820: import asyncio edouard@3820: import functools edouard@3820: from threading import Thread edouard@3820: edouard@3820: from asyncua import Client edouard@3820: from asyncua import ua edouard@3337: edouard@3337: import wx edouard@3371: import wx.lib.gizmos as gizmos # Formerly wx.gizmos in Classic edouard@3337: import wx.dataview as dv edouard@3337: edouard@3337: edouard@3337: UA_IEC_types = dict( edouard@3337: # pyopcua | IEC61131| C type | sz | open62541 enum | open62541 edouard@3337: Boolean = ("BOOL" , "uint8_t" , "X", "UA_TYPES_BOOLEAN", "UA_Boolean"), edouard@3337: SByte = ("SINT" , "int8_t" , "B", "UA_TYPES_SBYTE" , "UA_SByte" ), edouard@3337: Byte = ("USINT", "uint8_t" , "B", "UA_TYPES_BYTE" , "UA_Byte" ), edouard@3337: Int16 = ("INT" , "int16_t" , "W", "UA_TYPES_INT16" , "UA_Int16" ), edouard@3337: UInt16 = ("UINT" , "uint16_t", "W", "UA_TYPES_UINT16" , "UA_UInt16" ), edouard@3337: Int32 = ("DINT" , "uint32_t", "D", "UA_TYPES_INT32" , "UA_Int32" ), edouard@3337: UInt32 = ("UDINT", "int32_t" , "D", "UA_TYPES_UINT32" , "UA_UInt32" ), edouard@3337: Int64 = ("LINT" , "int64_t" , "L", "UA_TYPES_INT64" , "UA_Int64" ), edouard@3337: UInt64 = ("ULINT", "uint64_t", "L", "UA_TYPES_UINT64" , "UA_UInt64" ), edouard@3337: Float = ("REAL" , "float" , "D", "UA_TYPES_FLOAT" , "UA_Float" ), edouard@3337: Double = ("LREAL", "double" , "L", "UA_TYPES_DOUBLE" , "UA_Double" ), edouard@3337: ) edouard@3337: edouard@3337: UA_NODE_ID_types = { edouard@3338: "int" : ("UA_NODEID_NUMERIC", "{}" ), edouard@3338: "str" : ("UA_NODEID_STRING" , '"{}"'), edouard@3406: "UUID" : ("UA_NODEID_UUID" , '"{}"'), edouard@3337: } edouard@3337: edouard@3337: lstcolnames = [ "Name", "NSIdx", "IdType", "Id", "Type", "IEC"] edouard@3337: lstcolwidths = [ 100, 50, 100, 100, 100, 50] edouard@3337: lstcoltypess = [ str, int, str, str, str, int] edouard@3337: edouard@3337: directions = ["input", "output"] edouard@3337: edouard@3589: authParams = { edouard@3589: "x509":[ edouard@3589: ("Certificate", "certificate.der"), edouard@3589: ("PrivateKey", "private_key.pem"), edouard@3589: ("Policy", "Basic256Sha256"), edouard@3652: ("Mode", "SignAndEncrypt")], edouard@3589: "UserPassword":[ edouard@3589: ("User", None), edouard@3589: ("Password", None)]} edouard@3589: edouard@3371: class OPCUASubListModel(dv.DataViewIndexListModel): edouard@3337: def __init__(self, data, log): edouard@3371: dv.DataViewIndexListModel.__init__(self, len(data)) edouard@3337: self.data = data edouard@3337: self.log = log edouard@3337: edouard@3337: def GetColumnType(self, col): edouard@3337: return "string" edouard@3337: edouard@3337: def GetValueByRow(self, row, col): edouard@3337: return str(self.data[row][col]) edouard@3337: edouard@3337: # This method is called when the user edits a data item in the view. edouard@3337: def SetValueByRow(self, value, row, col): edouard@3337: expectedtype = lstcoltypess[col] edouard@3337: edouard@3337: try: edouard@3337: v = expectedtype(value) edouard@3337: except ValueError: edouard@3337: self.log("String {} is invalid for type {}\n".format(value,expectedtype.__name__)) edouard@3337: return False edouard@3337: edouard@3337: if col == lstcolnames.index("IdType") and v not in UA_NODE_ID_types: edouard@3337: self.log("{} is invalid for IdType\n".format(value)) edouard@3337: return False edouard@3337: edouard@3337: self.data[row][col] = v edouard@3337: return True edouard@3337: edouard@3337: # Report how many columns this model provides data for. edouard@3337: def GetColumnCount(self): edouard@3337: return len(lstcolnames) edouard@3337: edouard@3337: # Report the number of rows in the model edouard@3337: def GetCount(self): edouard@3337: #self.log.write('GetCount') edouard@3337: return len(self.data) edouard@3337: edouard@3337: # Called to check if non-standard attributes should be used in the edouard@3337: # cell at (row, col) edouard@3337: def GetAttrByRow(self, row, col, attr): edouard@3337: if col == 5: edouard@3337: attr.SetColour('blue') edouard@3337: attr.SetBold(True) edouard@3337: return True edouard@3337: return False edouard@3337: edouard@3337: edouard@3337: def DeleteRows(self, rows): edouard@3337: # make a copy since we'll be sorting(mutating) the list edouard@3337: # use reverse order so the indexes don't change as we remove items edouard@3337: rows = sorted(rows, reverse=True) edouard@3337: edouard@3337: for row in rows: edouard@3337: # remove it from our data structure edouard@3337: del self.data[row] edouard@3337: # notify the view(s) using this model that it has been removed edouard@3337: self.RowDeleted(row) edouard@3337: edouard@3337: edouard@3337: def AddRow(self, value): edouard@3378: if self.data.append(value): edouard@3378: # notify views edouard@3378: self.RowAppended() edouard@3337: edouard@3337: def ResetData(self): edouard@3337: self.Reset(len(self.data)) edouard@3337: edouard@3337: OPCUAClientDndMagicWord = "text/beremiz-opcuaclient" edouard@3337: edouard@3337: class NodeDropTarget(wx.DropTarget): edouard@3337: edouard@3337: def __init__(self, parent): edouard@3337: data = wx.CustomDataObject(OPCUAClientDndMagicWord) edouard@3337: wx.DropTarget.__init__(self, data) edouard@3337: self.ParentWindow = parent edouard@3337: edouard@3337: def OnDrop(self, x, y): edouard@3337: self.ParentWindow.OnNodeDnD() edouard@3337: return True edouard@3337: edouard@3337: class OPCUASubListPanel(wx.Panel): edouard@3337: def __init__(self, parent, log, model, direction): edouard@3337: self.log = log edouard@3337: wx.Panel.__init__(self, parent, -1) edouard@3337: edouard@3337: self.dvc = dv.DataViewCtrl(self, edouard@3337: style=wx.BORDER_THEME edouard@3337: | dv.DV_ROW_LINES edouard@3337: | dv.DV_HORIZ_RULES edouard@3337: | dv.DV_VERT_RULES edouard@3337: | dv.DV_MULTIPLE edouard@3337: ) edouard@3337: edouard@3337: self.model = model edouard@3337: edouard@3337: self.dvc.AssociateModel(self.model) edouard@3337: edouard@3337: for idx,(colname,width) in enumerate(zip(lstcolnames,lstcolwidths)): edouard@3337: self.dvc.AppendTextColumn(colname, idx, width=width, mode=dv.DATAVIEW_CELL_EDITABLE) edouard@3337: edouard@3337: DropTarget = NodeDropTarget(self) edouard@3666: self.SetDropTarget(DropTarget) edouard@3337: edouard@3337: self.Sizer = wx.BoxSizer(wx.VERTICAL) edouard@3337: edouard@3337: self.direction = direction edouard@3337: titlestr = direction + " variables" edouard@3337: edouard@3337: title = wx.StaticText(self, label = titlestr) edouard@3337: edouard@3337: delbt = wx.Button(self, label="Delete Row(s)") edouard@3337: self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, delbt) edouard@3337: edouard@3337: topsizer = wx.BoxSizer(wx.HORIZONTAL) edouard@3337: topsizer.Add(title, 1, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 5) edouard@3337: topsizer.Add(delbt, 0, wx.LEFT|wx.RIGHT, 5) edouard@3337: self.Sizer.Add(topsizer, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5) edouard@3337: self.Sizer.Add(self.dvc, 1, wx.EXPAND) edouard@3337: edouard@3337: edouard@3337: edouard@3337: def OnDeleteRows(self, evt): edouard@3337: items = self.dvc.GetSelections() edouard@3337: rows = [self.model.GetRow(item) for item in items] edouard@3337: self.model.DeleteRows(rows) edouard@3337: edouard@3337: edouard@3337: def OnNodeDnD(self): edouard@3337: # Have to find OPC-UA client extension panel from here edouard@3337: # in order to avoid keeping reference (otherwise __del__ isn't called) edouard@3337: # splitter. panel. splitter edouard@3337: ClientPanel = self.GetParent().GetParent().GetParent() edouard@3337: nodes = ClientPanel.GetSelectedNodes() edouard@3820: for node, properties in nodes: edouard@3820: if properties.cname != "Variable": edouard@3820: self.log("Node {} ignored (not a variable)".format(properties.dname)) edouard@3337: continue edouard@3337: edouard@3820: tname = properties.variant_type edouard@3337: if tname not in UA_IEC_types: edouard@3820: self.log("Node {} ignored (unsupported type)".format(properties.dname)) edouard@3337: continue edouard@3337: edouard@3337: if {"input":ua.AccessLevel.CurrentRead, edouard@3820: "output":ua.AccessLevel.CurrentWrite}[self.direction] not in properties.access: edouard@3820: self.log("Node {} ignored because of insuficient access rights".format(properties.dname)) edouard@3337: continue edouard@3337: edouard@3820: nid_type = type(properties.nid).__name__ edouard@3820: iecid = properties.nid edouard@3820: edouard@3820: value = [properties.dname, edouard@3820: properties.nsid, edouard@3337: nid_type, edouard@3820: properties.nid, edouard@3337: tname, edouard@3337: iecid] edouard@3337: self.model.AddRow(value) edouard@3337: edouard@3337: edouard@3337: edouard@3337: il = None edouard@3337: fldridx = None edouard@3337: fldropenidx = None edouard@3337: fileidx = None edouard@3337: smileidx = None edouard@3337: isz = (16,16) edouard@3337: edouard@3337: treecolnames = [ "Name", "Class", "NSIdx", "Id"] edouard@3337: treecolwidths = [ 250, 100, 50, 200] edouard@3337: edouard@3337: edouard@3337: def prepare_image_list(): edouard@3337: global il, fldridx, fldropenidx, fileidx, smileidx edouard@3337: edouard@3337: if il is not None: edouard@3337: return edouard@3337: edouard@3337: il = wx.ImageList(isz[0], isz[1]) edouard@3337: fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz)) edouard@3337: fldropenidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz)) edouard@3337: fileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz)) edouard@3337: smileidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_OTHER, isz)) edouard@3337: edouard@3337: edouard@3820: AsyncUAClientLoop = None edouard@3820: def AsyncUAClientLoopProc(): edouard@3820: asyncio.set_event_loop(AsyncUAClientLoop) edouard@3820: AsyncUAClientLoop.run_forever() edouard@3820: edouard@3820: def ExecuteSychronously(func, timeout=1): edouard@3820: def AsyncSychronizer(*args, **kwargs): edouard@3820: global AsyncUAClientLoop edouard@3820: # create asyncio loop edouard@3820: if AsyncUAClientLoop is None: edouard@3820: AsyncUAClientLoop = asyncio.new_event_loop() edouard@3820: Thread(target=AsyncUAClientLoopProc, daemon=True).start() edouard@3820: # schedule work in this loop edouard@3820: future = asyncio.run_coroutine_threadsafe(func(*args, **kwargs), AsyncUAClientLoop) edouard@3820: # wait max 5sec until connection completed edouard@3820: return future.result(timeout) edouard@3820: return AsyncSychronizer edouard@3820: edouard@3820: def ExecuteSychronouslyWithTimeout(timeout): edouard@3820: return functools.partial(ExecuteSychronously,timeout=timeout) edouard@3820: edouard@3820: edouard@3337: class OPCUAClientPanel(wx.SplitterWindow): edouard@3589: def __init__(self, parent, modeldata, log, config_getter): edouard@3337: self.log = log edouard@3337: wx.SplitterWindow.__init__(self, parent, -1) edouard@3337: edouard@3820: self.ordered_nps = [] edouard@3337: edouard@3337: self.inout_panel = wx.Panel(self) edouard@3337: self.inout_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) edouard@3337: self.inout_sizer.AddGrowableCol(0) edouard@3337: self.inout_sizer.AddGrowableRow(1) edouard@3337: edouard@3820: self.clientloop = None edouard@3337: self.client = None edouard@3589: self.config_getter = config_getter edouard@3337: edouard@3337: self.connect_button = wx.ToggleButton(self.inout_panel, -1, "Browse Server") edouard@3337: edouard@3337: self.selected_splitter = wx.SplitterWindow(self.inout_panel, style=wx.SUNKEN_BORDER | wx.SP_3D) edouard@3337: edouard@3337: self.selected_datas = modeldata edouard@3337: self.selected_models = { direction:OPCUASubListModel(self.selected_datas[direction], log) for direction in directions } edouard@3337: self.selected_lists = { direction:OPCUASubListPanel( edouard@3337: self.selected_splitter, log, edouard@3337: self.selected_models[direction], direction) edouard@3337: for direction in directions } edouard@3337: edouard@3337: self.selected_splitter.SplitHorizontally(*[self.selected_lists[direction] for direction in directions]+[300]) edouard@3337: edouard@3337: self.inout_sizer.Add(self.connect_button, flag=wx.GROW) edouard@3337: self.inout_sizer.Add(self.selected_splitter, flag=wx.GROW) edouard@3337: self.inout_sizer.Layout() edouard@3337: self.inout_panel.SetAutoLayout(True) edouard@3337: self.inout_panel.SetSizer(self.inout_sizer) edouard@3337: edouard@3337: self.Initialize(self.inout_panel) edouard@3337: edouard@3337: self.Bind(wx.EVT_TOGGLEBUTTON, self.OnConnectButton, self.connect_button) edouard@3337: edouard@3337: def OnClose(self): edouard@3337: if self.client is not None: edouard@3820: asyncio.run(self.client.disconnect()) edouard@3337: self.client = None edouard@3337: edouard@3337: def __del__(self): edouard@3337: self.OnClose() edouard@3337: edouard@3820: async def GetAsyncUANodeProperties(self, node): edouard@3820: properties = type("UANodeProperties",(),dict( edouard@3820: nsid = node.nodeid.NamespaceIndex, edouard@3820: nid = node.nodeid.Identifier, edouard@3820: dname = (await node.read_display_name()).Text, edouard@3820: cname = (await node.read_node_class()).name, edouard@3820: )) edouard@3820: if properties.cname == "Variable": edouard@3820: properties.access = await node.get_access_level() edouard@3820: properties.variant_type = (await node.read_data_type_as_variant_type()).name edouard@3820: return properties edouard@3820: edouard@3820: @ExecuteSychronouslyWithTimeout(5) edouard@3820: async def ConnectAsyncUAClient(self, config): edouard@3820: client = Client(config["URI"]) edouard@3820: edouard@3820: AuthType = config["AuthType"] edouard@3820: if AuthType=="UserPasword": edouard@3820: await client.set_user(config["User"]) edouard@3820: await client.set_password(config["Password"]) edouard@3820: elif AuthType=="x509": edouard@3820: await client.set_security_string( edouard@3820: "{Policy},{Mode},{Certificate},{PrivateKey}".format(**config)) edouard@3820: edouard@3820: await client.connect() edouard@3820: self.client = client edouard@3820: edouard@3820: # load definition of server specific structures/extension objects edouard@3820: await self.client.load_type_definitions() edouard@3820: edouard@3820: # returns root node object and its properties edouard@3820: rootnode = self.client.get_root_node() edouard@3820: return rootnode, await self.GetAsyncUANodeProperties(rootnode) edouard@3820: edouard@3820: @ExecuteSychronously edouard@3820: async def DisconnectAsyncUAClient(self): edouard@3820: if self.client is not None: edouard@3820: await self.client.disconnect() edouard@3820: self.client = None edouard@3820: edouard@3820: @ExecuteSychronously edouard@3820: async def GetAsyncUANodeChildren(self, node): edouard@3820: children = await node.get_children() edouard@3820: return [ (child, await self.GetAsyncUANodeProperties(child)) for child in children] edouard@3820: edouard@3337: def OnConnectButton(self, event): edouard@3337: if self.connect_button.GetValue(): edouard@3337: edouard@3589: config = self.config_getter() edouard@3667: self.log("OPCUA browser: connecting to {}\n".format(config["URI"])) edouard@3589: edouard@3667: try : edouard@3820: rootnode, rootnodeproperties = self.ConnectAsyncUAClient(config) edouard@3667: except Exception as e: edouard@3820: self.log("Exception in OPCUA browser: "+repr(e)+"\n") edouard@3667: self.client = None edouard@3667: self.connect_button.SetValue(False) edouard@3667: return edouard@3667: edouard@3667: self.tree_panel = wx.Panel(self) edouard@3667: self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) edouard@3667: self.tree_sizer.AddGrowableCol(0) edouard@3667: self.tree_sizer.AddGrowableRow(0) edouard@3667: edouard@3667: self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle= edouard@3667: gizmos.TR_DEFAULT_STYLE edouard@3667: | gizmos.TR_MULTIPLE edouard@3667: | gizmos.TR_FULL_ROW_HIGHLIGHT edouard@3667: ) edouard@3667: edouard@3667: prepare_image_list() edouard@3667: self.tree.SetImageList(il) edouard@3667: edouard@3667: for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)): edouard@3667: self.tree.AddColumn(colname) edouard@3667: self.tree.SetColumnWidth(idx, width) edouard@3667: edouard@3667: self.tree.SetMainColumn(0) edouard@3667: edouard@3820: rootitem = self.AddNodeItem(self.tree.AddRoot, rootnode, rootnodeproperties) edouard@3337: edouard@3337: # Populate first level so that root can be expanded edouard@3337: self.CreateSubItems(rootitem) edouard@3337: edouard@3337: self.tree.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpand) edouard@3337: edouard@3337: self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeNodeSelection) edouard@3337: self.tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag) edouard@3337: edouard@3337: self.tree.Expand(rootitem) edouard@3337: edouard@3820: hint = wx.StaticText(self.tree_panel, label = "Drag'n'drop desired variables from tree to Input or Output list") edouard@3337: edouard@3337: self.tree_sizer.Add(self.tree, flag=wx.GROW) edouard@3337: self.tree_sizer.Add(hint, flag=wx.GROW) edouard@3337: self.tree_sizer.Layout() edouard@3337: self.tree_panel.SetAutoLayout(True) edouard@3337: self.tree_panel.SetSizer(self.tree_sizer) edouard@3337: edouard@3337: self.SplitVertically(self.tree_panel, self.inout_panel, 500) edouard@3337: else: edouard@3820: self.DisconnectAsyncUAClient() edouard@3337: self.Unsplit(self.tree_panel) edouard@3337: self.tree_panel.Destroy() edouard@3337: edouard@3337: def CreateSubItems(self, item): edouard@3820: node, properties, browsed = self.tree.GetPyData(item) edouard@3337: if not browsed: edouard@3820: children = self.GetAsyncUANodeChildren(node) edouard@3820: for subnode, subproperties in children: edouard@3820: self.AddNodeItem(lambda n: self.tree.AppendItem(item, n), subnode, subproperties) edouard@3820: self.tree.SetPyData(item,(node, properties, True)) edouard@3820: edouard@3820: def AddNodeItem(self, item_creation_func, node, properties): edouard@3820: item = item_creation_func(properties.dname) edouard@3820: edouard@3820: if properties.cname == "Variable": edouard@3820: access = properties.access edouard@3337: normalidx = fileidx edouard@3337: r = ua.AccessLevel.CurrentRead in access edouard@3337: w = ua.AccessLevel.CurrentWrite in access edouard@3337: if r and w: edouard@3337: ext = "RW" edouard@3337: elif r: edouard@3337: ext = "RO" edouard@3337: elif w: edouard@3337: ext = "WO" # not sure this one exist edouard@3337: else: edouard@3337: ext = "no access" # not sure this one exist edouard@3820: cname = "Var "+properties.variant_type+" (" + ext + ")" edouard@3337: else: edouard@3337: normalidx = fldridx edouard@3337: edouard@3820: self.tree.SetPyData(item,(node, properties, False)) edouard@3820: self.tree.SetItemText(item, properties.cname, 1) edouard@3820: self.tree.SetItemText(item, str(properties.nsid), 2) edouard@3820: self.tree.SetItemText(item, type(properties.nid).__name__+": "+str(properties.nid), 3) edouard@3337: self.tree.SetItemImage(item, normalidx, which = wx.TreeItemIcon_Normal) edouard@3337: self.tree.SetItemImage(item, fldropenidx, which = wx.TreeItemIcon_Expanded) edouard@3337: edouard@3337: return item edouard@3337: edouard@3337: def OnExpand(self, evt): edouard@3337: for item in evt.GetItem().GetChildren(): edouard@3337: self.CreateSubItems(item) edouard@3337: edouard@3337: # def OnActivate(self, evt): edouard@3337: # item = evt.GetItem() edouard@3337: # node, browsed = self.tree.GetPyData(item) edouard@3337: edouard@3337: def OnTreeNodeSelection(self, event): edouard@3337: items = self.tree.GetSelections() edouard@3337: items_pydata = [self.tree.GetPyData(item) for item in items] edouard@3337: edouard@3820: nps = [(node,properties) for node, properties, unused in items_pydata] edouard@3337: edouard@3337: # append new nodes to ordered list edouard@3820: for np in nps: edouard@3820: if np not in self.ordered_nps: edouard@3820: self.ordered_nps.append(np) edouard@3337: edouard@3337: # filter out vanished items edouard@3820: self.ordered_nps = [ edouard@3820: np edouard@3820: for np in self.ordered_nps edouard@3820: if np in nps] edouard@3337: edouard@3337: def GetSelectedNodes(self): edouard@3820: return self.ordered_nps edouard@3337: edouard@3337: def OnTreeBeginDrag(self, event): edouard@3337: """ edouard@3337: Called when a drag is started in tree edouard@3337: @param event: wx.TreeEvent edouard@3337: """ edouard@3820: if self.ordered_nps: edouard@3337: # Just send a recognizable mime-type, drop destination edouard@3337: # will get python data from parent edouard@3337: data = wx.CustomDataObject(OPCUAClientDndMagicWord) edouard@3337: dragSource = wx.DropSource(self) edouard@3337: dragSource.SetData(data) edouard@3337: dragSource.DoDragDrop() edouard@3337: edouard@3337: def Reset(self): edouard@3337: for direction in directions: edouard@3337: self.selected_models[direction].ResetData() edouard@3337: edouard@3337: edouard@3378: class OPCUAClientList(list): edouard@3674: def __init__(self, log, change_callback): edouard@3378: super(OPCUAClientList, self).__init__(self) edouard@3378: self.log = log edouard@3674: self.change_callback = change_callback edouard@3378: edouard@3378: def append(self, value): kinsamanka@3750: v = dict(list(zip(lstcolnames, value))) edouard@3378: edouard@3378: if type(v["IEC"]) != int: edouard@3378: if len(self) == 0: edouard@3378: v["IEC"] = 0 edouard@3378: else: edouard@3378: iecnums = set(zip(*self)[lstcolnames.index("IEC")]) edouard@3378: greatest = max(iecnums) edouard@3378: holes = set(range(greatest)) - iecnums edouard@3378: v["IEC"] = min(holes) if holes else greatest+1 edouard@3378: edouard@3378: if v["IdType"] not in UA_NODE_ID_types: edouard@3378: self.log("Unknown IdType\n".format(value)) edouard@3378: return False edouard@3378: edouard@3378: try: edouard@3378: for t,n in zip(lstcoltypess, lstcolnames): edouard@3378: v[n] = t(v[n]) edouard@3378: except ValueError: edouard@3378: self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"])) edouard@3378: return False edouard@3378: kinsamanka@3750: if len(self)>0 and v["Id"] in list(zip(*self))[lstcolnames.index("Id")]: edouard@3378: self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"])) edouard@3378: return False edouard@3378: edouard@3378: list.append(self, [v[n] for n in lstcolnames]) edouard@3378: edouard@3674: self.change_callback() edouard@3674: edouard@3378: return True edouard@3378: edouard@3674: def __delitem__(self, index): edouard@3674: list.__delitem__(self, index) edouard@3674: self.change_callback() edouard@3674: edouard@3337: class OPCUAClientModel(dict): edouard@3674: def __init__(self, log, change_callback = lambda : None): edouard@3378: super(OPCUAClientModel, self).__init__() edouard@3337: for direction in directions: edouard@3674: self[direction] = OPCUAClientList(log, change_callback) edouard@3337: edouard@3337: def LoadCSV(self,path): edouard@3820: with open(path, 'r') as csvfile: edouard@3337: reader = csv.reader(csvfile, delimiter=',', quotechar='"') kinsamanka@3750: buf = {direction:[] for direction, _model in self.items()} kinsamanka@3750: for direction, model in self.items(): edouard@3378: self[direction][:] = [] edouard@3337: for row in reader: edouard@3337: direction = row[0] edouard@3674: # avoids calling change callback whe loading CSV edouard@3674: list.append(self[direction],row[1:]) edouard@3337: edouard@3337: def SaveCSV(self,path): edouard@3820: with open(path, 'w') as csvfile: kinsamanka@3750: for direction, data in self.items(): edouard@3337: writer = csv.writer(csvfile, delimiter=',', edouard@3337: quotechar='"', quoting=csv.QUOTE_MINIMAL) edouard@3337: for row in data: edouard@3337: writer.writerow([direction] + row) edouard@3337: edouard@3589: def GenerateC(self, path, locstr, config): edouard@3337: template = """/* code generated by beremiz OPC-UA extension */ edouard@3337: edouard@3337: #include edouard@3337: #include edouard@3337: #include edouard@3591: #include edouard@3677: #include edouard@3591: edouard@3591: #include edouard@3591: #include edouard@3591: edouard@3633: #define _Log(level, ...) \\ edouard@3633: {{ \\ edouard@3633: char mstr[256]; \\ edouard@3633: snprintf(mstr, 255, __VA_ARGS__); \\ edouard@3633: LogMessage(level, mstr, strlen(mstr)); \\ edouard@3633: }} edouard@3633: edouard@3633: #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__); edouard@3633: #define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__); edouard@3633: #define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__); edouard@3633: edouard@3591: static UA_INLINE UA_ByteString edouard@3591: loadFile(const char *const path) {{ edouard@3591: UA_ByteString fileContents = UA_STRING_NULL; edouard@3591: edouard@3591: FILE *fp = fopen(path, "rb"); edouard@3591: if(!fp) {{ edouard@3591: errno = 0; edouard@3633: LogError("OPC-UA could not open %s", path); edouard@3591: return fileContents; edouard@3591: }} edouard@3591: edouard@3591: fseek(fp, 0, SEEK_END); edouard@3591: fileContents.length = (size_t)ftell(fp); edouard@3591: fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte)); edouard@3591: if(fileContents.data) {{ edouard@3591: fseek(fp, 0, SEEK_SET); edouard@3591: size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp); edouard@3633: if(read != fileContents.length){{ edouard@3591: UA_ByteString_clear(&fileContents); edouard@3633: LogError("OPC-UA could not read %s", path); edouard@3633: }} edouard@3591: }} else {{ edouard@3591: fileContents.length = 0; edouard@3633: LogError("OPC-UA Not enough memoty to load %s", path); edouard@3591: }} edouard@3591: fclose(fp); edouard@3591: edouard@3591: return fileContents; edouard@3591: }} edouard@3337: edouard@3407: static UA_Client *client; edouard@3591: static UA_ClientConfig *cc; edouard@3337: edouard@3337: #define DECL_VAR(ua_type, C_type, c_loc_name) \\ edouard@3407: static UA_Variant c_loc_name##_variant; \\ edouard@3407: static C_type c_loc_name##_buf = 0; \\ edouard@3337: C_type *c_loc_name = &c_loc_name##_buf; edouard@3337: edouard@3589: {decl} edouard@3589: edouard@3589: void __cleanup_{locstr}(void) edouard@3589: {{ edouard@3337: UA_Client_disconnect(client); edouard@3337: UA_Client_delete(client); edouard@3589: }} edouard@3337: edouard@3591: #define INIT_NoAuth() \\ edouard@3620: LogInfo("OPC-UA Init no auth"); \\ edouard@3591: UA_ClientConfig_setDefault(cc); \\ edouard@3591: retval = UA_Client_connect(client, uri); edouard@3591: edouard@3677: /* Note : Single policy is enforced here, by default open62541 client supports all policies */ edouard@3652: #define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate) \\ edouard@3633: LogInfo("OPC-UA Init x509 %s,%s,%s,%s", #Policy, #UpperCaseMode, PrivateKey, Certificate); \\ edouard@3633: \\ edouard@3591: UA_ByteString certificate = loadFile(Certificate); \\ edouard@3591: UA_ByteString privateKey = loadFile(PrivateKey); \\ edouard@3591: \\ edouard@3591: cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode; \\ edouard@3677: \\ edouard@3677: /* replacement for default behaviour */ \\ edouard@3677: /* UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0); */ \\ edouard@3677: do{{ \\ edouard@3677: retval = UA_ClientConfig_setDefault(cc); \\ edouard@3677: if(retval != UA_STATUSCODE_GOOD) \\ edouard@3677: break; \\ edouard@3677: \\ edouard@3677: UA_SecurityPolicy *sp = (UA_SecurityPolicy*) \\ edouard@3677: UA_realloc(cc->securityPolicies, sizeof(UA_SecurityPolicy) * 2); \\ edouard@3677: if(!sp){{ \\ edouard@3677: retval = UA_STATUSCODE_BADOUTOFMEMORY; \\ edouard@3677: break; \\ edouard@3677: }} \\ edouard@3677: cc->securityPolicies = sp; \\ edouard@3677: \\ edouard@3677: retval = UA_SecurityPolicy_##Policy(&cc->securityPolicies[cc->securityPoliciesSize], \\ edouard@3677: certificate, privateKey, &cc->logger); \\ edouard@3677: if(retval != UA_STATUSCODE_GOOD) {{ \\ edouard@3677: UA_LOG_WARNING(&cc->logger, UA_LOGCATEGORY_USERLAND, \\ edouard@3677: "Could not add SecurityPolicy Policy with error code %s", \\ edouard@3677: UA_StatusCode_name(retval)); \\ edouard@3677: UA_free(cc->securityPolicies); \\ edouard@3677: cc->securityPolicies = NULL; \\ edouard@3677: break; \\ edouard@3677: }} \\ edouard@3677: \\ edouard@3677: ++cc->securityPoliciesSize; \\ edouard@3677: }} while(0); \\ edouard@3591: \\ edouard@3591: retval = UA_Client_connect(client, uri); \\ edouard@3591: \\ edouard@3591: UA_ByteString_clear(&certificate); \\ edouard@3591: UA_ByteString_clear(&privateKey); edouard@3591: edouard@3591: #define INIT_UserPassword(User, Password) \\ edouard@3620: LogInfo("OPC-UA Init UserPassword %s,%s", User, Password); \\ edouard@3612: UA_ClientConfig_setDefault(cc); \\ edouard@3591: retval = UA_Client_connectUsername(client, uri, User, Password); edouard@3591: edouard@3406: #define INIT_READ_VARIANT(ua_type, c_loc_name) \\ edouard@3392: UA_Variant_init(&c_loc_name##_variant); edouard@3392: edouard@3589: #define INIT_WRITE_VARIANT(ua_type, ua_type_enum, c_loc_name) \\ edouard@3392: UA_Variant_setScalar(&c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]); edouard@3337: edouard@3589: int __init_{locstr}(int argc,char **argv) edouard@3589: {{ edouard@3337: UA_StatusCode retval; edouard@3337: client = UA_Client_new(); edouard@3591: cc = UA_Client_getConfig(client); edouard@3591: char *uri = "{uri}"; edouard@3589: {init} edouard@3337: edouard@3589: if(retval != UA_STATUSCODE_GOOD) {{ edouard@3620: LogError("OPC-UA Init Failed %d", retval); edouard@3337: UA_Client_delete(client); edouard@3337: return EXIT_FAILURE; edouard@3589: }} edouard@3612: return 0; edouard@3589: }} edouard@3337: edouard@3392: #define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ edouard@3392: retval = UA_Client_readValueAttribute( \\ edouard@3392: client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant); \\ edouard@3392: if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(&c_loc_name##_variant) && \\ edouard@3589: c_loc_name##_variant.type == &UA_TYPES[ua_type_enum]) {{ \\ edouard@3392: c_loc_name##_buf = *(ua_type*)c_loc_name##_variant.data; \\ edouard@3392: UA_Variant_clear(&c_loc_name##_variant); /* Unalloc requiered on each read ! */ \\ edouard@3589: }} edouard@3589: edouard@3589: void __retrieve_{locstr}(void) edouard@3589: {{ edouard@3337: UA_StatusCode retval; edouard@3589: {retrieve} edouard@3589: }} edouard@3589: edouard@3589: #define WRITE_VALUE(ua_type, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id) \\ edouard@3392: UA_Client_writeValueAttribute( \\ edouard@3392: client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant); edouard@3337: edouard@3589: void __publish_{locstr}(void) edouard@3589: {{ edouard@3589: {publish} edouard@3589: }} edouard@3337: edouard@3337: """ edouard@3337: edouard@3337: formatdict = dict( edouard@3337: locstr = locstr, edouard@3589: uri = config["URI"], edouard@3337: decl = "", edouard@3337: cleanup = "", edouard@3337: init = "", edouard@3337: retrieve = "", edouard@3337: publish = "" edouard@3337: ) edouard@3591: edouard@3591: AuthType = config["AuthType"] edouard@3591: if AuthType == "x509": edouard@3591: config["UpperCaseMode"] = config["Mode"].upper() edouard@3591: formatdict["init"] += """ edouard@3652: INIT_x509({Policy}, {UpperCaseMode}, "{PrivateKey}", "{Certificate}")""".format(**config) edouard@3591: elif AuthType == "UserPassword": edouard@3591: formatdict["init"] += """ edouard@3591: INIT_UserPassword("{User}", "{Password}")""".format(**config) edouard@3591: else: edouard@3591: formatdict["init"] += """ edouard@3591: INIT_NoAuth()""" edouard@3591: kinsamanka@3750: for direction, data in self.items(): edouard@3337: iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction] edouard@3337: for row in data: edouard@3338: name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row edouard@3337: iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type] edouard@3337: c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number) edouard@3338: ua_nodeid_type, id_formating = UA_NODE_ID_types[ua_nodeid_type] edouard@3338: ua_node_id = id_formating.format(_ua_node_id) edouard@3337: edouard@3337: formatdict["decl"] += """ edouard@3337: DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals()) edouard@3392: edouard@3392: if direction == "input": edouard@3591: formatdict["init"] += """ edouard@3392: INIT_READ_VARIANT({ua_type}, {c_loc_name})""".format(**locals()) edouard@3392: formatdict["retrieve"] += """ edouard@3337: READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) edouard@3392: edouard@3392: if direction == "output": edouard@3591: formatdict["init"] += """ edouard@3392: INIT_WRITE_VARIANT({ua_type}, {ua_type_enum}, {c_loc_name})""".format(**locals()) edouard@3392: formatdict["publish"] += """ edouard@3392: WRITE_VALUE({ua_type}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals()) edouard@3337: edouard@3589: Ccode = template.format(**formatdict) edouard@3337: edouard@3337: return Ccode edouard@3337: edouard@3337: if __name__ == "__main__": edouard@3337: edouard@3337: import wx.lib.mixins.inspection as wit edouard@3337: import sys,os edouard@3337: edouard@3337: app = wit.InspectableApp() edouard@3337: edouard@3337: frame = wx.Frame(None, -1, "OPCUA Client Test App", size=(800,600)) edouard@3337: edouard@3589: argc = len(sys.argv) edouard@3589: edouard@3589: config={} edouard@3589: config["URI"] = sys.argv[1] if argc>1 else "opc.tcp://localhost:4840" edouard@3589: edouard@3589: if argc > 2: edouard@3589: AuthType = sys.argv[2] edouard@3589: config["AuthType"] = AuthType kinsamanka@3750: for (name, default), value in zip_longest(authParams[AuthType], sys.argv[3:]): edouard@3589: if value is None: edouard@3589: if default is None: edouard@3589: raise Exception(name+" param expected") edouard@3589: value = default edouard@3589: config[name] = value edouard@3337: edouard@3337: test_panel = wx.Panel(frame) edouard@3337: test_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) edouard@3337: test_sizer.AddGrowableCol(0) edouard@3337: test_sizer.AddGrowableRow(0) edouard@3337: edouard@3378: modeldata = OPCUAClientModel(print) edouard@3337: edouard@3589: opcuatestpanel = OPCUAClientPanel(test_panel, modeldata, print, lambda:config) edouard@3337: edouard@3337: def OnGenerate(evt): edouard@3337: dlg = wx.FileDialog( edouard@3337: frame, message="Generate file as ...", defaultDir=os.getcwd(), edouard@3337: defaultFile="", edouard@3337: wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT edouard@3337: ) edouard@3337: edouard@3337: if dlg.ShowModal() == wx.ID_OK: edouard@3337: path = dlg.GetPath() edouard@3337: Ccode = """ edouard@3337: /* edouard@3337: In case open62541 was built just aside beremiz, you can build this test with: edouard@3337: gcc %s -o %s \\ edouard@3337: -I ../../open62541/plugins/include/ \\ edouard@3337: -I ../../open62541/build/src_generated/ \\ edouard@3337: -I ../../open62541/include/ \\ edouard@3337: -I ../../open62541/arch/ ../../open62541/build/bin/libopen62541.a edouard@3337: */ edouard@3337: edouard@3589: """%(path, path[:-2]) + modeldata.GenerateC(path, "test", config) + """ edouard@3337: edouard@3612: int LogMessage(uint8_t level, char* buf, uint32_t size){ edouard@3620: printf("log level:%d message:'%.*s'\\n", level, size, buf); edouard@3612: }; edouard@3612: edouard@3337: int main(int argc, char *argv[]) { edouard@3337: edouard@3337: __init_test(arc,argv); edouard@3337: edouard@3337: __retrieve_test(); edouard@3337: edouard@3337: __publish_test(); edouard@3337: edouard@3337: __cleanup_test(); edouard@3337: edouard@3337: return EXIT_SUCCESS; edouard@3337: } edouard@3337: """ edouard@3337: edouard@3820: with open(path, 'w') as Cfile: edouard@3337: Cfile.write(Ccode) edouard@3337: edouard@3337: edouard@3337: dlg.Destroy() edouard@3337: edouard@3337: def OnLoad(evt): edouard@3337: dlg = wx.FileDialog( edouard@3337: frame, message="Choose a file", edouard@3337: defaultDir=os.getcwd(), edouard@3337: defaultFile="", edouard@3337: wildcard="CSV (*.csv)|*.csv", edouard@3337: style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST ) edouard@3337: edouard@3337: if dlg.ShowModal() == wx.ID_OK: edouard@3337: path = dlg.GetPath() edouard@3337: modeldata.LoadCSV(path) edouard@3337: opcuatestpanel.Reset() edouard@3337: edouard@3337: dlg.Destroy() edouard@3337: edouard@3337: def OnSave(evt): edouard@3337: dlg = wx.FileDialog( edouard@3337: frame, message="Save file as ...", defaultDir=os.getcwd(), edouard@3337: defaultFile="", edouard@3337: wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT edouard@3337: ) edouard@3337: edouard@3337: if dlg.ShowModal() == wx.ID_OK: edouard@3337: path = dlg.GetPath() edouard@3337: modeldata.SaveCSV(path) edouard@3337: edouard@3337: dlg.Destroy() edouard@3337: edouard@3337: test_sizer.Add(opcuatestpanel, flag=wx.GROW) edouard@3337: edouard@3337: testbt_sizer = wx.BoxSizer(wx.HORIZONTAL) edouard@3337: edouard@3337: loadbt = wx.Button(test_panel, label="Load") edouard@3337: test_panel.Bind(wx.EVT_BUTTON, OnLoad, loadbt) edouard@3337: edouard@3337: savebt = wx.Button(test_panel, label="Save") edouard@3337: test_panel.Bind(wx.EVT_BUTTON, OnSave, savebt) edouard@3337: edouard@3337: genbt = wx.Button(test_panel, label="Generate") edouard@3337: test_panel.Bind(wx.EVT_BUTTON, OnGenerate, genbt) edouard@3337: edouard@3337: testbt_sizer.Add(loadbt, 0, wx.LEFT|wx.RIGHT, 5) edouard@3337: testbt_sizer.Add(savebt, 0, wx.LEFT|wx.RIGHT, 5) edouard@3337: testbt_sizer.Add(genbt, 0, wx.LEFT|wx.RIGHT, 5) edouard@3337: edouard@3337: test_sizer.Add(testbt_sizer, flag=wx.GROW) edouard@3337: test_sizer.Layout() edouard@3337: test_panel.SetAutoLayout(True) edouard@3337: test_panel.SetSizer(test_sizer) edouard@3337: edouard@3337: def OnClose(evt): edouard@3337: opcuatestpanel.OnClose() edouard@3337: evt.Skip() edouard@3337: edouard@3337: frame.Bind(wx.EVT_CLOSE, OnClose) edouard@3337: edouard@3337: frame.Show() edouard@3337: edouard@3337: app.MainLoop() edouard@3337: