# HG changeset patch
# User Edouard Tisserant
# Date 1555672302 -7200
# Node ID a332f989f0b84b9b71a70a46f1d9ae8e2bf3ad3a
# Parent  bf0c1a40cf373143d1205f90a16e68cdabe07768
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.

diff -r bf0c1a40cf37 -r a332f989f0b8 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):