connectors/PYRO/__init__.py
changeset 2324 1cf3768ebf85
parent 2314 e927c101ce6d
child 2334 d1470c052662
equal deleted inserted replaced
2323:33a0dbabccd3 2324:1cf3768ebf85
    34 import Pyro
    34 import Pyro
    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 
    39 zeroconf_service_type = '_PYRO._tcp.local.'
    40 zeroconf_service_type = '_PYRO._tcp.local.'
    40 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    41 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    41 # supported by this connector confnode.
    42 # supported by this connector confnode.
    42 #
    43 #
    43 # for connectors that do not support DNS-SD, this attribute can be omitted
    44 # for connectors that do not support DNS-SD, this attribute can be omitted
    52 
    53 
    53     servicetype, location = uri.split("://")
    54     servicetype, location = uri.split("://")
    54     if servicetype == "PYROS":
    55     if servicetype == "PYROS":
    55         import connectors.PYRO.PSK_Adapter
    56         import connectors.PYRO.PSK_Adapter
    56         schemename = "PYROLOCPSK"
    57         schemename = "PYROLOCPSK"
    57         url, ID = location.split('#')
    58         url, ID = location.split('#') #TODO fix exception when # not found
    58         # load PSK from project
    59         # load PSK from project
    59         secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID+'.secret')
    60         secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID+'.secret')
    60         if not os.path.exists(secpath):
    61         if not os.path.exists(secpath):
    61             confnodesroot.logger.write_error(
    62             confnodesroot.logger.write_error(
    62                 'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath)
    63                 'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath)
    63             return None
    64             return None
    64         Pyro.config.PYROPSK = (open(secpath).read(), ID)
    65         secret = open(secpath).read().partition(':')[2].rstrip('\n\r')
       
    66         Pyro.config.PYROPSK = (secret, ID)
    65         # strip ID from URL, so that pyro can understand it.
    67         # strip ID from URL, so that pyro can understand it.
    66         location = url
    68         location = url
    67     else:
    69     else:
    68         schemename = "PYROLOC"
    70         schemename = "PYROLOC"
    69 
    71 
    86             return None
    88             return None
    87 
    89 
    88     # Try to get the proxy object
    90     # Try to get the proxy object
    89     try:
    91     try:
    90         RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject")
    92         RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject")
    91     except Exception:
    93     except Exception,e:
    92         confnodesroot.logger.write_error(_("Connection to '%s' failed.\n") % location)
    94         confnodesroot.logger.write_error(_("Connection to '%s' failed with exception '%s'\n") % (location, str(e)))
    93         confnodesroot.logger.write_error(traceback.format_exc())
    95         #confnodesroot.logger.write_error(traceback.format_exc())
    94         return None
    96         return None
    95 
    97 
    96     def PyroCatcher(func, default=None):
    98     def PyroCatcher(func, default=None):
    97         """
    99         """
    98         A function that catch a Pyro exceptions, write error to logger
   100         A function that catch a Pyro exceptions, write error to logger
   115             return default
   117             return default
   116         return catcher_func
   118         return catcher_func
   117 
   119 
   118     # Check connection is effective.
   120     # Check connection is effective.
   119     # lambda is for getattr of GetPLCstatus to happen inside catcher
   121     # lambda is for getattr of GetPLCstatus to happen inside catcher
   120     if PyroCatcher(RemotePLCObjectProxy.GetPLCstatus)() is None:
   122     IDPSK = PyroCatcher(RemotePLCObjectProxy.GetPLCID)()
   121         confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n"))
   123     if IDPSK is None:
       
   124         confnodesroot.logger.write_error(_("Cannot get PLC ID - connection failed.\n"))
   122         return None
   125         return None
       
   126 
       
   127     if servicetype != "PYROS":
       
   128         ID,PSK = IDPSK
       
   129         secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID+'.secret')
       
   130         with open(secpath, 'w') as f:
       
   131             f.write(ID+":"+PSK)
   123 
   132 
   124     _special_return_funcs = {
   133     _special_return_funcs = {
   125         "StartPLC": False,
   134         "StartPLC": False,
   126         "GetTraceVariables": ("Broken", None),
   135         "GetTraceVariables": ("Broken", None),
   127         "GetPLCstatus": ("Broken", None),
   136         "GetPLCstatus": ("Broken", None),