svghmi/svghmi.py
changeset 3647 7c427418396f
parent 3499 72ee59ff959c
child 3657 e0d6f5f0dcc2
child 3662 364f2409eeda
equal deleted inserted replaced
3646:db87744d8900 3647:7c427418396f
   634         for thing in ["Start", "Stop", "Watchdog"]:
   634         for thing in ["Start", "Stop", "Watchdog"]:
   635              given_command = self.GetParamsAttributes("SVGHMI.On"+thing)["value"]
   635              given_command = self.GetParamsAttributes("SVGHMI.On"+thing)["value"]
   636              svghmi_cmds[thing] = (
   636              svghmi_cmds[thing] = (
   637                 "Popen(" +
   637                 "Popen(" +
   638                 repr(shlex.split(given_command.format(**svghmi_options))) +
   638                 repr(shlex.split(given_command.format(**svghmi_options))) +
   639                 ")") if given_command else "pass # no command given"
   639                 ")") if given_command else "None # no command given"
   640 
   640 
   641         runtimefile_path = os.path.join(buildpath, "runtime_%s_svghmi_.py" % location_str)
   641         runtimefile_path = os.path.join(buildpath, "runtime_%s_svghmi_.py" % location_str)
   642         runtimefile = open(runtimefile_path, 'w')
   642         runtimefile = open(runtimefile_path, 'w')
   643         runtimefile.write("""
   643         runtimefile.write("""
   644 # TODO : multiple watchdog (one for each svghmi instance)
   644 #!/usr/bin/env python
       
   645 # -*- coding: utf-8 -*-
       
   646 
       
   647 # generated by beremiz/svghmi/svghmi.py
       
   648 
       
   649 browser_proc = None
       
   650 
   645 def svghmi_{location}_watchdog_trigger():
   651 def svghmi_{location}_watchdog_trigger():
   646     {svghmi_cmds[Watchdog]}
   652     global browser_proc
       
   653     restart_proc = {svghmi_cmds[Watchdog]}
       
   654     waitpid_timeout(restart_proc, "SVGHMI watchdog triggered command")
       
   655     waitpid_timeout(browser_proc, "SVGHMI browser process")
       
   656     browser_proc = None
   647 
   657 
   648 max_svghmi_sessions = {maxConnections_total}
   658 max_svghmi_sessions = {maxConnections_total}
   649 
   659 
   650 def _runtime_{location}_svghmi_start():
   660 def _runtime_{location}_svghmi_start():
   651     global svghmi_watchdog, svghmi_servers
   661     global svghmi_watchdog, svghmi_servers, browser_proc
   652 
   662 
   653     srv = svghmi_servers.get("{interface}:{port}", None)
   663     srv = svghmi_servers.get("{interface}:{port}", None)
   654     if srv is not None:
   664     if srv is not None:
   655         svghmi_root, svghmi_listener, path_list = srv
   665         svghmi_root, svghmi_listener, path_list = srv
   656         if '{path}' in path_list:
   666         if '{path}' in path_list:
   671         NoCacheFile('{xhtml}',
   681         NoCacheFile('{xhtml}',
   672             defaultType='application/xhtml+xml'))
   682             defaultType='application/xhtml+xml'))
   673 
   683 
   674     path_list.append("{path}")
   684     path_list.append("{path}")
   675 
   685 
   676     {svghmi_cmds[Start]}
   686     browser_proc = {svghmi_cmds[Start]}
   677 
   687 
   678     if {enable_watchdog}:
   688     if {enable_watchdog}:
   679         if svghmi_watchdog is None:
   689         if svghmi_watchdog is None:
   680             svghmi_watchdog = Watchdog(
   690             svghmi_watchdog = Watchdog(
   681                 {watchdog_initial},
   691                 {watchdog_initial},
   684         else:
   694         else:
   685             raise Exception("SVGHMI {name}: only one watchdog allowed")
   695             raise Exception("SVGHMI {name}: only one watchdog allowed")
   686 
   696 
   687 
   697 
   688 def _runtime_{location}_svghmi_stop():
   698 def _runtime_{location}_svghmi_stop():
   689     global svghmi_watchdog, svghmi_servers
   699     global svghmi_watchdog, svghmi_servers, browser_proc
   690 
   700 
   691     if svghmi_watchdog is not None:
   701     if svghmi_watchdog is not None:
   692         svghmi_watchdog.cancel()
   702         svghmi_watchdog.cancel()
   693         svghmi_watchdog = None
   703         svghmi_watchdog = None
   694 
   704 
   700     if len(path_list)==0:
   710     if len(path_list)==0:
   701         svghmi_root.delEntity("ws")
   711         svghmi_root.delEntity("ws")
   702         svghmi_listener.stopListening()
   712         svghmi_listener.stopListening()
   703         svghmi_servers.pop("{interface}:{port}")
   713         svghmi_servers.pop("{interface}:{port}")
   704 
   714 
   705     {svghmi_cmds[Stop]}
   715     stop_proc = {svghmi_cmds[Stop]}
       
   716     waitpid_timeout(stop_proc, "SVGHMI stop command")
       
   717     waitpid_timeout(browser_proc, "SVGHMI browser process")
       
   718     browser_proc = None
   706 
   719 
   707         """.format(location=location_str,
   720         """.format(location=location_str,
   708                    xhtml=target_fname,
   721                    xhtml=target_fname,
   709                    svghmi_cmds=svghmi_cmds,
   722                    svghmi_cmds=svghmi_cmds,
   710                    watchdog_initial = self.GetParamsAttributes("SVGHMI.WatchdogInitial")["value"],
   723                    watchdog_initial = self.GetParamsAttributes("SVGHMI.WatchdogInitial")["value"],