andrej@1731: #!/usr/bin/env python andrej@1731: # -*- coding: utf-8 -*- andrej@1731: andrej@1731: # This file is part of Beremiz, a Integrated Development Environment for andrej@1731: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1731: # andrej@1731: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1731: # andrej@1731: # See COPYING file for copyrights details. andrej@1731: # andrej@1731: # This program is free software; you can redistribute it and/or andrej@1731: # modify it under the terms of the GNU General Public License andrej@1731: # as published by the Free Software Foundation; either version 2 andrej@1731: # of the License, or (at your option) any later version. andrej@1731: # andrej@1731: # This program is distributed in the hope that it will be useful, andrej@1731: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1731: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1731: # GNU General Public License for more details. andrej@1731: # andrej@1731: # You should have received a copy of the GNU General Public License andrej@1731: # along with this program; if not, write to the Free Software andrej@1731: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1731: andrej@1826: andrej@1731: from time import sleep andrej@1731: import copy andrej@1731: import socket andrej@1783: import os.path andrej@1783: kinsamanka@3776: import Pyro5 kinsamanka@3776: import Pyro5.client kinsamanka@3776: import Pyro5.errors andrej@1832: kinsamanka@3776: # TODO: PSK kinsamanka@3776: andrej@2416: from runtime import PlcStatus kinsamanka@3750: import importlib andrej@1832: andrej@2543: kinsamanka@3776: Pyro5.config.SERPENT_BYTES_REPR = True andrej@1736: andrej@1731: def PYRO_connector_factory(uri, confnodesroot): andrej@1731: """ andrej@1731: This returns the connector to Pyro style PLCobject andrej@1731: """ andrej@1731: confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri) andrej@1731: Edouard@2338: scheme, location = uri.split("://") kinsamanka@3776: kinsamanka@3776: # TODO: use ssl kinsamanka@3776: kinsamanka@3776: schemename = "PYRO" Edouard@2312: andrej@1731: # Try to get the proxy object andrej@1731: try: kinsamanka@3776: RemotePLCObjectProxy = Pyro5.client.Proxy(f"{schemename}:PLCObject@{location}") andrej@2536: except Exception as e: edouard@2492: confnodesroot.logger.write_error( edouard@2492: _("Connection to {loc} failed with exception {ex}\n").format( andrej@2538: loc=location, ex=str(e))) andrej@1731: return None andrej@1731: kinsamanka@3776: RemotePLCObjectProxy._pyroTimeout = 60 Edouard@2468: andrej@1731: def PyroCatcher(func, default=None): andrej@1731: """ andrej@1731: A function that catch a Pyro exceptions, write error to logger andrej@1731: and return default value when it happen andrej@1731: """ andrej@1731: def catcher_func(*args, **kwargs): andrej@1731: try: andrej@1731: return func(*args, **kwargs) kinsamanka@3776: except Pyro5.errors.ConnectionClosedError as e: Edouard@2622: confnodesroot._SetConnector(None) andrej@1731: confnodesroot.logger.write_error(_("Connection lost!\n")) kinsamanka@3776: except Pyro5.errors.ProtocolError as e: andrej@1731: confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) andrej@2418: except Exception as e: edouard@3805: errmess = ''.join(Pyro5.errors.get_pyro_traceback()) andrej@1731: confnodesroot.logger.write_error(errmess + "\n") andrej@1826: print(errmess) andrej@1731: confnodesroot._SetConnector(None) andrej@1731: return default andrej@1731: return catcher_func andrej@1731: andrej@1731: # Check connection is effective. andrej@1731: # lambda is for getattr of GetPLCstatus to happen inside catcher Edouard@2324: IDPSK = PyroCatcher(RemotePLCObjectProxy.GetPLCID)() Edouard@2324: if IDPSK is None: edouard@2430: confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n")) edouard@2430: else: edouard@2492: ID, secret = IDPSK edouard@2430: PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri) andrej@1731: Edouard@1995: _special_return_funcs = { Edouard@1995: "StartPLC": False, edouard@2429: "GetTraceVariables": (PlcStatus.Broken, None), andrej@2416: "GetPLCstatus": (PlcStatus.Broken, None), Edouard@1995: "RemoteExec": (-1, "RemoteExec script failed!") Edouard@1995: } Edouard@1997: andrej@1731: class PyroProxyProxy(object): andrej@1731: """ andrej@1731: A proxy proxy class to handle Beremiz Pyro interface specific behavior. andrej@1731: And to put Pyro exception catcher in between caller and Pyro proxy andrej@1731: """ andrej@1731: def __getattr__(self, attrName): andrej@1731: member = self.__dict__.get(attrName, None) andrej@1731: if member is None: andrej@1731: def my_local_func(*args, **kwargs): andrej@1731: return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) Edouard@1995: member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None)) andrej@1731: self.__dict__[attrName] = member andrej@1731: return member andrej@1731: Edouard@2463: return PyroProxyProxy