# HG changeset patch # User dporopat # Date 1526280344 -7200 # Node ID 25dafeb98b558998d14824d87c737b125d2467b3 # Parent c8a32ad27c0a7f01a5de39751183356fec60fd54 #2486 Reconnect funtion in SaveWampClientConf function. diff -r c8a32ad27c0a -r 25dafeb98b55 Beremiz_service.py --- a/Beremiz_service.py Wed May 09 09:59:53 2018 +0200 +++ b/Beremiz_service.py Mon May 14 08:45:44 2018 +0200 @@ -618,9 +618,9 @@ if wampconf is not None: try: - _wampconf = WC.LoadWampClientConf(wampconf) + WC.SetServer(pyroserver, wampconf, wampsecret) + _wampconf = WC.LoadWampClientConf() if _wampconf: - WC.SetServer(pyroserver, wampconf, wampsecret) if _wampconf.get("url", False) and _wampconf.get("active", False): # TODO : test more ? WC.RegisterWampClient() else: diff -r c8a32ad27c0a -r 25dafeb98b55 runtime/WampClient.py --- a/runtime/WampClient.py Wed May 09 09:59:53 2018 +0200 +++ b/runtime/WampClient.py Mon May 14 08:45:44 2018 +0200 @@ -177,34 +177,43 @@ del connector -def LoadWampClientConf(wampconf): +def LoadWampClientConf(items=None): try: - WSClientConf = json.load(open(wampconf)) + WSClientConf = json.load(open(_WampConf)) + if items and isinstance(items, list): + WSClientConfItems = {} + for item in items: + wampconf_value = WSClientConf.get(item, None) + if wampconf_value is not None: + WSClientConfItems[item] = wampconf_value + if WSClientConfItems: + return WSClientConfItems return WSClientConf except ValueError, ve: print(_("WAMP load error: "), ve) return None - except Exception: - return None - -def SaveWampClientConf(wampconf, url, active): + except Exception, e: + print(_("WAMP load error: "), e) + return None + +def SaveWampClientConf(items): 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) + WSClientConf = LoadWampClientConf() + saveChanges = False + if items: + for itemKey in items.keys(): + wampconf_value = WSClientConf.get(itemKey, None) + if (wampconf_value is not None) and (items[itemKey] is not None) and (wampconf_value != items[itemKey]): + WSClientConf[itemKey] = items[itemKey] + saveChanges = True + + if saveChanges: + with open(os.path.realpath(_WampConf), 'w') as f: + json.dump(WSClientConf, f, sort_keys=True, indent=4) + if 'active' in WSClientConf and WSClientConf['active']: + StartReconnectWampClient() + else: + StopReconnectWampClient() return WSClientConf except ValueError, ve: @@ -232,11 +241,13 @@ return False -def RegisterWampClient(wampconf = None, secretfname = None): +def RegisterWampClient(wampconf=None, secretfname=None): + global _WampConf if wampconf: - WSClientConf = LoadWampClientConf(wampconf) - else: - WSClientConf = LoadWampClientConf(_WampConf) + _WampConf = wampconf + WSClientConf = LoadWampClientConf() + else: + WSClientConf = LoadWampClientConf() if not WSClientConf: print(_("WAMP client connection not established!")) @@ -272,43 +283,34 @@ # start the client from a Twisted endpoint conn = connectWS(transport_factory) print(_("WAMP client connecting to :"), WSClientConf["url"]) - return True # conn + return True + def StopReconnectWampClient(): _transportFactory.stopTrying() return _WampSession.leave() + def StartReconnectWampClient(): if _WampSession: # do reconnect _WampSession.disconnect() return True - elif not _WampSession: + else: # do connect RegisterWampClient() return True -def ReconnectionWampClient(active, url): - """ReconnectionWampClient function used for reconnecting to Crossbar router. - - Args: - active (bool): Value in wampconf.json file. True: using Wamp connection. False: not using Wamp connection. - url (str): Value in wampconf.json file. Url of Crossbar router. - """ - SaveWampClientConf(_WampConf, url, active) - - if active: - StartReconnectWampClient() - elif not active and _WampSession: - StopReconnectWampClient() def GetSession(): return _WampSession + def StatusWampClient(): return _WampSession and _WampSession.is_attached() -def SetServer(pysrv, wampconf = None, wampsecret = None): + +def SetServer(pysrv, wampconf=None, wampsecret=None): global _PySrv, _WampConf, _WampSecret _PySrv = pysrv _WampConf = wampconf diff -r c8a32ad27c0a -r 25dafeb98b55 tests/wamp/project_files/wampconf.json --- a/tests/wamp/project_files/wampconf.json Wed May 09 09:59:53 2018 +0200 +++ b/tests/wamp/project_files/wampconf.json Mon May 14 08:45:44 2018 +0200 @@ -1,10 +1,17 @@ { - "active": true, - "url":"ws://127.0.0.1:8888", - "realm":"Automation", - "ID":"wamptest", - "password":"1234567890", - "key":"ABCDEFGHIJ", - "protocolOptions":{"autoPingTimeout":20, "autoPingInterval":60}, - "registerOptions":{"match":"exact", "invoke":"last", "concurrency": 2} + "ID": "wamptest", + "active": true, + "key": "ABCDEFGHIJ", + "password": "1234567890", + "protocolOptions": { + "autoPingInterval": 60, + "autoPingTimeout": 20 + }, + "realm": "Automation", + "registerOptions": { + "concurrency": 2, + "invoke": "last", + "match": "exact" + }, + "url": "ws://127.0.0.1:8888" }