17 # |
17 # |
18 #You should have received a copy of the GNU General Public |
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 |
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 |
20 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 import traceback |
21 import traceback |
|
22 import LPCObject |
22 |
23 |
23 |
24 |
24 def LPC_connector_factory(uri, pluginsroot): |
25 def LPC_connector_factory(uri, pluginsroot): |
25 """ |
26 """ |
26 This returns the connector to LPC style PLCobject |
27 This returns the connector to LPC style PLCobject |
27 """ |
28 """ |
28 pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri) |
29 pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri) |
29 |
30 return LPCObject() |
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 |
31 |
87 |
32 |