Beremiz_service.py
changeset 2459 21164625b393
parent 2429 15f18dc8b56a
parent 2450 5024c19ca8f0
child 2476 1881d0ff5ae2
equal deleted inserted replaced
2458:2a70d5240300 2459:21164625b393
    30 import sys
    30 import sys
    31 import getopt
    31 import getopt
    32 import threading
    32 import threading
    33 from threading import Thread, Semaphore, Lock
    33 from threading import Thread, Semaphore, Lock
    34 import __builtin__
    34 import __builtin__
       
    35 from builtins import str as text
       
    36 from past.builtins import execfile
       
    37 from six.moves import builtins
    35 
    38 
    36 import runtime
    39 import runtime
    37 from runtime.PyroServer import Server
    40 from runtime.PyroServer import Server
    38 from runtime.xenomai import TryPreloadXenomai
    41 from runtime.xenomai import TryPreloadXenomai
    39 from runtime import LogMessageAndException
    42 from runtime import LogMessageAndException
       
    43 from runtime import PlcStatus
    40 import util.paths as paths
    44 import util.paths as paths
    41 
    45 
    42 
    46 
    43 def version():
    47 def version():
    44     from version import app_version
    48     from version import app_version
   140     os.chdir(WorkingDir)
   144     os.chdir(WorkingDir)
   141 elif len(argv) == 0:
   145 elif len(argv) == 0:
   142     WorkingDir = os.getcwd()
   146     WorkingDir = os.getcwd()
   143     argv = [WorkingDir]
   147     argv = [WorkingDir]
   144 
   148 
   145 __builtin__.__dict__['_'] = lambda x: x
   149 builtins.__dict__['_'] = lambda x: x
   146 # TODO: add a cmdline parameter if Trying Preloading Xenomai makes problem
   150 # TODO: add a cmdline parameter if Trying Preloading Xenomai makes problem
   147 TryPreloadXenomai()
   151 TryPreloadXenomai()
   148 version()
   152 version()
   149 
   153 
   150 
   154 
   159     langid = wx.LANGUAGE_DEFAULT
   163     langid = wx.LANGUAGE_DEFAULT
   160     # Define translation domain (name of translation files)
   164     # Define translation domain (name of translation files)
   161     domain = "Beremiz"
   165     domain = "Beremiz"
   162 
   166 
   163     # Define locale for wx
   167     # Define locale for wx
   164     loc = __builtin__.__dict__.get('loc', None)
   168     loc = builtins.__dict__.get('loc', None)
   165     if loc is None:
   169     if loc is None:
   166         wx.LogGui.EnableLogging(False)
   170         wx.LogGui.EnableLogging(False)
   167         loc = wx.Locale(langid)
   171         loc = wx.Locale(langid)
   168         wx.LogGui.EnableLogging(True)
   172         wx.LogGui.EnableLogging(True)
   169         __builtin__.__dict__['loc'] = loc
   173         builtins.__dict__['loc'] = loc
   170         # Define location for searching translation files
   174         # Define location for searching translation files
   171     loc.AddCatalogLookupPathPrefix(localedir)
   175     loc.AddCatalogLookupPathPrefix(localedir)
   172     # Define locale domain
   176     # Define locale domain
   173     loc.AddCatalog(domain)
   177     loc.AddCatalog(domain)
   174 
   178 
   182     # And unicode string returned by wx.GetTranslation() are
   186     # And unicode string returned by wx.GetTranslation() are
   183     # automatically converted to 'ascii' string.
   187     # automatically converted to 'ascii' string.
   184     def unicode_translation(message):
   188     def unicode_translation(message):
   185         return wx.GetTranslation(message).encode(default_locale)
   189         return wx.GetTranslation(message).encode(default_locale)
   186 
   190 
   187     __builtin__.__dict__['_'] = unicode_translation
   191     builtins.__dict__['_'] = unicode_translation
   188     # __builtin__.__dict__['_'] = wx.GetTranslation
   192     # builtins.__dict__['_'] = wx.GetTranslation
   189 
   193 
   190 
   194 
   191 # Life is hard... have a candy.
   195 # Life is hard... have a candy.
   192 # pylint: disable=wrong-import-position,wrong-import-order
   196 # pylint: disable=wrong-import-position,wrong-import-order
   193 if enablewx:
   197 if enablewx:
   198         print("Wx unavailable !")
   202         print("Wx unavailable !")
   199         havewx = False
   203         havewx = False
   200 
   204 
   201     if havewx:
   205     if havewx:
   202         import re
   206         import re
   203         from types import *
       
   204 
   207 
   205         if wx.VERSION >= (3, 0, 0):
   208         if wx.VERSION >= (3, 0, 0):
   206             app = wx.App(redirect=False)
   209             app = wx.App(redirect=False)
   207         else:
   210         else:
   208             app = wx.PySimpleApp(redirect=False)
   211             app = wx.PySimpleApp(redirect=False)
   313 
   316 
   314             def OnTaskBarChangeInterface(self, evt):
   317             def OnTaskBarChangeInterface(self, evt):
   315                 ip_addr = self.pyroserver.ip_addr
   318                 ip_addr = self.pyroserver.ip_addr
   316                 ip_addr = '' if ip_addr is None else ip_addr
   319                 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)
   320                 dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=ip_addr)
   318                 dlg.SetTests([(re.compile('\d{1,3}(?:\.\d{1,3}){3}$').match, _("IP is not valid!")),
   321                 dlg.SetTests([(re.compile(r'\d{1,3}(?:\.\d{1,3}){3}$').match, _("IP is not valid!")),
   319                               (lambda x:len([x for x in x.split(".") if 0 <= int(x) <= 255]) == 4,
   322                               (lambda x:len([x for x in x.split(".") if 0 <= int(x) <= 255]) == 4,
   320                                _("IP is not valid!"))])
   323                                _("IP is not valid!"))])
   321                 if dlg.ShowModal() == wx.ID_OK:
   324                 if dlg.ShowModal() == wx.ID_OK:
   322                     self.pyroserver.ip_addr = dlg.GetValue()
   325                     self.pyroserver.ip_addr = dlg.GetValue()
   323                     self.pyroserver.Restart()
   326                     self.pyroserver.Restart()
   324 
   327 
   325             def OnTaskBarChangePort(self, evt):
   328             def OnTaskBarChangePort(self, evt):
   326                 dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port))
   329                 dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port))
   327                 dlg.SetTests([(UnicodeType.isdigit, _("Port number must be an integer!")), (lambda port: 0 <= int(port) <= 65535, _("Port number must be 0 <= port <= 65535!"))])
   330                 dlg.SetTests([(text.isdigit, _("Port number must be an integer!")), (lambda port: 0 <= int(port) <= 65535, _("Port number must be 0 <= port <= 65535!"))])
   328                 if dlg.ShowModal() == wx.ID_OK:
   331                 if dlg.ShowModal() == wx.ID_OK:
   329                     self.pyroserver.port = int(dlg.GetValue())
   332                     self.pyroserver.port = int(dlg.GetValue())
   330                     self.pyroserver.Restart()
   333                     self.pyroserver.Restart()
   331 
   334 
   332             def OnTaskBarChangeWorkingDir(self, evt):
   335             def OnTaskBarChangeWorkingDir(self, evt):
   366                     Thread(target=self.pyroserver.Quit).start()
   369                     Thread(target=self.pyroserver.Quit).start()
   367                 self.RemoveIcon()
   370                 self.RemoveIcon()
   368                 wx.CallAfter(wx.GetApp().ExitMainLoop)
   371                 wx.CallAfter(wx.GetApp().ExitMainLoop)
   369 
   372 
   370             def UpdateIcon(self, plcstatus):
   373             def UpdateIcon(self, plcstatus):
   371                 if plcstatus is "Started":
   374                 if plcstatus is PlcStatus.Started:
   372                     currenticon = self.MakeIcon(starticon)
   375                     currenticon = self.MakeIcon(starticon)
   373                 elif plcstatus is "Stopped":
   376                 elif plcstatus is PlcStatus.Stopped:
   374                     currenticon = self.MakeIcon(stopicon)
   377                     currenticon = self.MakeIcon(stopicon)
   375                 else:
   378                 else:
   376                     currenticon = self.MakeIcon(defaulticon)
   379                     currenticon = self.MakeIcon(defaulticon)
   377                 self.SetIcon(currenticon, "Beremiz Service")
   380                 self.SetIcon(currenticon, "Beremiz Service")
   378 
   381