1 import wx |
1 import wx |
2 import wx.grid |
2 import wx.grid |
3 import wx.gizmos |
3 import wx.gizmos |
4 |
4 |
5 from controls import CustomGrid, CustomTable, EditorPanel |
5 from controls import CustomGrid, CustomTable, EditorPanel |
|
6 |
|
7 SCAN_COMMAND = """ |
|
8 import commands |
|
9 result = commands.getoutput("ethercat slaves") |
|
10 slaves = [] |
|
11 for slave_line in result.splitlines(): |
|
12 chunks = slave_line.split() |
|
13 idx, pos, state, flag = chunks[:4] |
|
14 name = " ".join(chunks[4:]) |
|
15 alias, position = pos.split(":") |
|
16 slave = {"idx": int(idx), |
|
17 "alias": int(alias), |
|
18 "position": int(position), |
|
19 "name": name} |
|
20 details = commands.getoutput("ethercat slaves -p %d -v" % slave["idx"]) |
|
21 for details_line in details.splitlines(): |
|
22 details_line = details_line.strip() |
|
23 for header, param in [("Vendor Id:", "vendor_id"), |
|
24 ("Product code:", "product_code"), |
|
25 ("Revision number:", "revision_number")]: |
|
26 if details_line.startswith(header): |
|
27 slave[param] = int(details_line.split()[-1], 16) |
|
28 break |
|
29 slaves.append(slave) |
|
30 returnVal = slaves |
|
31 """ |
6 |
32 |
7 [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3) |
33 [ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE] = range(3) |
8 |
34 |
9 def AppendMenu(parent, help, id, kind, text): |
35 def AppendMenu(parent, help, id, kind, text): |
10 if wx.VERSION >= (2, 6, 0): |
36 if wx.VERSION >= (2, 6, 0): |
519 event.Skip() |
545 event.Skip() |
520 |
546 |
521 [ID_CONFIGEDITOR, ID_CONFIGEDITORSLAVENODES, |
547 [ID_CONFIGEDITOR, ID_CONFIGEDITORSLAVENODES, |
522 ] = [wx.NewId() for _init_ctrls in range(2)] |
548 ] = [wx.NewId() for _init_ctrls in range(2)] |
523 |
549 |
524 [ID_CONFIGEDITORPLUGINMENUADDSLAVE, ID_CONFIGEDITORPLUGINMENUDELETESLAVE, |
550 [ID_CONFIGEDITORPLUGINMENUSCANNETWORK, ID_CONFIGEDITORPLUGINMENUADDSLAVE, |
525 ] = [wx.NewId() for _init_coll_PluginMenu_Items in range(2)] |
551 ID_CONFIGEDITORPLUGINMENUDELETESLAVE, |
|
552 ] = [wx.NewId() for _init_coll_PluginMenu_Items in range(3)] |
526 |
553 |
527 class ConfigEditor(EditorPanel): |
554 class ConfigEditor(EditorPanel): |
528 |
555 |
529 ID = ID_CONFIGEDITOR |
556 ID = ID_CONFIGEDITOR |
530 |
557 |
553 |
580 |
554 self._init_sizers() |
581 self._init_sizers() |
555 |
582 |
556 def _init_MenuItems(self): |
583 def _init_MenuItems(self): |
557 self.MenuItems = [ |
584 self.MenuItems = [ |
|
585 (wx.ITEM_NORMAL, (_("Scan network"), ID_CONFIGEDITORPLUGINMENUSCANNETWORK, '', self.OnScanNetworkMenu)), |
|
586 (wx.ITEM_SEPARATOR, None), |
558 (wx.ITEM_NORMAL, (_("Add slave"), ID_CONFIGEDITORPLUGINMENUADDSLAVE, '', self.OnAddSlaveMenu)), |
587 (wx.ITEM_NORMAL, (_("Add slave"), ID_CONFIGEDITORPLUGINMENUADDSLAVE, '', self.OnAddSlaveMenu)), |
559 (wx.ITEM_NORMAL, (_("Delete slave"), ID_CONFIGEDITORPLUGINMENUDELETESLAVE, '', self.OnDeleteSlaveMenu)), |
588 (wx.ITEM_NORMAL, (_("Delete slave"), ID_CONFIGEDITORPLUGINMENUDELETESLAVE, '', self.OnDeleteSlaveMenu)), |
560 ] |
589 ] |
561 |
590 |
562 def __init__(self, parent, controler, window): |
591 def __init__(self, parent, controler, window): |
621 panel = self.SlaveNodes.GetPage(idx) |
650 panel = self.SlaveNodes.GetPage(idx) |
622 if panel.GetSlave() == slave: |
651 if panel.GetSlave() == slave: |
623 self.SlaveNodes.SetSelection(idx) |
652 self.SlaveNodes.SetSelection(idx) |
624 return |
653 return |
625 |
654 |
|
655 def OnScanNetworkMenu(self, event): |
|
656 error, returnVal = self.Controler.RemoteExec(SCAN_COMMAND, returnVal = None) |
|
657 if error != 0: |
|
658 dialog = wx.MessageDialog(self, returnVal, "Error", wx.OK|wx.ICON_ERROR) |
|
659 dialog.ShowModal() |
|
660 dialog.Destroy() |
|
661 elif returnVal is not None: |
|
662 print returnVal |
|
663 wx.CallAfter(self.RefreshView) |
|
664 |
626 def OnAddSlaveMenu(self, event): |
665 def OnAddSlaveMenu(self, event): |
627 slave = self.Controler.AddSlave() |
666 slave = self.Controler.AddSlave() |
628 self.RefreshParentWindow() |
667 self.RefreshParentWindow() |
629 wx.CallAfter(self.RefreshView, slave) |
668 wx.CallAfter(self.RefreshView, slave) |
630 |
669 |