Beremiz_service.py
changeset 2644 769fefae7c81
parent 2601 9c5b20dc2b2e
child 2650 449c9539887a
equal deleted inserted replaced
2643:b98d9e08231f 2644:769fefae7c81
    28 from __future__ import print_function
    28 from __future__ import print_function
    29 import os
    29 import os
    30 import sys
    30 import sys
    31 import getopt
    31 import getopt
    32 import threading
    32 import threading
       
    33 import shlex
    33 from threading import Thread, Semaphore, Lock, currentThread
    34 from threading import Thread, Semaphore, Lock, currentThread
    34 from builtins import str as text
    35 from builtins import str as text
    35 from past.builtins import execfile
    36 from past.builtins import execfile
    36 from six.moves import builtins
    37 from six.moves import builtins
    37 
    38 
    42 from runtime import PlcStatus
    43 from runtime import PlcStatus
    43 from runtime import default_evaluator
    44 from runtime import default_evaluator
    44 from runtime.Stunnel import ensurePSK
    45 from runtime.Stunnel import ensurePSK
    45 import util.paths as paths
    46 import util.paths as paths
    46 
    47 
       
    48 try:
       
    49     from runtime.spawn_subprocess import Popen
       
    50 except ImportError:
       
    51     from subprocess import Popen
    47 
    52 
    48 def version():
    53 def version():
    49     from version import app_version
    54     from version import app_version
    50     print("Beremiz_service: ", app_version)
    55     print("Beremiz_service: ", app_version)
    51 
    56 
    70            working_dir - directory where are stored PLC files
    75            working_dir - directory where are stored PLC files
    71 """ % sys.argv[0])
    76 """ % sys.argv[0])
    72 
    77 
    73 
    78 
    74 try:
    79 try:
    75     opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:w:c:e:s:h", ["help", "version"])
    80     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="])
    76 except getopt.GetoptError as err:
    81 except getopt.GetoptError as err:
    77     # print help information and exit:
    82     # print help information and exit:
    78     print(str(err))  # will print something like "option -a not recognized"
    83     print(str(err))  # will print something like "option -a not recognized"
    79     usage()
    84     usage()
    80     sys.exit(2)
    85     sys.exit(2)
    91 havewx = False
    96 havewx = False
    92 enabletwisted = True
    97 enabletwisted = True
    93 havetwisted = False
    98 havetwisted = False
    94 
    99 
    95 extensions = []
   100 extensions = []
       
   101 statuschange = []
       
   102 def status_change_call_factory(wanted, args):
       
   103     def status_change_call(status):
       
   104         if wanted is None or status is wanted:
       
   105             cmd = shlex.split(args.format(status))
       
   106             Popen(cmd)
       
   107     return status_change_call
    96 
   108 
    97 for o, a in opts:
   109 for o, a in opts:
    98     if o == "-h" or o == "--help":
   110     if o == "-h" or o == "--help":
    99         usage()
   111         usage()
   100         sys.exit()
   112         sys.exit()
   101     if o == "--version":
   113     if o == "--version":
   102         version()
   114         version()
   103         sys.exit()
   115         sys.exit()
       
   116     if o == "--on-plc-start":
       
   117         statuschange.append(status_change_call_factory(PlcStatus.Started, a))
       
   118     elif o == "--on-plc-stop":
       
   119         statuschange.append(status_change_call_factory(PlcStatus.Stopped, a))
       
   120     elif o == "--status-change":
       
   121         statuschange.append(status_change_call_factory(None, a))
   104     elif o == "-i":
   122     elif o == "-i":
   105         if len(a.split(".")) == 4:
   123         if len(a.split(".")) == 4:
   106             interface = a
   124             interface = a
   107         elif a == "localhost":
   125         elif a == "localhost":
   108             interface = '127.0.0.1'
   126             interface = '127.0.0.1'
   399         except ImportError:
   417         except ImportError:
   400             print(_("Twisted unavailable."))
   418             print(_("Twisted unavailable."))
   401             havetwisted = False
   419             havetwisted = False
   402 
   420 
   403 pyruntimevars = {}
   421 pyruntimevars = {}
   404 statuschange = []
       
   405 
   422 
   406 if havetwisted:
   423 if havetwisted:
   407     if havewx:
   424     if havewx:
   408         reactor.registerWxApp(app)
   425         reactor.registerWxApp(app)
   409 
   426