runtime/PLCObject.py
changeset 2719 745b64e7c695
parent 2702 f0a70f0246da
child 2720 971bb3503957
--- a/runtime/PLCObject.py	Fri Dec 25 17:12:02 2020 +0000
+++ b/runtime/PLCObject.py	Mon Jan 18 10:59:28 2021 +0100
@@ -360,8 +360,18 @@
                 v = parent.python_runtime_vars["_"+name+"_pack"](t, value)
                 parent.python_runtime_vars["_PySafeSetPLCGlob_"+name](ctypes.byref(v))
 
+        class OnChangeStateClass(object):
+            def __getattr__(self, name):
+                u = parent.python_runtime_vars["_"+name+"_unpack"]
+                return type("changedesc",(),dict(
+                    count = parent.python_runtime_vars["_PyOnChangeCount_"+name].value,
+                    first = u(parent.python_runtime_vars["_PyOnChangeFirst_"+name]),
+                    last = u(parent.python_runtime_vars["_PyOnChangeLast_"+name])))
+
+
         self.python_runtime_vars.update({
             "PLCGlobals":     PLCSafeGlobals(),
+            "OnChange":       OnChangeStateClass(),
             "WorkingDir":     self.workingdir,
             "PLCObject":      self,
             "PLCBinary":      self.PLClibraryHandle,
@@ -440,6 +450,7 @@
 
             if cmd == "Activate":
                 self.PythonRuntimeCall("start")
+                self.PreStartPLC()
                 self.PythonThreadLoop()
                 self.PythonRuntimeCall("stop", reverse_order=True)
             else:  # "Finish"
@@ -471,8 +482,6 @@
             if not self.LoadPLC():
                 self._fail(_("Problem starting PLC : can't load PLC"))
 
-        self.PreStartPLC()
-
         if self.CurrentPLCFilename is not None and self.PLCStatus == PlcStatus.Stopped:
             c_argv = ctypes.c_char_p * len(self.argv)
             res = self._startPLC(len(self.argv), c_argv(*self.argv))
@@ -546,12 +555,15 @@
             os.close(fd)
         self._init_blobs()
 
-    def _BlobAsFile(self, blobID, newpath):
+    def BlobAsFile(self, blobID, newpath):
         blob = self.blobs.pop(blobID, None)
 
         if blob is None:
             raise Exception(_("Missing data to create file: {}").format(newpath))
 
+        self._BlobAsFile(blob, newpath)
+
+    def _BlobAsFile(self, blob, newpath):
         fd, path, _md5sum = blob
         fobj = os.fdopen(fd)
         fobj.flush()
@@ -610,13 +622,13 @@
 
             try:
                 # Create new PLC file
-                self._BlobAsFile(plc_object, new_PLC_filename)
+                self.BlobAsFile(plc_object, new_PLC_filename)
 
                 # Then write the files
                 log = open(extra_files_log, "w")
                 for fname, blobID in extrafiles:
                     fpath = os.path.join(self.workingdir, fname)
-                    self._BlobAsFile(blobID, fpath)
+                    self.BlobAsFile(blobID, fpath)
                     log.write(fname+'\n')
 
                 # Store new PLC filename based on md5 key