36 import Pyro.util |
36 import Pyro.util |
37 from Pyro.errors import PyroError |
37 from Pyro.errors import PyroError |
38 |
38 |
39 import PSKManagement as PSK |
39 import PSKManagement as PSK |
40 from connectors.PYRO.PSK_Adapter import setupPSKAdapter |
40 from connectors.PYRO.PSK_Adapter import setupPSKAdapter |
41 from runtime import PlcStatus |
|
42 |
41 |
43 |
42 |
44 def switch_pyro_adapter(use_ssl): |
43 def switch_pyro_adapter(use_ssl): |
45 """ |
44 """ |
46 Reloads Pyro module with new settings. |
45 Reloads Pyro module with new settings. |
86 loc=location, ex=str(e))) |
85 loc=location, ex=str(e))) |
87 return None |
86 return None |
88 |
87 |
89 RemotePLCObjectProxy.adapter.setTimeout(60) |
88 RemotePLCObjectProxy.adapter.setTimeout(60) |
90 |
89 |
|
90 class MissingCallException(Exception): |
|
91 pass |
|
92 |
91 def PyroCatcher(func, default=None): |
93 def PyroCatcher(func, default=None): |
92 """ |
94 """ |
93 A function that catch a Pyro exceptions, write error to logger |
95 A function that catch a Pyro exceptions, write error to logger |
94 and return default value when it happen |
96 and return default value when it happen |
95 """ |
97 """ |
99 except Pyro.errors.ConnectionClosedError as e: |
101 except Pyro.errors.ConnectionClosedError as e: |
100 confnodesroot._SetConnector(None) |
102 confnodesroot._SetConnector(None) |
101 confnodesroot.logger.write_error(_("Connection lost!\n")) |
103 confnodesroot.logger.write_error(_("Connection lost!\n")) |
102 except Pyro.errors.ProtocolError as e: |
104 except Pyro.errors.ProtocolError as e: |
103 confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) |
105 confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) |
|
106 except MissingCallException as e: |
|
107 confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e.message) |
104 except Exception as e: |
108 except Exception as e: |
105 # confnodesroot.logger.write_error(traceback.format_exc()) |
109 # confnodesroot.logger.write_error(traceback.format_exc()) |
106 errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
110 errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
107 confnodesroot.logger.write_error(errmess + "\n") |
111 confnodesroot.logger.write_error(errmess + "\n") |
108 print(errmess) |
112 print(errmess) |
117 confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n")) |
121 confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n")) |
118 else: |
122 else: |
119 ID, secret = IDPSK |
123 ID, secret = IDPSK |
120 PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri) |
124 PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri) |
121 |
125 |
122 _special_return_funcs = { |
|
123 "StartPLC": False, |
|
124 "GetTraceVariables": (PlcStatus.Broken, None), |
|
125 "GetPLCstatus": (PlcStatus.Broken, None), |
|
126 "RemoteExec": (-1, "RemoteExec script failed!") |
|
127 } |
|
128 |
|
129 class PyroProxyProxy(object): |
126 class PyroProxyProxy(object): |
130 """ |
127 """ |
131 A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
128 A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
132 And to put Pyro exception catcher in between caller and Pyro proxy |
129 And to put Pyro exception catcher in between caller and Pyro proxy |
133 """ |
130 """ |
134 def __getattr__(self, attrName): |
131 def __getattr__(self, attrName): |
135 member = self.__dict__.get(attrName, None) |
132 member = self.__dict__.get(attrName, None) |
136 if member is None: |
133 if member is None: |
137 def my_local_func(*args, **kwargs): |
134 def my_local_func(*args, **kwargs): |
138 return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) |
135 call = RemotePLCObjectProxy.__getattr__(attrName) |
139 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None)) |
136 if call is None: |
|
137 raise MissingCallException(attrName) |
|
138 else: |
|
139 return call(*args, **kwargs) |
|
140 member = PyroCatcher(my_local_func, self.PLCObjDefaults.get(attrName, None)) |
140 self.__dict__[attrName] = member |
141 self.__dict__[attrName] = member |
141 return member |
142 return member |
142 |
143 |
143 return PyroProxyProxy |
144 return PyroProxyProxy |