Avoid loading PLC at startup when autostart is not set in command line. As a side effect PLC status is artificially set to Stopped, and StartPLC eventually loads PLC if it is not already loaded.
authorEdouard Tisserant
Fri, 19 Apr 2019 13:11:42 +0200
changeset 2608 a332f989f0b8
parent 2607 bf0c1a40cf37
child 2609 c8cb60bf6c6a
Avoid loading PLC at startup when autostart is not set in command line. As a side effect PLC status is artificially set to Stopped, and StartPLC eventually loads PLC if it is not already loaded.
runtime/PLCObject.py
--- a/runtime/PLCObject.py	Fri Apr 19 10:53:02 2019 +0200
+++ b/runtime/PLCObject.py	Fri Apr 19 13:11:42 2019 +0200
@@ -109,9 +109,9 @@
             self.CurrentPLCFilename = open(
                 self._GetMD5FileName(),
                 "r").read().strip() + lib_ext
-            if self.LoadPLC():
-                self.PLCStatus = PlcStatus.Stopped
-                if autostart:
+            self.PLCStatus = PlcStatus.Stopped
+            if autostart:
+                if self.LoadPLC():
                     self.StartPLC()
                     return
         except Exception:
@@ -451,6 +451,16 @@
 
     @RunInMain
     def StartPLC(self):
+
+        def fail(msg):
+            self.LogMessage(0, msg)
+            self.PLCStatus = PlcStatus.Broken
+            self.StatusChange()
+
+        if self.PLClibraryHandle is None:
+            if not self.LoadPLC():
+                fail(_("Problem starting PLC : can't load PLC"))
+
         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))
@@ -460,9 +470,7 @@
                 self.PythonThreadCommand("Activate")
                 self.LogMessage("PLC started")
             else:
-                self.LogMessage(0, _("Problem starting PLC : error %d" % res))
-                self.PLCStatus = PlcStatus.Broken
-                self.StatusChange()
+                fail(_("Problem starting PLC : error %d" % res))
 
     @RunInMain
     def StopPLC(self):