plugger.py
changeset 350 a3a5561bde1d
parent 348 054fbf1ae0f8
child 351 89eca146bee0
equal deleted inserted replaced
349:24d4e48714ed 350:a3a5561bde1d
   706         self.IECdebug_lock = Lock()
   706         self.IECdebug_lock = Lock()
   707 
   707 
   708         self.DebugTimer=None
   708         self.DebugTimer=None
   709         self.ResetIECProgramsAndVariables()
   709         self.ResetIECProgramsAndVariables()
   710         
   710         
       
   711         # Timer to pull PLC status
       
   712         ID_STATUSTIMER = wx.NewId()
       
   713         self.StatusTimer = wx.Timer(self.AppFrame, ID_STATUSTIMER)
       
   714         self.AppFrame.Bind(wx.EVT_TIMER, self.PullPLCStatusProc, self.StatusTimer)
       
   715         
   711         #This method are not called here... but in NewProject and OpenProject
   716         #This method are not called here... but in NewProject and OpenProject
   712         #self._AddParamsMembers()
   717         #self._AddParamsMembers()
   713         #self.PluggedChilds = {}
   718         #self.PluggedChilds = {}
   714 
   719 
   715         # In both new or load scenario, no need to save
   720         # In both new or load scenario, no need to save
   716         self.ChangesToSave = False        
   721         self.ChangesToSave = False
   717         # root have no parent
   722         # root have no parent
   718         self.PlugParent = None
   723         self.PlugParent = None
   719         # Keep track of the plugin type name
   724         # Keep track of the plugin type name
   720         self.PlugType = "Beremiz"
   725         self.PlugType = "Beremiz"
   721         # After __init__ root plugin is not valid
   726         # After __init__ root plugin is not valid
   723         self.BuildPath = None
   728         self.BuildPath = None
   724         self.PLCEditor = None
   729         self.PLCEditor = None
   725         self.PLCDebug = None
   730         self.PLCDebug = None
   726         self.DebugThread = None
   731         self.DebugThread = None
   727         self.debug_break = False
   732         self.debug_break = False
       
   733         self.previous_plcstate = None
       
   734         self.StatusPrint = {"Broken": self.logger.write_error,
       
   735                             None: lambda x: None}
   728         # copy PluginMethods so that it can be later customized
   736         # copy PluginMethods so that it can be later customized
   729         self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
   737         self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
   730         self.LoadSTLibrary()
   738         self.LoadSTLibrary()
   731 
   739 
   732     def PluginLibraryFilePath(self):
   740     def PluginLibraryFilePath(self):
  1397     def UpdateMethodsFromPLCStatus(self):
  1405     def UpdateMethodsFromPLCStatus(self):
  1398         # Get PLC state : Running or Stopped
  1406         # Get PLC state : Running or Stopped
  1399         # TODO : use explicit status instead of boolean
  1407         # TODO : use explicit status instead of boolean
  1400         if self._connector is not None:
  1408         if self._connector is not None:
  1401             status = self._connector.GetPLCstatus()
  1409             status = self._connector.GetPLCstatus()
  1402             self.logger.write("PLC is %s\n"%status)
       
  1403         else:
  1410         else:
  1404             status = "Disconnected"
  1411             status = "Disconnected"
  1405         for args in {
  1412         for args in {
  1406                "Started":[("_Run", False),
  1413                "Started":[("_Run", False),
  1407                           ("_Debug", False),
  1414                           ("_Debug", False),
  1413                           ("_Debug", False),
  1420                           ("_Debug", False),
  1414                           ("_Stop", False)],
  1421                           ("_Stop", False)],
  1415                "Dirty":  [("_Run", True),
  1422                "Dirty":  [("_Run", True),
  1416                           ("_Debug", True),
  1423                           ("_Debug", True),
  1417                           ("_Stop", False)],
  1424                           ("_Stop", False)],
       
  1425                "Broken": [("_Run", True),
       
  1426                           ("_Debug", True),
       
  1427                           ("_Stop", False)],
  1418                "Disconnected":  [("_Run", False),
  1428                "Disconnected":  [("_Run", False),
  1419                                  ("_Debug", False),
  1429                                  ("_Debug", False),
  1420                                  ("_Stop", False)],
  1430                                  ("_Stop", False)],
  1421                }.get(status,[]):
  1431                }.get(status,[]):
  1422             self.ShowMethod(*args)
  1432             self.ShowMethod(*args)
  1423         
  1433         return status
       
  1434     
       
  1435     def PullPLCStatusProc(self, event): 
       
  1436         current_status = self.UpdateMethodsFromPLCStatus()
       
  1437         if current_status != self.previous_plcstate:
       
  1438             self.previous_plcstate = current_status
       
  1439             self.StatusPrint.get(current_status, self.logger.write)("PLC is %s\n"%current_status)
       
  1440             self.AppFrame.RefreshAll()
       
  1441             
  1424     def _Run(self):
  1442     def _Run(self):
  1425         """
  1443         """
  1426         Start PLC
  1444         Start PLC
  1427         """
  1445         """
  1428         if self._connector.StartPLC():
  1446         self._connector.StartPLC()
  1429             self.logger.write("Starting PLC\n")
       
  1430         else:
       
  1431             self.logger.write_error("Couldn't start PLC !\n")
       
  1432         self.UpdateMethodsFromPLCStatus()
  1447         self.UpdateMethodsFromPLCStatus()
  1433 
  1448 
  1434     def RegisterDebugVarToConnector(self):
  1449     def RegisterDebugVarToConnector(self):
  1435         self.DebugTimer=None
  1450         self.DebugTimer=None
  1436         Idxs = []
  1451         Idxs = []
  1567 
  1582 
  1568     def _Debug(self):
  1583     def _Debug(self):
  1569         """
  1584         """
  1570         Start PLC (Debug Mode)
  1585         Start PLC (Debug Mode)
  1571         """
  1586         """
  1572         if self.GetIECProgramsAndVariables() and \
  1587         if self.GetIECProgramsAndVariables():
  1573            self._connector.StartPLC(debug=True):
  1588             self._connector.StartPLC(debug=True)
  1574             self.logger.write("Starting PLC (debug mode)\n")
  1589             self.logger.write("Starting PLC (debug mode)\n")
  1575             if self.PLCDebug is None:
  1590             if self.PLCDebug is None:
  1576                 self.RefreshPluginsBlockLists()
  1591                 self.RefreshPluginsBlockLists()
  1577                 def _onclose():
  1592                 def _onclose():
  1578                     self.PLCDebug = None
  1593                     self.PLCDebug = None
  1611         """
  1626         """
  1612         if self.DebugThread is not None:
  1627         if self.DebugThread is not None:
  1613             self.logger.write("Stopping debug\n")
  1628             self.logger.write("Stopping debug\n")
  1614             self.KillDebugThread()
  1629             self.KillDebugThread()
  1615         
  1630         
  1616         if self._connector.StopPLC():
  1631         if not self._connector.StopPLC():
  1617             self.logger.write("Stopping PLC\n")
       
  1618         else:
       
  1619             self.logger.write_error("Couldn't stop PLC !\n")
  1632             self.logger.write_error("Couldn't stop PLC !\n")
  1620         self.UpdateMethodsFromPLCStatus()
  1633         self.UpdateMethodsFromPLCStatus()
  1621 
  1634 
  1622     def _Connect(self):
  1635     def _Connect(self):
  1623         # don't accept re-connetion is already connected
  1636         # don't accept re-connetion is already connected
  1660             self.ShowMethod("_Connect", False)
  1673             self.ShowMethod("_Connect", False)
  1661             self.ShowMethod("_Disconnect", True)
  1674             self.ShowMethod("_Disconnect", True)
  1662             self.ShowMethod("_Transfer", True)
  1675             self.ShowMethod("_Transfer", True)
  1663 
  1676 
  1664             self.CompareLocalAndRemotePLC()
  1677             self.CompareLocalAndRemotePLC()
  1665             self.UpdateMethodsFromPLCStatus()
  1678             
       
  1679             # Init with actual PLC status and print it
       
  1680             self.previous_plcstate = self.UpdateMethodsFromPLCStatus()
       
  1681             self.logger.write("PLC is %s\n"%self.previous_plcstate)
       
  1682             
       
  1683             # Start the status Timer
       
  1684             self.StatusTimer.Start(milliseconds=500, oneShot=False)
  1666 
  1685 
  1667     def CompareLocalAndRemotePLC(self):
  1686     def CompareLocalAndRemotePLC(self):
  1668         if self._connector is None:
  1687         if self._connector is None:
  1669             return
  1688             return
  1670         # We are now connected. Update button status
  1689         # We are now connected. Update button status
  1689     def _Disconnect(self):
  1708     def _Disconnect(self):
  1690         self._connector = None
  1709         self._connector = None
  1691         self.ShowMethod("_Transfer", False)
  1710         self.ShowMethod("_Transfer", False)
  1692         self.ShowMethod("_Connect", True)
  1711         self.ShowMethod("_Connect", True)
  1693         self.ShowMethod("_Disconnect", False)
  1712         self.ShowMethod("_Disconnect", False)
       
  1713         self.StatusTimer.Stop()
  1694         self.UpdateMethodsFromPLCStatus()
  1714         self.UpdateMethodsFromPLCStatus()
  1695         
  1715         
  1696     def _Transfer(self):
  1716     def _Transfer(self):
  1697         # Get the last build PLC's 
  1717         # Get the last build PLC's 
  1698         MD5 = self.GetLastBuildMD5()
  1718         MD5 = self.GetLastBuildMD5()