runtime/PLCObject.py
branchpython3
changeset 3772 ec2babbd5698
parent 3752 9f6f46dbe3ae
child 3776 1fbc900ca1ce
equal deleted inserted replaced
3771:67a0df6478b3 3772:ec2babbd5698
   130         else:
   130         else:
   131             level = LogLevelsDefault
   131             level = LogLevelsDefault
   132             msg, = args
   132             msg, = args
   133         PLCprint(msg)
   133         PLCprint(msg)
   134         if self._LogMessage is not None:
   134         if self._LogMessage is not None:
   135             return self._LogMessage(level, msg, len(msg))
   135             return self._LogMessage(level, msg.encode(), len(msg))
   136         return None
   136         return None
   137 
   137 
   138     @RunInMain
   138     @RunInMain
   139     def ResetLogCount(self):
   139     def ResetLogCount(self):
   140         if self._ResetLogCount is not None:
   140         if self._ResetLogCount is not None:
   158                                      self._log_read_buffer, maxsz,
   158                                      self._log_read_buffer, maxsz,
   159                                      ctypes.byref(tick),
   159                                      ctypes.byref(tick),
   160                                      ctypes.byref(tv_sec),
   160                                      ctypes.byref(tv_sec),
   161                                      ctypes.byref(tv_nsec))
   161                                      ctypes.byref(tv_nsec))
   162             if sz and sz <= maxsz:
   162             if sz and sz <= maxsz:
   163                 self._log_read_buffer[sz] = '\x00'
   163                 return (self._log_read_buffer[:sz].decode(), tick.value,
   164                 return self._log_read_buffer.value, tick.value, tv_sec.value, tv_nsec.value
   164                         tv_sec.value, tv_nsec.value)
   165         elif self._loading_error is not None and level == 0:
   165         elif self._loading_error is not None and level == 0:
   166             return self._loading_error, 0, 0, 0
   166             return self._loading_error, 0, 0, 0
   167         return None
   167         return None
   168 
   168 
   169     def _GetMD5FileName(self):
   169     def _GetMD5FileName(self):
   183             self._PLClibraryHandle = dlopen(self._GetLibFileName())
   183             self._PLClibraryHandle = dlopen(self._GetLibFileName())
   184             self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
   184             self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
   185 
   185 
   186             self.PLC_ID = ctypes.c_char_p.in_dll(self.PLClibraryHandle, "PLC_ID")
   186             self.PLC_ID = ctypes.c_char_p.in_dll(self.PLClibraryHandle, "PLC_ID")
   187             if len(md5) == 32:
   187             if len(md5) == 32:
   188                 self.PLC_ID.value = md5
   188                 self.PLC_ID.value = md5.encode()
   189 
   189 
   190             self._startPLC = self.PLClibraryHandle.startPLC
   190             self._startPLC = self.PLClibraryHandle.startPLC
   191             self._startPLC.restype = ctypes.c_int
   191             self._startPLC.restype = ctypes.c_int
   192             self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
   192             self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
   193 
   193 
   420 
   420 
   421     def PythonThreadLoop(self):
   421     def PythonThreadLoop(self):
   422         res, cmd, blkid = "None", "None", ctypes.c_void_p()
   422         res, cmd, blkid = "None", "None", ctypes.c_void_p()
   423         compile_cache = {}
   423         compile_cache = {}
   424         while True:
   424         while True:
   425             cmd = self._PythonIterator(res, blkid)
   425             cmd = self._PythonIterator(res.encode(), blkid)
   426             FBID = blkid.value
   426             FBID = blkid.value
   427             if cmd is None:
   427             if cmd is None:
   428                 break
   428                 break
       
   429             cmd = cmd.decode()
   429             try:
   430             try:
   430                 self.python_runtime_vars["FBID"] = FBID
   431                 self.python_runtime_vars["FBID"] = FBID
   431                 ccmd, AST = compile_cache.get(FBID, (None, None))
   432                 ccmd, AST = compile_cache.get(FBID, (None, None))
   432                 if ccmd is None or ccmd != cmd:
   433                 if ccmd is None or ccmd != cmd:
   433                     AST = compile(cmd, '<plc>', 'eval')
   434                     AST = compile(cmd, '<plc>', 'eval')
   487             self.PythonThreadAck = None
   488             self.PythonThreadAck = None
   488 
   489 
   489         self.PythonThreadCondLock.release()
   490         self.PythonThreadCondLock.release()
   490 
   491 
   491     def _fail(self, msg):
   492     def _fail(self, msg):
   492         self.LogMessage(0, msg)
   493         self.LogMessage(0, msg.decode())
   493         self.PLCStatus = PlcStatus.Broken
   494         self.PLCStatus = PlcStatus.Broken
   494         self.StatusChange()
   495         self.StatusChange()
   495 
   496 
   496     def PreStartPLC(self):
   497     def PreStartPLC(self):
   497         """ 
   498         """ 
   572 
   573 
   573     @RunInMain
   574     @RunInMain
   574     def SeedBlob(self, seed):
   575     def SeedBlob(self, seed):
   575         blob = (mkstemp(dir=self.tmpdir) + (hashlib.new('md5'),))
   576         blob = (mkstemp(dir=self.tmpdir) + (hashlib.new('md5'),))
   576         _fd, _path, md5sum = blob
   577         _fd, _path, md5sum = blob
   577         md5sum.update(seed)
   578         md5sum.update(seed.encode())
   578         newBlobID = md5sum.digest()
   579         newBlobID = md5sum.digest()
   579         self.blobs[newBlobID] = blob
   580         self.blobs[newBlobID] = blob
   580         return newBlobID
   581         return newBlobID
   581 
   582 
   582     @RunInMain
   583     @RunInMain
   601 
   602 
   602     def BlobAsFile(self, blobID, newpath):
   603     def BlobAsFile(self, blobID, newpath):
   603         blob = self.blobs.pop(blobID, None)
   604         blob = self.blobs.pop(blobID, None)
   604 
   605 
   605         if blob is None:
   606         if blob is None:
   606             raise Exception(_("Missing data to create file: {}").format(newpath))
   607             raise Exception(
       
   608                 _(f"Missing data to create file: {newpath}").decode())
   607 
   609 
   608         self._BlobAsFile(blob, newpath)
   610         self._BlobAsFile(blob, newpath)
   609 
   611 
   610     def _BlobAsFile(self, blob, newpath):
   612     def _BlobAsFile(self, blob, newpath):
   611         fd, path, _md5sum = blob
   613         fd, path, _md5sum = blob