author | etisserant |
Tue, 23 Dec 2008 23:56:29 +0100 | |
changeset 281 | 3f0fc8de99b0 |
parent 235 | a66e150f2888 |
child 284 | 3fecc96090c8 |
permissions | -rwxr-xr-x |
203 | 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 Pyro.core as pyro |
|
22 |
from Pyro.errors import PyroError |
|
23 |
import traceback |
|
24 |
from time import sleep |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
25 |
import copy |
203 | 26 |
|
27 |
def PYRO_connector_factory(uri, pluginsroot): |
|
28 |
""" |
|
29 |
This returns the connector to Pyro style PLCobject |
|
30 |
""" |
|
31 |
pluginsroot.logger.write("Connecting to URI : %s\n"%uri) |
|
32 |
||
33 |
servicetype, location = uri.split("://") |
|
34 |
||
35 |
# Try to get the proxy object |
|
36 |
try : |
|
37 |
RemotePLCObjectProxy = pyro.getAttrProxyForURI("PYROLOC://"+location+"/PLCObject") |
|
38 |
except Exception, msg: |
|
39 |
pluginsroot.logger.write_error("Wrong URI, please check it !\n") |
|
40 |
pluginsroot.logger.write_error(traceback.format_exc()) |
|
41 |
return None |
|
42 |
||
43 |
def PyroCatcher(func, default=None): |
|
44 |
""" |
|
45 |
A function that catch a pyro exceptions, write error to logger |
|
46 |
and return defaul value when it happen |
|
47 |
""" |
|
218 | 48 |
def catcher_func(*args,**kwargs): |
203 | 49 |
try: |
50 |
return func(*args,**kwargs) |
|
51 |
except PyroError,e: |
|
52 |
#pluginsroot.logger.write_error(traceback.format_exc()) |
|
53 |
pluginsroot.logger.write_error(str(e)) |
|
54 |
pluginsroot._Disconnect() |
|
55 |
return default |
|
218 | 56 |
return catcher_func |
203 | 57 |
|
58 |
# Check connection is effective. |
|
59 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
|
60 |
if PyroCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() == None: |
|
61 |
pluginsroot.logger.write_error("Cannot get PLC status - connection failed.\n") |
|
62 |
return None |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
63 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
64 |
# for safe use in from debug thread, must create a copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
65 |
RemotePLCObjectProxyCopy = copy.copy(RemotePLCObjectProxy) |
203 | 66 |
|
67 |
class PyroProxyProxy: |
|
68 |
""" |
|
69 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
|
70 |
And to put pyro exception catcher in between caller and pyro proxy |
|
71 |
""" |
|
72 |
def GetPyroProxy(self): |
|
73 |
""" |
|
74 |
This func returns the real Pyro Proxy. |
|
75 |
Use this if you musn't keep reference to it. |
|
76 |
""" |
|
77 |
return RemotePLCObjectProxy |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
78 |
|
235 | 79 |
def _PyroStartPLC(self, *args, **kwargs): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
80 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
81 |
pluginsroot._connector.GetPyroProxy() is used |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
82 |
rather than RemotePLCObjectProxy because |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
83 |
object is recreated meanwhile, |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
84 |
so we must not keep ref to it here |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
85 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
86 |
if pluginsroot._connector.GetPyroProxy().GetPLCstatus() == "Dirty": |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
87 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
88 |
Some bad libs with static symbols may polute PLC |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
89 |
ask runtime to suicide and come back again |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
90 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
91 |
pluginsroot.logger.write("Force runtime reload\n") |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
92 |
pluginsroot._connector.GetPyroProxy().ForceReload() |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
93 |
pluginsroot._Disconnect() |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
94 |
# let remote PLC time to resurect.(freeze app) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
95 |
sleep(0.5) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
96 |
pluginsroot._Connect() |
235 | 97 |
return pluginsroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
98 |
StartPLC = PyroCatcher(_PyroStartPLC, False) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
99 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
100 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
101 |
def _PyroGetTraceVariables(self): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
102 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
103 |
for safe use in from debug thread, must use the copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
104 |
""" |
235 | 105 |
if RemotePLCObjectProxyCopy.GetPLCstatus() == "Started": |
106 |
return RemotePLCObjectProxyCopy.GetTraceVariables() |
|
107 |
else: |
|
108 |
return None,None |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
109 |
GetTraceVariables = PyroCatcher(_PyroGetTraceVariables) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
110 |
|
203 | 111 |
|
112 |
def __getattr__(self, attrName): |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
113 |
member = self.__dict__.get(attrName, None) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
114 |
if member is None: |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
115 |
def my_local_func(*args,**kwargs): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
116 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
117 |
member = PyroCatcher(my_local_func, None) |
203 | 118 |
self.__dict__[attrName] = member |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
119 |
return member |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
120 |
|
203 | 121 |
return PyroProxyProxy() |
122 |
||
123 |