runtime/PLCObject.py
changeset 368 86ecd8374dae
parent 366 cd90e4c10261
child 391 9b1801ef99b5
child 393 af20e07e53c5
equal deleted inserted replaced
367:a76ee5307bb7 368:86ecd8374dae
    43     sys.stdout.write("PLCobject : "+message+"\n")
    43     sys.stdout.write("PLCobject : "+message+"\n")
    44     sys.stdout.flush()
    44     sys.stdout.flush()
    45 
    45 
    46 class PLCObject(pyro.ObjBase):
    46 class PLCObject(pyro.ObjBase):
    47     _Idxs = []
    47     _Idxs = []
    48     def __init__(self, workingdir, daemon, argv, statuschange, evaluator):
    48     def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website):
    49         pyro.ObjBase.__init__(self)
    49         pyro.ObjBase.__init__(self)
    50         self.evaluator = evaluator
    50         self.evaluator = evaluator
    51         self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
    51         self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
    52         self.workingdir = workingdir
    52         self.workingdir = workingdir
    53         self.PLCStatus = "Stopped"
    53         self.PLCStatus = "Stopped"
    57         # Creates fake C funcs proxies
    57         # Creates fake C funcs proxies
    58         self._FreePLC()
    58         self._FreePLC()
    59         self.daemon = daemon
    59         self.daemon = daemon
    60         self.statuschange = statuschange
    60         self.statuschange = statuschange
    61         self.hmi_frame = None
    61         self.hmi_frame = None
       
    62         self.website = website
    62         
    63         
    63         # Get the last transfered PLC if connector must be restart
    64         # Get the last transfered PLC if connector must be restart
    64         try:
    65         try:
    65             self.CurrentPLCFilename=open(
    66             self.CurrentPLCFilename=open(
    66                              self._GetMD5FileName(),
    67                              self._GetMD5FileName(),
   203         return False
   204         return False
   204 
   205 
   205     def PrepareRuntimePy(self):
   206     def PrepareRuntimePy(self):
   206         self.python_threads_vars = globals().copy()
   207         self.python_threads_vars = globals().copy()
   207         self.python_threads_vars["WorkingDir"] = self.workingdir
   208         self.python_threads_vars["WorkingDir"] = self.workingdir
       
   209         self.python_threads_vars["website"] = self.website
   208         self.python_threads_vars["_runtime_begin"] = []
   210         self.python_threads_vars["_runtime_begin"] = []
   209         self.python_threads_vars["_runtime_cleanup"] = []
   211         self.python_threads_vars["_runtime_cleanup"] = []
   210 #        pyfile = os.path.join(self.workingdir, "runtime.py")
   212         
   211 #        hmifile = os.path.join(self.workingdir, "hmi.py")
       
   212 #        if os.path.exists(hmifile):
       
   213 #            try:
       
   214 #                execfile(hmifile, self.python_threads_vars)
       
   215 #                if os.path.exists(pyfile):
       
   216 #                    try:
       
   217 #                        # TODO handle exceptions in runtime.py
       
   218 #                        # pyfile may redefine _runtime_cleanup
       
   219 #                        # or even call _PythonThreadProc itself.
       
   220 #                        execfile(pyfile, self.python_threads_vars)
       
   221 #                    except:
       
   222 #                        PLCprint(traceback.format_exc())
       
   223 #                if self.python_threads_vars.has_key('wx'):
       
   224 #                    wx = self.python_threads_vars['wx']
       
   225 #                    # try to instanciate the first frame found.
       
   226 #                    for name, obj in self.python_threads_vars.iteritems():
       
   227 #                        # obj is a class
       
   228 #                        if type(obj)==type(type) and issubclass(obj,wx.Frame):
       
   229 #                            def create_frame():
       
   230 #                                self.hmi_frame = obj(None)
       
   231 #                                self.python_threads_vars[name] = self.hmi_frame
       
   232 #                                # keep track of class... never know
       
   233 #                                self.python_threads_vars['Class_'+name] = obj
       
   234 #                                self.hmi_frame.Bind(wx.EVT_CLOSE, OnCloseFrame)
       
   235 #                                self.hmi_frame.Show()
       
   236 #                            
       
   237 #                            def OnCloseFrame(evt):
       
   238 #                                wx.MessageBox(_("Please stop PLC to close"))
       
   239 #                            create_frame()
       
   240 #                            break
       
   241 #            except:
       
   242 #                PLCprint(traceback.format_exc())
       
   243 #        elif os.path.exists(pyfile):
       
   244 #            try:
       
   245 #                # TODO handle exceptions in runtime.py
       
   246 #                # pyfile may redefine _runtime_cleanup
       
   247 #                # or even call _PythonThreadProc itself.
       
   248 #                execfile(pyfile, self.python_threads_vars)
       
   249 #            except:
       
   250 #                PLCprint(traceback.format_exc())
       
   251         for filename in os.listdir(self.workingdir):
   213         for filename in os.listdir(self.workingdir):
   252             name, ext = os.path.splitext(filename)
   214             name, ext = os.path.splitext(filename)
   253             if name.startswith("runtime") and ext == ".py":
   215             if name.startswith("runtime") and ext == ".py":
   254                 try:
   216                 try:
   255                     # TODO handle exceptions in runtime.py
   217                     # TODO handle exceptions in runtime.py
   265                 if runtime_cleanup is not None:
   227                 if runtime_cleanup is not None:
   266                     self.python_threads_vars["_runtime_cleanup"].append(runtime_cleanup)
   228                     self.python_threads_vars["_runtime_cleanup"].append(runtime_cleanup)
   267         
   229         
   268         for runtime_begin in self.python_threads_vars.get("_runtime_begin", []):
   230         for runtime_begin in self.python_threads_vars.get("_runtime_begin", []):
   269             runtime_begin()
   231             runtime_begin()
       
   232             
       
   233         if self.website is not None:
       
   234             self.website.PLCStarted()
   270 
   235 
   271     def FinishRuntimePy(self):
   236     def FinishRuntimePy(self):
   272         for runtime_cleanup in self.python_threads_vars.get("_runtime_cleanup", []):
   237         for runtime_cleanup in self.python_threads_vars.get("_runtime_cleanup", []):
   273             runtime_cleanup()    
   238             runtime_cleanup()    
   274 #        if self.python_threads_vars is not None:
   239         if self.website is not None:
   275 #            runtime_cleanup = self.python_threads_vars.get("_runtime_cleanup",None)
   240             self.website.PLCStopped()
   276 #        if runtime_cleanup is not None:
       
   277 #            runtime_cleanup()
       
   278 #        if self.hmi_frame is not None:
       
   279 #            self.hmi_frame.Destroy()
       
   280         self.python_threads_vars = None
   241         self.python_threads_vars = None
   281 
   242 
   282     def PythonThreadProc(self, debug):
   243     def PythonThreadProc(self, debug):
   283         PLCprint("PythonThreadProc started")
   244         PLCprint("PythonThreadProc started")
   284         c_argv = ctypes.c_char_p * len(self.argv)
   245         c_argv = ctypes.c_char_p * len(self.argv)