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