svghmi/svghmi.py
branchsvghmi
changeset 3286 5a135e635bfa
parent 3277 8e81e4ce9bc6
child 3287 70a76083c59c
equal deleted inserted replaced
3285:d6612579f1d4 3286:5a135e635bfa
   466         t = time.time()
   466         t = time.time()
   467         oldt, m = self.times_msgs[k]
   467         oldt, m = self.times_msgs[k]
   468         self.indent = self.indent - 1
   468         self.indent = self.indent - 1
   469         self.GetCTRoot().logger.write("    "*self.indent + "... finished in %.3fs\n"%(t - oldt))
   469         self.GetCTRoot().logger.write("    "*self.indent + "... finished in %.3fs\n"%(t - oldt))
   470 
   470 
       
   471     def get_SVGHMI_options(self):
       
   472         view_name = self.BaseParams.getName()
       
   473         port = self.GetParamsAttributes("SVGHMI.Port")["value"]
       
   474         interface = self.GetParamsAttributes("SVGHMI.Interface")["value"]
       
   475         path = self.GetParamsAttributes("SVGHMI.Path")["value"].format(name=view_name)
       
   476         if path and path[0]=='/':
       
   477             path = path[1:]
       
   478         enable_watchdog = self.GetParamsAttributes("SVGHMI.EnableWatchdog")["value"]
       
   479         url="http://"+interface+("" if port==80 else (":"+str(port))
       
   480             ) + (("/"+path) if path else ""
       
   481             ) + ("#watchdog" if enable_watchdog else "")
       
   482 
       
   483         return dict(
       
   484             view_name=view_name,
       
   485             port=port,
       
   486             interface=interface,
       
   487             path=path,
       
   488             enable_watchdog=enable_watchdog,
       
   489             url=url)
       
   490 
   471     def CTNGenerate_C(self, buildpath, locations):
   491     def CTNGenerate_C(self, buildpath, locations):
   472 
   492 
   473         location_str = "_".join(map(str, self.GetCurrentLocation()))
   493         location_str = "_".join(map(str, self.GetCurrentLocation()))
   474         view_name = self.BaseParams.getName()
   494         svghmi_options = self.get_SVGHMI_options()
   475 
   495 
   476         svgfile = self._getSVGpath()
   496         svgfile = self._getSVGpath()
   477 
   497 
   478         res = ([], "", False)
   498         res = ([], "", False)
   479 
   499 
   531                 try: 
   551                 try: 
   532                     self.ProgressStart("xslt", "XSLT transform")
   552                     self.ProgressStart("xslt", "XSLT transform")
   533                     result = transform.transform(svgdom)  # , profile_run=True)
   553                     result = transform.transform(svgdom)  # , profile_run=True)
   534                     self.ProgressEnd("xslt")
   554                     self.ProgressEnd("xslt")
   535                 except XSLTApplyError as e:
   555                 except XSLTApplyError as e:
   536                     self.FatalError("SVGHMI " + view_name  + ": " + e.message)
   556                     self.FatalError("SVGHMI " + svghmi_options["view_name"] + ": " + e.message)
   537                 finally:
   557                 finally:
   538                     for entry in transform.get_error_log():
   558                     for entry in transform.get_error_log():
   539                         message = "SVGHMI: "+ entry.message + "\n" 
   559                         message = "SVGHMI: "+ entry.message + "\n" 
   540                         self.GetCTRoot().logger.write_warning(message)
   560                         self.GetCTRoot().logger.write_warning(message)
   541 
   561 
   566 """)
   586 """)
   567             target_file.close()
   587             target_file.close()
   568 
   588 
   569         res += ((target_fname, open(target_path, "rb")),)
   589         res += ((target_fname, open(target_path, "rb")),)
   570 
   590 
   571         port = self.GetParamsAttributes("SVGHMI.Port")["value"]
       
   572         interface = self.GetParamsAttributes("SVGHMI.Interface")["value"]
       
   573         path = self.GetParamsAttributes("SVGHMI.Path")["value"].format(name=view_name)
       
   574         if path and path[0]=='/':
       
   575             path = path[1:]
       
   576         enable_watchdog = self.GetParamsAttributes("SVGHMI.EnableWatchdog")["value"]
       
   577         url="http://"+interface+("" if port==80 else (":"+str(port))
       
   578             ) + (("/"+path) if path else ""
       
   579             ) + ("#watchdog" if enable_watchdog else "")
       
   580 
       
   581         svghmi_cmds = {}
   591         svghmi_cmds = {}
   582         for thing in ["Start", "Stop", "Watchdog"]:
   592         for thing in ["Start", "Stop", "Watchdog"]:
   583              given_command = self.GetParamsAttributes("SVGHMI.On"+thing)["value"]
   593              given_command = self.GetParamsAttributes("SVGHMI.On"+thing)["value"]
   584              svghmi_cmds[thing] = (
   594              svghmi_cmds[thing] = (
   585                 "Popen(" +
   595                 "Popen(" +
   586                 repr(shlex.split(given_command.format(
   596                 repr(shlex.split(given_command.format(**svghmi_options))) +
   587                     port=port, 
       
   588                     name=view_name,
       
   589                     url=url))) +
       
   590                 ")") if given_command else "pass # no command given"
   597                 ")") if given_command else "pass # no command given"
   591 
   598 
   592         runtimefile_path = os.path.join(buildpath, "runtime_%s_svghmi_.py" % location_str)
   599         runtimefile_path = os.path.join(buildpath, "runtime_%s_svghmi_.py" % location_str)
   593         runtimefile = open(runtimefile_path, 'w')
   600         runtimefile = open(runtimefile_path, 'w')
   594         runtimefile.write("""
   601         runtimefile.write("""
   655 
   662 
   656     {svghmi_cmds[Stop]}
   663     {svghmi_cmds[Stop]}
   657 
   664 
   658         """.format(location=location_str,
   665         """.format(location=location_str,
   659                    xhtml=target_fname,
   666                    xhtml=target_fname,
   660                    view_name=view_name,
       
   661                    svghmi_cmds=svghmi_cmds,
   667                    svghmi_cmds=svghmi_cmds,
   662                    port = port,
       
   663                    interface = interface,
       
   664                    path = path,
       
   665                    enable_watchdog = enable_watchdog,
       
   666                    watchdog_initial = self.GetParamsAttributes("SVGHMI.WatchdogInitial")["value"],
   668                    watchdog_initial = self.GetParamsAttributes("SVGHMI.WatchdogInitial")["value"],
   667                    watchdog_interval = self.GetParamsAttributes("SVGHMI.WatchdogInterval")["value"],
   669                    watchdog_interval = self.GetParamsAttributes("SVGHMI.WatchdogInterval")["value"],
   668                    maxConnections = self.GetParamsAttributes("SVGHMI.MaxConnections")["value"],
   670                    maxConnections = self.GetParamsAttributes("SVGHMI.MaxConnections")["value"],
   669                    maxConnections_total = maxConnectionsTotal
   671                    maxConnections_total = maxConnectionsTotal,
   670                    ))
   672                    **svghmi_options
       
   673         ))
   671 
   674 
   672         runtimefile.close()
   675         runtimefile.close()
   673 
   676 
   674         res += (("runtime_%s_svghmi.py" % location_str, open(runtimefile_path, "rb")),)
   677         res += (("runtime_%s_svghmi.py" % location_str, open(runtimefile_path, "rb")),)
   675 
   678 
   682             if os.path.isfile(svgpath):
   685             if os.path.isfile(svgpath):
   683                 shutil.copy(svgpath, self._getSVGpath())
   686                 shutil.copy(svgpath, self._getSVGpath())
   684             else:
   687             else:
   685                 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath)
   688                 self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath)
   686         dialog.Destroy()
   689         dialog.Destroy()
       
   690 
       
   691     def getDefaultSVG(self):
       
   692         return os.path.join(ScriptDirectory, "default.svg")
   687 
   693 
   688     def _StartInkscape(self):
   694     def _StartInkscape(self):
   689         svgfile = self._getSVGpath()
   695         svgfile = self._getSVGpath()
   690         open_inkscape = True
   696         open_inkscape = True
   691         if not self.GetCTRoot().CheckProjectPathPerm():
   697         if not self.GetCTRoot().CheckProjectPathPerm():
   696             open_inkscape = dialog.ShowModal() == wx.ID_YES
   702             open_inkscape = dialog.ShowModal() == wx.ID_YES
   697             dialog.Destroy()
   703             dialog.Destroy()
   698         if open_inkscape:
   704         if open_inkscape:
   699             if not os.path.isfile(svgfile):
   705             if not os.path.isfile(svgfile):
   700                 # make a copy of default svg from source
   706                 # make a copy of default svg from source
   701                 default = os.path.join(ScriptDirectory, "default.svg")
   707                 default = self.getDefaultSVG()
   702                 shutil.copyfile(default, svgfile)
   708                 shutil.copyfile(default, svgfile)
   703             open_svg(svgfile)
   709             open_svg(svgfile)
   704 
   710 
   705     def _StartPOEdit(self, POFile):
   711     def _StartPOEdit(self, POFile):
   706         open_poedit = True
   712         open_poedit = True