connectors/LPC/__init__.py
changeset 448 8ef035de86de
child 502 5343ae43f6d0
equal deleted inserted replaced
447:af3399aca7b7 448:8ef035de86de
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 #
       
     4 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     5 #
       
     6 #See COPYING file for copyrights details.
       
     7 #
       
     8 #This library is free software; you can redistribute it and/or
       
     9 #modify it under the terms of the GNU General Public
       
    10 #License as published by the Free Software Foundation; either
       
    11 #version 2.1 of the License, or (at your option) any later version.
       
    12 #
       
    13 #This library is distributed in the hope that it will be useful,
       
    14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16 #General Public License for more details.
       
    17 #
       
    18 #You should have received a copy of the GNU General Public
       
    19 #License along with this library; if not, write to the Free Software
       
    20 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    21 import traceback
       
    22 
       
    23 
       
    24 def LPC_connector_factory(uri, pluginsroot):
       
    25     """
       
    26     This returns the connector to LPC style PLCobject
       
    27     """
       
    28     pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri)
       
    29 
       
    30     servicetype, location = uri.split("://")
       
    31     
       
    32     # Try to get the proxy object
       
    33     try :
       
    34         # TODO: Open Serial Port
       
    35         RemotePLCObjectProxy = LPCObject(pluginsroot) # LPC_PLCObject_Proxy
       
    36     except Exception, msg:
       
    37         pluginsroot.logger.write_error(_("Couldn't connect !\n"))
       
    38         pluginsroot.logger.write_error(traceback.format_exc())
       
    39         return None
       
    40 
       
    41     def LPCCatcher(func, default=None):
       
    42         """
       
    43         A function that catch a pyserial exceptions, write error to logger
       
    44         and return defaul value when it happen
       
    45         """
       
    46         def catcher_func(*args,**kwargs):
       
    47             try:
       
    48                 return func(*args,**kwargs)
       
    49             except Exception,e:
       
    50                 #pluginsroot.logger.write_error(traceback.format_exc())
       
    51                 pluginsroot.logger.write_error(str(e)+"\n")
       
    52                 pluginsroot._connector = None
       
    53                 return default
       
    54         return catcher_func
       
    55 
       
    56     # Check connection is effective. 
       
    57     # lambda is for getattr of GetPLCstatus to happen inside catcher
       
    58     if LPCCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() == None:
       
    59         pluginsroot.logger.write_error(_("Cannot get PLC status - connection failed.\n"))
       
    60         return None
       
    61 
       
    62 
       
    63     class LPCProxy:
       
    64         """
       
    65         A Serial proxy class to handle Beremiz Pyro interface specific behavior.
       
    66         And to put LPC exception catcher in between caller and pyro proxy
       
    67         """
       
    68         def _LPCGetTraceVariables(self):
       
    69             return self.RemotePLCObjectProxy.GetTraceVariables()
       
    70         GetTraceVariables = LPCCatcher(_LPCGetTraceVariables,("Broken",None,None))
       
    71 
       
    72         def _LPCGetPLCstatus(self):
       
    73             return RemotePLCObjectProxy.GetPLCstatus()
       
    74         GetPLCstatus = LPCCatcher(_LPCGetPLCstatus, "Broken")
       
    75         
       
    76         def __getattr__(self, attrName):
       
    77             member = self.__dict__.get(attrName, None)
       
    78             if member is None:
       
    79                 def my_local_func(*args,**kwargs):
       
    80                     return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs)
       
    81                 member = LPCCatcher(my_local_func, None)
       
    82                 self.__dict__[attrName] = member
       
    83             return member
       
    84 
       
    85     return LPCProxy()
       
    86     
       
    87