21 # |
21 # |
22 # You should have received a copy of the GNU General Public License |
22 # You should have received a copy of the GNU General Public License |
23 # along with this program; if not, write to the Free Software |
23 # along with this program; if not, write to the Free Software |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 |
25 |
|
26 |
|
27 from __future__ import print_function |
26 import os |
28 import os |
27 import sys |
29 import sys |
28 import getopt |
30 import getopt |
29 import threading |
31 import threading |
30 from threading import Thread |
32 from threading import Thread |
35 from runtime import PLCObject, PLCprint, ServicePublisher |
37 from runtime import PLCObject, PLCprint, ServicePublisher |
36 import util.paths as paths |
38 import util.paths as paths |
37 |
39 |
38 |
40 |
39 def usage(): |
41 def usage(): |
40 print """ |
42 print(""" |
41 Usage of Beremiz PLC execution service :\n |
43 Usage of Beremiz PLC execution service :\n |
42 %s {[-n servicename] [-i IP] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir |
44 %s {[-n servicename] [-i IP] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir |
43 -n - zeroconf service name (default:disabled) |
45 -n - zeroconf service name (default:disabled) |
44 -i - IP address of interface to bind to (default:localhost) |
46 -i - IP address of interface to bind to (default:localhost) |
45 -p - port number default:3000 |
47 -p - port number default:3000 |
50 -w - web server port or "off" (default:8009) |
52 -w - web server port or "off" (default:8009) |
51 -c - WAMP client config file or "off" (default:wampconf.json) |
53 -c - WAMP client config file or "off" (default:wampconf.json) |
52 -e - python extension (absolute path .py) |
54 -e - python extension (absolute path .py) |
53 |
55 |
54 working_dir - directory where are stored PLC files |
56 working_dir - directory where are stored PLC files |
55 """ % sys.argv[0] |
57 """ % sys.argv[0]) |
56 |
58 |
57 |
59 |
58 try: |
60 try: |
59 opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:w:c:e:h") |
61 opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:w:c:e:h") |
60 except getopt.GetoptError, err: |
62 except getopt.GetoptError, err: |
61 # print help information and exit: |
63 # print help information and exit: |
62 print str(err) # will print something like "option -a not recognized" |
64 print(str(err)) # will print something like "option -a not recognized" |
63 usage() |
65 usage() |
64 sys.exit(2) |
66 sys.exit(2) |
65 |
67 |
66 # default values |
68 # default values |
67 given_ip = None |
69 given_ip = None |
300 if self.pyroserver.plcobj is not None: |
302 if self.pyroserver.plcobj is not None: |
301 plcstatus = self.pyroserver.plcobj.GetPLCstatus()[0] |
303 plcstatus = self.pyroserver.plcobj.GetPLCstatus()[0] |
302 if plcstatus is "Stopped": |
304 if plcstatus is "Stopped": |
303 self.pyroserver.plcobj.StartPLC() |
305 self.pyroserver.plcobj.StartPLC() |
304 else: |
306 else: |
305 print _("PLC is empty or already started.") |
307 print(_("PLC is empty or already started.")) |
306 |
308 |
307 def OnTaskBarStopPLC(self, evt): |
309 def OnTaskBarStopPLC(self, evt): |
308 if self.pyroserver.plcobj is not None: |
310 if self.pyroserver.plcobj is not None: |
309 if self.pyroserver.plcobj.GetPLCstatus()[0] == "Started": |
311 if self.pyroserver.plcobj.GetPLCstatus()[0] == "Started": |
310 Thread(target=self.pyroserver.plcobj.StopPLC).start() |
312 Thread(target=self.pyroserver.plcobj.StopPLC).start() |
311 else: |
313 else: |
312 print _("PLC is not started.") |
314 print(_("PLC is not started.")) |
313 |
315 |
314 def OnTaskBarChangeInterface(self, evt): |
316 def OnTaskBarChangeInterface(self, evt): |
315 ip_addr = self.pyroserver.ip_addr |
317 ip_addr = self.pyroserver.ip_addr |
316 ip_addr = '' if ip_addr is None else ip_addr |
318 ip_addr = '' if ip_addr is None else ip_addr |
317 dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=ip_addr) |
319 dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=ip_addr) |
431 self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, |
433 self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, |
432 self.statuschange, self.evaluator, |
434 self.statuschange, self.evaluator, |
433 self.pyruntimevars) |
435 self.pyruntimevars) |
434 uri = self.daemon.connect(self.plcobj, "PLCObject") |
436 uri = self.daemon.connect(self.plcobj, "PLCObject") |
435 |
437 |
436 print _("Pyro port :"), self.port |
438 print(_("Pyro port :"), self.port) |
437 print _("Pyro object's uri :"), uri |
439 print(_("Pyro object's uri :"), uri) |
438 |
440 |
439 # Beremiz IDE detects daemon start by looking |
441 # Beremiz IDE detects daemon start by looking |
440 # for self.workdir in the daemon's stdout. |
442 # for self.workdir in the daemon's stdout. |
441 # Therefore don't delete the following line |
443 # Therefore don't delete the following line |
442 print _("Current working directory :"), self.workdir |
444 print(_("Current working directory :"), self.workdir) |
443 |
445 |
444 # Configure and publish service |
446 # Configure and publish service |
445 # Not publish service if localhost in address params |
447 # Not publish service if localhost in address params |
446 if self.servicename is not None and \ |
448 if self.servicename is not None and \ |
447 self.ip_addr is not None and \ |
449 self.ip_addr is not None and \ |
448 self.ip_addr != "localhost" and \ |
450 self.ip_addr != "localhost" and \ |
449 self.ip_addr != "127.0.0.1": |
451 self.ip_addr != "127.0.0.1": |
450 print _("Publishing service on local network") |
452 print(_("Publishing service on local network")) |
451 self.servicepublisher = ServicePublisher.ServicePublisher() |
453 self.servicepublisher = ServicePublisher.ServicePublisher() |
452 self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) |
454 self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) |
453 |
455 |
454 self.plcobj.AutoLoad() |
456 self.plcobj.AutoLoad() |
455 if self.plcobj.GetPLCstatus()[0] != "Empty": |
457 if self.plcobj.GetPLCstatus()[0] != "Empty": |
568 if havetwisted: |
570 if havetwisted: |
569 if webport is not None: |
571 if webport is not None: |
570 try: |
572 try: |
571 import runtime.NevowServer as NS |
573 import runtime.NevowServer as NS |
572 except Exception, e: |
574 except Exception, e: |
573 print _("Nevow/Athena import failed :"), e |
575 print(_("Nevow/Athena import failed :"), e) |
574 webport = None |
576 webport = None |
575 NS.WorkingDir = WorkingDir |
577 NS.WorkingDir = WorkingDir |
576 |
578 |
577 if wampconf is not None: |
579 if wampconf is not None: |
578 try: |
580 try: |
579 import runtime.WampClient as WC |
581 import runtime.WampClient as WC |
580 except Exception, e: |
582 except Exception, e: |
581 print _("WAMP import failed :"), e |
583 print(_("WAMP import failed :"), e) |
582 wampconf = None |
584 wampconf = None |
583 |
585 |
584 # Load extensions |
586 # Load extensions |
585 for extfilename in extensions: |
587 for extfilename in extensions: |
586 extension_folder = os.path.split(os.path.realpath(extfilename))[0] |
588 extension_folder = os.path.split(os.path.realpath(extfilename))[0] |
592 try: |
594 try: |
593 website = NS.RegisterWebsite(webport) |
595 website = NS.RegisterWebsite(webport) |
594 pyruntimevars["website"] = website |
596 pyruntimevars["website"] = website |
595 statuschange.append(NS.website_statuslistener_factory(website)) |
597 statuschange.append(NS.website_statuslistener_factory(website)) |
596 except Exception, e: |
598 except Exception, e: |
597 print _("Nevow Web service failed. "), e |
599 print(_("Nevow Web service failed. "), e) |
598 |
600 |
599 if wampconf is not None: |
601 if wampconf is not None: |
600 try: |
602 try: |
601 WC.RegisterWampClient(wampconf) |
603 WC.RegisterWampClient(wampconf) |
602 pyruntimevars["wampsession"] = WC.GetSession |
604 pyruntimevars["wampsession"] = WC.GetSession |
603 WC.SetServer(pyroserver) |
605 WC.SetServer(pyroserver) |
604 except Exception, e: |
606 except Exception, e: |
605 print _("WAMP client startup failed. "), e |
607 print(_("WAMP client startup failed. "), e) |
606 |
608 |
607 |
609 |
608 if havetwisted or havewx: |
610 if havetwisted or havewx: |
609 pyro_thread = Thread(target=pyroserver.Loop) |
611 pyro_thread = Thread(target=pyroserver.Loop) |
610 pyro_thread.start() |
612 pyro_thread.start() |