connectors/PYRO/__init__.py
changeset 2429 15f18dc8b56a
parent 2339 48b4eba13064
parent 2418 5587c490a070
child 2430 65ff9a309ff3
equal deleted inserted replaced
2428:e0f16317668e 2429:15f18dc8b56a
    35 import Pyro.core
    35 import Pyro.core
    36 import Pyro.util
    36 import Pyro.util
    37 from Pyro.errors import PyroError
    37 from Pyro.errors import PyroError
    38 
    38 
    39 import PSKManagement as PSK
    39 import PSKManagement as PSK
       
    40 from runtime import PlcStatus
    40 
    41 
    41 zeroconf_service_type = '_PYRO._tcp.local.'
    42 zeroconf_service_type = '_PYRO._tcp.local.'
    42 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    43 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    43 # supported by this connector confnode.
    44 # supported by this connector confnode.
    44 #
    45 #
    89             return None
    90             return None
    90 
    91 
    91     # Try to get the proxy object
    92     # Try to get the proxy object
    92     try:
    93     try:
    93         RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject")
    94         RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject")
    94     except Exception,e:
    95     except Exception:
    95         confnodesroot.logger.write_error(_("Connection to '%s' failed with exception '%s'\n") % (location, str(e)))
    96         confnodesroot.logger.write_error(_("Connection to '%s' failed with exception '%s'\n") % (location, str(e)))
    96         #confnodesroot.logger.write_error(traceback.format_exc())
    97         #confnodesroot.logger.write_error(traceback.format_exc())
    97         return None
    98         return None
    98 
    99 
    99     def PyroCatcher(func, default=None):
   100     def PyroCatcher(func, default=None):
   102         and return default value when it happen
   103         and return default value when it happen
   103         """
   104         """
   104         def catcher_func(*args, **kwargs):
   105         def catcher_func(*args, **kwargs):
   105             try:
   106             try:
   106                 return func(*args, **kwargs)
   107                 return func(*args, **kwargs)
   107             except Pyro.errors.ConnectionClosedError, e:
   108             except Pyro.errors.ConnectionClosedError as e:
   108                 confnodesroot.logger.write_error(_("Connection lost!\n"))
   109                 confnodesroot.logger.write_error(_("Connection lost!\n"))
   109                 confnodesroot._SetConnector(None)
   110                 confnodesroot._SetConnector(None)
   110             except Pyro.errors.ProtocolError, e:
   111             except Pyro.errors.ProtocolError as e:
   111                 confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e)
   112                 confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e)
   112             except Exception, e:
   113             except Exception as e:
   113                 # confnodesroot.logger.write_error(traceback.format_exc())
   114                 # confnodesroot.logger.write_error(traceback.format_exc())
   114                 errmess = ''.join(Pyro.util.getPyroTraceback(e))
   115                 errmess = ''.join(Pyro.util.getPyroTraceback(e))
   115                 confnodesroot.logger.write_error(errmess + "\n")
   116                 confnodesroot.logger.write_error(errmess + "\n")
   116                 print(errmess)
   117                 print(errmess)
   117                 confnodesroot._SetConnector(None)
   118                 confnodesroot._SetConnector(None)
   128     ID,secret = IDPSK
   129     ID,secret = IDPSK
   129     PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
   130     PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
   130 
   131 
   131     _special_return_funcs = {
   132     _special_return_funcs = {
   132         "StartPLC": False,
   133         "StartPLC": False,
   133         "GetTraceVariables": ("Broken", None),
   134         "GetTraceVariables": (PlcStatus.Broken, None),
   134         "GetPLCstatus": ("Broken", None),
   135         "GetPLCstatus": (PlcStatus.Broken, None),
   135         "RemoteExec": (-1, "RemoteExec script failed!")
   136         "RemoteExec": (-1, "RemoteExec script failed!")
   136     }
   137     }
   137 
   138 
   138     class PyroProxyProxy(object):
   139     class PyroProxyProxy(object):
   139         """
   140         """
   140         A proxy proxy class to handle Beremiz Pyro interface specific behavior.
   141         A proxy proxy class to handle Beremiz Pyro interface specific behavior.
   141         And to put Pyro exception catcher in between caller and Pyro proxy
   142         And to put Pyro exception catcher in between caller and Pyro proxy
   142         """
   143         """
   143 
       
   144         def __getattr__(self, attrName):
   144         def __getattr__(self, attrName):
   145             member = self.__dict__.get(attrName, None)
   145             member = self.__dict__.get(attrName, None)
   146             if member is None:
   146             if member is None:
   147                 def my_local_func(*args, **kwargs):
   147                 def my_local_func(*args, **kwargs):
   148                     return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs)
   148                     return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs)
   149                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   149                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   150                 self.__dict__[attrName] = member
   150                 self.__dict__[attrName] = member
   151             return member
   151             return member
   152 
   152 
   153     return PyroProxyProxy()
   153     return PyroProxyProxy()
   154