#2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore. #2486
authordporopat <denis.poropat@smarteh.si>
Thu, 19 Apr 2018 13:40:56 +0200
branch#2486
changeset 2202 237c1a2de1c8
parent 2201 4e511f5aad19
child 2203 c8a32ad27c0a
#2486 Adding reconnect funtion to WampClient. Global TransportFactory not needed anymore.
Beremiz_service.py
runtime/WampClient.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:
--- 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