plugger.py
changeset 227 48c13b84505c
parent 222 d0f7d36bf241
child 228 848da15cf513
equal deleted inserted replaced
226:f301f4ed4717 227:48c13b84505c
   650         </xsd:complexType>
   650         </xsd:complexType>
   651       </xsd:element>
   651       </xsd:element>
   652     </xsd:schema>
   652     </xsd:schema>
   653     """
   653     """
   654 
   654 
   655     def __init__(self, frame, logger):
   655     def __init__(self, frame, logger, runtime_port):
   656         PLCControler.__init__(self)
   656         PLCControler.__init__(self)
   657         
   657 
       
   658         self.runtime_port = runtime_port
   658         self.MandatoryParams = None
   659         self.MandatoryParams = None
   659         self.AppFrame = frame
   660         self.AppFrame = frame
   660         self.logger = logger
   661         self.logger = logger
   661         self._builder = None
   662         self._builder = None
   662         self._connector = None
   663         self._connector = None
   663         
   664         
   664         # Setup debug information
   665         # Setup debug information
   665         self.IECdebug_callables = {}
   666         self.IECdebug_datas = {}
   666         self.IECdebug_callables_lock = Lock()
   667         self.IECdebug_lock = Lock()
   667 
   668 
   668         # Timer to prevent rapid-fire when registering many variables
   669         # Timer to prevent rapid-fire when registering many variables
   669         self.DebugTimer=Timer(0.5,self.RegisterDebugVarToConnector)
   670         self.DebugTimer=Timer(0.5,self.RegisterDebugVarToConnector)
   670         self.ResetIECProgramsAndVariables()
   671         self.ResetIECProgramsAndVariables()
   671 
   672 
  1058         return debug_code
  1059         return debug_code
  1059 
  1060 
  1060     def RegisterDebugVarToConnector(self):
  1061     def RegisterDebugVarToConnector(self):
  1061         Idxs = []
  1062         Idxs = []
  1062         if self._connector is not None:
  1063         if self._connector is not None:
  1063             self.IECdebug_callables_lock.acquire()
  1064             self.IECdebug_lock.acquire()
  1064             for IECPath,WeakCallableDict in self.IECdebug_callables:
  1065             for IECPath,data_tuple in self.IECdebug_datas:
       
  1066                 WeakCallableDict, data_log, status = data_tuple
  1065                 if len(WeakCallableDict) == 0:
  1067                 if len(WeakCallableDict) == 0:
  1066                     # Callable Dict is empty.
  1068                     # Callable Dict is empty.
  1067                     # This variable is not needed anymore!
  1069                     # This variable is not needed anymore!
  1068                     self.IECdebug_callables.pop(IECPath)
  1070                     # self.IECdebug_callables.pop(IECPath)
       
  1071                     # TODO
       
  1072                     pass
  1069                 else:
  1073                 else:
  1070                     # Convert 
  1074                     # Convert 
  1071                     Idx = self._IECPathToIdx.get(IECPath,None)
  1075                     Idx = self._IECPathToIdx.get(IECPath,None)
  1072                     if Idx is not None:
  1076                     if Idx is not None:
  1073                         Idxs.append(Idx)
  1077                         Idxs.append(Idx)
  1074                     else:
  1078                     else:
  1075                         self.logger.write_warning("Debug : Unknown variable %s\n"%IECPath)
  1079                         self.logger.write_warning("Debug : Unknown variable %s\n"%IECPath)
  1076             self.IECdebug_callables_lock.release()
  1080             self.IECdebug_lock.release()
  1077             self._connector.TraceVariables(Idxs)
  1081             self._connector.TraceVariables(Idxs)
  1078         
  1082         
  1079     def SubscribeDebugIECVariable(self, IECPath, callable, *args, **kwargs):
  1083     def SubscribeDebugIECVariable(self, IECPath, callable, *args, **kwargs):
  1080         """
  1084         """
  1081         Dispatching use a dictionnary linking IEC variable paths
  1085         Dispatching use a dictionnary linking IEC variable paths
  1082         to a WeakKeyDictionary linking 
  1086         to a WeakKeyDictionary linking 
  1083         weakly referenced callables to optionnal args
  1087         weakly referenced callables to optionnal args
  1084         """
  1088         """
  1085         self.IECdebug_callables_lock.acquire()
  1089         self.IECdebug_lock.acquire()
  1086         # If no entry exist, create a new one with a fresh WeakKeyDictionary
  1090         # If no entry exist, create a new one with a fresh WeakKeyDictionary
  1087         self.IECdebug_callables.setdefault(
  1091         IECdebug_data = self.IECdebug_datas.get(IECPath, None)
  1088                    IECPath, 
  1092         if IECdebug_data is None:
  1089                    WeakKeyDictionary())[callable]=(args, kwargs)
  1093             IECdebug_data  = [
  1090         self.IECdebug_callables_lock.release()
  1094                     WeakKeyDictionary(), # Callables
       
  1095                     [],                  # Data storage [(tick, data),...]
       
  1096                     "Registered"]        # Variable status
       
  1097             self.IECdebug_datas[IECPath] = IECdebug_data
       
  1098         
       
  1099         IECdebug_data[0][callable]=(args, kwargs)
       
  1100 
       
  1101         self.IECdebug_lock.release()
  1091         # Rearm anti-rapid-fire timer
  1102         # Rearm anti-rapid-fire timer
  1092         self.DebugTimer.cancel()
  1103         self.DebugTimer.cancel()
  1093         self.DebugTimer.start()
  1104         self.DebugTimer.start()
  1094         
  1105         
  1095     def Generate_plc_common_main(self):
  1106     def Generate_plc_common_main(self):