Beremiz_service.py
changeset 1997 d9e8fb47340f
parent 1994 1fdc32be71b8
child 1999 36a624779f9f
equal deleted inserted replaced
1996:4ae9c4447947 1997:d9e8fb47340f
   398     except Exception:
   398     except Exception:
   399         res = (None, sys.exc_info())
   399         res = (None, sys.exc_info())
   400     return res
   400     return res
   401 
   401 
   402 
   402 
   403 
       
   404 class Server(object):
   403 class Server(object):
   405     def __init__(self, servicename, ip_addr, port,
   404     def __init__(self, servicename, ip_addr, port,
   406                  workdir, argv,
   405                  workdir, argv,
   407                  statuschange=None, evaluator=default_evaluator,
   406                  statuschange=None, evaluator=default_evaluator,
   408                  pyruntimevars=None):
   407                  pyruntimevars=None):
   435         if self._to_be_published():
   434         if self._to_be_published():
   436             print(_("Publishing service on local network"))
   435             print(_("Publishing service on local network"))
   437 
   436 
   438         sys.stdout.flush()
   437         sys.stdout.flush()
   439 
   438 
   440 
       
   441     def PyroLoop(self, when_ready):
   439     def PyroLoop(self, when_ready):
   442         while self.continueloop:
   440         while self.continueloop:
   443             Pyro.config.PYRO_MULTITHREADED = 0
   441             Pyro.config.PYRO_MULTITHREADED = 0
   444             pyro.initServer()
   442             pyro.initServer()
   445             self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port)
   443             self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port)
   447             # pyro never frees memory after connection close if no timeout set
   445             # pyro never frees memory after connection close if no timeout set
   448             # taking too small timeout value may cause
   446             # taking too small timeout value may cause
   449             # unwanted diconnection when IDE is kept busy for long periods
   447             # unwanted diconnection when IDE is kept busy for long periods
   450             self.daemon.setTimeout(60)
   448             self.daemon.setTimeout(60)
   451 
   449 
   452             uri = self.daemon.connect(self.plcobj, "PLCObject")
   450             self.daemon.connect(self.plcobj, "PLCObject")
   453 
   451 
   454             if self._to_be_published():
   452             if self._to_be_published():
   455                 self.servicepublisher = ServicePublisher.ServicePublisher()
   453                 self.servicepublisher = ServicePublisher.ServicePublisher()
   456                 self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
   454                 self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
   457 
   455 
   481         self.plcobj.AutoLoad()
   479         self.plcobj.AutoLoad()
   482         if self.plcobj.GetPLCstatus()[0] == "Stopped":
   480         if self.plcobj.GetPLCstatus()[0] == "Stopped":
   483             if autostart:
   481             if autostart:
   484                 self.plcobj.StartPLC()
   482                 self.plcobj.StartPLC()
   485         self.plcobj.StatusChange()
   483         self.plcobj.StatusChange()
   486 
       
   487 
       
   488 
   484 
   489 if enabletwisted:
   485 if enabletwisted:
   490     import warnings
   486     import warnings
   491     with warnings.catch_warnings():
   487     with warnings.catch_warnings():
   492         warnings.simplefilter("ignore")
   488         warnings.simplefilter("ignore")
   650     ui_thread_started = Lock()
   646     ui_thread_started = Lock()
   651     ui_thread_started.acquire()
   647     ui_thread_started.acquire()
   652     if havetwisted:
   648     if havetwisted:
   653         # reactor._installSignalHandlersAgain()
   649         # reactor._installSignalHandlersAgain()
   654         def ui_thread_target():
   650         def ui_thread_target():
   655             # FIXME: had to disable SignaHandlers install because 
   651             # FIXME: had to disable SignaHandlers install because
   656             # signal not working in non-main thread
   652             # signal not working in non-main thread
   657             reactor.run(installSignalHandlers=False)
   653             reactor.run(installSignalHandlers=False)
   658     else :
   654     else:
   659         ui_thread_target = app.MainLoop
   655         ui_thread_target = app.MainLoop
   660 
   656 
   661     ui_thread = Thread(target = ui_thread_target)
   657     ui_thread = Thread(target=ui_thread_target)
   662     ui_thread.start()
   658     ui_thread.start()
   663 
   659 
   664     # This order ui loop to unblock main thread when ready.
   660     # This order ui loop to unblock main thread when ready.
   665     if havetwisted:
   661     if havetwisted:
   666         reactor.callLater(0,ui_thread_started.release)
   662         reactor.callLater(0, ui_thread_started.release)
   667     else :
   663     else:
   668         wx.CallAfter(ui_thread_started.release)
   664         wx.CallAfter(ui_thread_started.release)
   669 
   665 
   670     # Wait for ui thread to be effective
   666     # Wait for ui thread to be effective
   671     ui_thread_started.acquire()
   667     ui_thread_started.acquire()
   672     print("UI thread started successfully.")
   668     print("UI thread started successfully.")