# HG changeset patch # User dporopat # Date 1524138056 -7200 # Node ID 237c1a2de1c89bdece9bcd69d659995de625c943 # Parent 4e511f5aad196ecb687e2336ccb98bea1fbc65ae #2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore. diff -r 4e511f5aad19 -r 237c1a2de1c8 Beremiz_service.py --- a/Beremiz_service.py Wed Apr 18 10:42:33 2018 +0200 +++ b/Beremiz_service.py Thu Apr 19 13:40:56 2018 +0200 @@ -620,11 +620,11 @@ try: _wampconf = WC.LoadWampClientConf(wampconf) if _wampconf: - WC.SetServer(pyroserver) - if _wampconf.get("url", None): # TODO : test more ? - WC.RegisterWampClient(wampconf, wampsecret) + WC.SetServer(pyroserver, wampconf, wampsecret) + if _wampconf.get("url", False) and _wampconf.get("active", False): # TODO : test more ? + WC.RegisterWampClient() else: - raise Exception(_("WAMP config is incomplete.")) + raise Exception(_("WAMP config is incomplete or active is false.")) else: raise Exception(_("WAMP config is missing.")) except Exception: diff -r 4e511f5aad19 -r 237c1a2de1c8 runtime/WampClient.py --- a/runtime/WampClient.py Wed Apr 18 10:42:33 2018 +0200 +++ b/runtime/WampClient.py Thu Apr 19 13:40:56 2018 +0200 @@ -39,6 +39,8 @@ _transportFactory = None _WampSession = None _PySrv = None +_WampConf = None +_WampSecret = None ExposedCalls = [ "StartPLC", @@ -184,6 +186,31 @@ except Exception: return None +def SaveWampClientConf(wampconf, url, active): + try: + WSClientConf = LoadWampClientConf(wampconf) + change = False + if url: + oldUrl = WSClientConf.get('url', None) + if oldUrl != url: + WSClientConf['url'] = url + change = True + + oldActive = WSClientConf.get('active', False) + if oldActive != active: + WSClientConf['active'] = active + change = True + + if change: + with open(os.path.realpath(wampconf), 'w') as f: + json.dump(WSClientConf, f) + + return WSClientConf + except ValueError, ve: + print(_("WAMP load error: "), ve) + return None + except Exception: + return None def LoadWampSecret(secretfname): try: @@ -203,8 +230,11 @@ return False -def RegisterWampClient(wampconf, secretfname): - WSClientConf = LoadWampClientConf(wampconf) +def RegisterWampClient(wampconf = None, secretfname = None): + if wampconf: + WSClientConf = LoadWampClientConf(wampconf) + else: + WSClientConf = LoadWampClientConf(_WampConf) if not WSClientConf: print(_("WAMP client connection not established!")) @@ -214,7 +244,10 @@ print(_("WAMP url {} is not correct!".format(WSClientConf["url"]))) return False - WampSecret = LoadWampSecret(secretfname) + if secretfname: + WampSecret = LoadWampSecret(secretfname) + else: + WampSecret = LoadWampSecret(_WampSecret) if WampSecret is not None: WSClientConf["secret"] = WampSecret @@ -240,16 +273,32 @@ return True # conn -def GetTransportFactory(): - global _transportFactory - return _transportFactory - +def ReconnectWampClient(active, url): + SaveWampClientConf(_WampConf, url, active) + + if not active and _WampSession: + # crossbar connection active is off, retry connection off + _transportFactory.stopTrying() + return _WampSession.leave() + elif _WampSession and active: + # do reconnecting + _WampSession.disconnect() + return True + elif not _WampSession and active: + # crossbar connection active is on, do connect + RegisterWampClient() + return True + else: + return False def GetSession(): - global _WampSession return _WampSession - -def SetServer(pysrv): - global _PySrv +def StatusWampClient(): + return _WampSession and _WampSession.is_attached() + +def SetServer(pysrv, wampconf = None, wampsecret = None): + global _PySrv, _WampConf, _WampSecret _PySrv = pysrv + _WampConf = wampconf + _WampSecret = wampsecret