Add --on-plc-start --on-plc-stop and --status-change to runtime command line, calling given command respectively on start stop or any event. Command line string is python formated (to eventually include status with {}) before being split (supports quoted strings) and passed to Popen for non-blocking execution.
authorEdouard Tisserant
Wed, 18 Dec 2019 13:31:22 +0100
changeset 2644 769fefae7c81
parent 2643 b98d9e08231f
child 2645 d15a997859b1
child 2650 449c9539887a
Add --on-plc-start --on-plc-stop and --status-change to runtime command line, calling given command respectively on start stop or any event. Command line string is python formated (to eventually include status with {}) before being split (supports quoted strings) and passed to Popen for non-blocking execution.
Beremiz_service.py
--- a/Beremiz_service.py	Mon Nov 25 08:10:45 2019 +0100
+++ b/Beremiz_service.py	Wed Dec 18 13:31:22 2019 +0100
@@ -30,6 +30,7 @@
 import sys
 import getopt
 import threading
+import shlex
 from threading import Thread, Semaphore, Lock, currentThread
 from builtins import str as text
 from past.builtins import execfile
@@ -44,6 +45,10 @@
 from runtime.Stunnel import ensurePSK
 import util.paths as paths
 
+try:
+    from runtime.spawn_subprocess import Popen
+except ImportError:
+    from subprocess import Popen
 
 def version():
     from version import app_version
@@ -72,7 +77,7 @@
 
 
 try:
-    opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:w:c:e:s:h", ["help", "version"])
+    opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:w:c:e:s:h", ["help", "version", "status-change=", "on-plc-start=", "on-plc-stop="])
 except getopt.GetoptError as err:
     # print help information and exit:
     print(str(err))  # will print something like "option -a not recognized"
@@ -93,6 +98,13 @@
 havetwisted = False
 
 extensions = []
+statuschange = []
+def status_change_call_factory(wanted, args):
+    def status_change_call(status):
+        if wanted is None or status is wanted:
+            cmd = shlex.split(args.format(status))
+            Popen(cmd)
+    return status_change_call
 
 for o, a in opts:
     if o == "-h" or o == "--help":
@@ -101,6 +113,12 @@
     if o == "--version":
         version()
         sys.exit()
+    if o == "--on-plc-start":
+        statuschange.append(status_change_call_factory(PlcStatus.Started, a))
+    elif o == "--on-plc-stop":
+        statuschange.append(status_change_call_factory(PlcStatus.Stopped, a))
+    elif o == "--status-change":
+        statuschange.append(status_change_call_factory(None, a))
     elif o == "-i":
         if len(a.split(".")) == 4:
             interface = a
@@ -401,7 +419,6 @@
             havetwisted = False
 
 pyruntimevars = {}
-statuschange = []
 
 if havetwisted:
     if havewx: