ed@448: #!/usr/bin/env python ed@448: # -*- coding: utf-8 -*- ed@448: # ed@448: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD ed@448: # ed@448: #See COPYING file for copyrights details. ed@448: # ed@448: #This library is free software; you can redistribute it and/or ed@448: #modify it under the terms of the GNU General Public ed@448: #License as published by the Free Software Foundation; either ed@448: #version 2.1 of the License, or (at your option) any later version. ed@448: # ed@448: #This library is distributed in the hope that it will be useful, ed@448: #but WITHOUT ANY WARRANTY; without even the implied warranty of ed@448: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ed@448: #General Public License for more details. ed@448: # ed@448: #You should have received a copy of the GNU General Public ed@448: #License along with this library; if not, write to the Free Software ed@448: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ed@448: import traceback ed@448: ed@448: ed@448: def LPC_connector_factory(uri, pluginsroot): ed@448: """ ed@448: This returns the connector to LPC style PLCobject ed@448: """ ed@448: pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri) ed@448: ed@448: servicetype, location = uri.split("://") ed@448: ed@448: # Try to get the proxy object ed@448: try : ed@448: # TODO: Open Serial Port ed@448: RemotePLCObjectProxy = LPCObject(pluginsroot) # LPC_PLCObject_Proxy ed@448: except Exception, msg: ed@448: pluginsroot.logger.write_error(_("Couldn't connect !\n")) ed@448: pluginsroot.logger.write_error(traceback.format_exc()) ed@448: return None ed@448: ed@448: def LPCCatcher(func, default=None): ed@448: """ ed@448: A function that catch a pyserial exceptions, write error to logger ed@448: and return defaul value when it happen ed@448: """ ed@448: def catcher_func(*args,**kwargs): ed@448: try: ed@448: return func(*args,**kwargs) ed@448: except Exception,e: ed@448: #pluginsroot.logger.write_error(traceback.format_exc()) ed@448: pluginsroot.logger.write_error(str(e)+"\n") ed@448: pluginsroot._connector = None ed@448: return default ed@448: return catcher_func ed@448: ed@448: # Check connection is effective. ed@448: # lambda is for getattr of GetPLCstatus to happen inside catcher ed@448: if LPCCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() == None: ed@448: pluginsroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) ed@448: return None ed@448: ed@448: ed@448: class LPCProxy: ed@448: """ ed@448: A Serial proxy class to handle Beremiz Pyro interface specific behavior. ed@448: And to put LPC exception catcher in between caller and pyro proxy ed@448: """ ed@448: def _LPCGetTraceVariables(self): ed@448: return self.RemotePLCObjectProxy.GetTraceVariables() ed@448: GetTraceVariables = LPCCatcher(_LPCGetTraceVariables,("Broken",None,None)) ed@448: ed@448: def _LPCGetPLCstatus(self): ed@448: return RemotePLCObjectProxy.GetPLCstatus() ed@448: GetPLCstatus = LPCCatcher(_LPCGetPLCstatus, "Broken") ed@448: ed@448: def __getattr__(self, attrName): ed@448: member = self.__dict__.get(attrName, None) ed@448: if member is None: ed@448: def my_local_func(*args,**kwargs): ed@448: return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs) ed@448: member = LPCCatcher(my_local_func, None) ed@448: self.__dict__[attrName] = member ed@448: return member ed@448: ed@448: return LPCProxy() ed@448: ed@448: