--- a/Beremiz_service.py Wed Jul 04 14:10:03 2018 +0200
+++ b/Beremiz_service.py Wed Jul 04 14:17:00 2018 +0200
@@ -623,14 +623,13 @@
- _wampconf = WC.LoadWampClientConf(wampconf)
+ WC.SetServer(pyroserver, wampconf, wampsecret) + _wampconf = WC.GetConfiguration() - if _wampconf["url"]: # TODO : test more ?
- WC.RegisterWampClient(wampconf, wampsecret)
- pyruntimevars["wampsession"] = WC.GetSession
- WC.SetServer(pyroserver)
+ if _wampconf.get("url", False) and _wampconf.get("active", False): # TODO : test more ? + WC.RegisterWampClient() - raise Exception(_("WAMP config is incomplete."))
+ raise Exception(_("WAMP config is incomplete or active is false.")) raise Exception(_("WAMP config is missing."))
--- a/runtime/WampClient.py Wed Jul 04 14:10:03 2018 +0200
+++ b/runtime/WampClient.py Wed Jul 04 14:17:00 2018 +0200
@@ -26,6 +26,8 @@
from __future__ import print_function
from autobahn.twisted import wamp
from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
from autobahn.wamp import types, auth
@@ -34,21 +36,24 @@
from twisted.internet.protocol import ReconnectingClientFactory
+_transportFactory = None
- "SetTraceVariablesList",
+ ("SetTraceVariablesList", {}), + ("GetTraceVariables", {}), # Those two lists are meant to be filled by customized runtime
@@ -69,11 +74,20 @@
obj = getattr(obj, names.pop(0))
+def getValidOptins(options, arguments): + validOptions[key] = options[key] + if len(validOptions) > 0: class WampSession(wamp.ApplicationSession):
if "secret" in self.config.extra:
- user = self.config.extra["ID"].encode('utf8')
+ user = self.config.extra["ID"] self.join(u"Automation", [u"wampcra"], user)
@@ -91,10 +105,15 @@
ID = self.config.extra["ID"]
- print('WAMP session joined by :', ID)
- for name in ExposedCalls:
- regoption = types.RegisterOptions(u'exact', u'last')
- yield self.register(GetCallee(name), u'.'.join((ID, name)), regoption)
+ for name, kwargs in ExposedCalls: + registerOptions = types.RegisterOptions(**kwargs) + print(_("TypeError register option: {}".format(e))) + yield self.register(GetCallee(name), u'.'.join((ID, name)), registerOptions) for name in SubscribedEvents:
yield self.subscribe(GetCallee(name), unicode(name))
@@ -102,32 +121,96 @@
+ print(_('WAMP session joined (%s) by:' % time.ctime()), ID) def onLeave(self, details):
+ global _WampSession, _transportFactory + super(WampSession, self).onLeave(details) + _transportFactory = None print(_('WAMP session left'))
class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
+ def __init__(self, config, *args, **kwargs): + global _transportFactory + WampWebSocketClientFactory.__init__(self, *args, **kwargs) + protocolOptions = config.extra.get('protocolOptions', None) + self.setProtocolOptions(**protocolOptions) + _transportFactory = self + print(_("Custom protocol options failed :"), e) + _transportFactory = None + def buildProtocol(self, addr): + return ReconnectingClientFactory.buildProtocol(self, addr) def clientConnectionFailed(self, connector, reason):
- print(_("WAMP Client connection failed (%s) .. retrying .." % time.ctime()))
- ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
+ if self.continueTrying: + print(_("WAMP Client connection failed (%s) .. retrying .." % time.ctime())) + super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason) def clientConnectionLost(self, connector, reason):
- print(_("WAMP Client connection lost (%s) .. retrying .." % time.ctime()))
- ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
+ if self.continueTrying: + print(_("WAMP Client connection lost (%s) .. retrying .." % time.ctime())) + super(ReconnectingWampWebSocketClientFactory, self).clientConnectionFailed(connector, reason) -def LoadWampClientConf(wampconf):
+def GetConfiguration(items=None): - WSClientConf = json.load(open(wampconf))
+ WSClientConf = json.load(open(_WampConf)) + if items and isinstance(items, list): + wampconf_value = WSClientConf.get(item, None) + if wampconf_value is not None: + WSClientConfItems[item] = wampconf_value + return WSClientConfItems print(_("WAMP load error: "), ve)
+ print(_("WAMP load error: "), e) +def SetConfiguration(items): + WSClientConf = json.load(open(_WampConf)) + 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] + 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']: + if _transportFactory and _WampSession: + StopReconnectWampClient() + StartReconnectWampClient() + StopReconnectWampClient() + print(_("WAMP save error: "), ve) + print(_("WAMP save error: "), e) def LoadWampSecret(secretfname):
@@ -140,15 +223,33 @@
-def RegisterWampClient(wampconf, secretfname):
+ if re.match(r'w{1}s{1,2}:{1}/{2}.+:{1}[0-9]+/{1}.+', uri): - WSClientConf = LoadWampClientConf(wampconf)
+def RegisterWampClient(wampconf=None, secretfname=None): + WSClientConf = GetConfiguration() + WSClientConf = GetConfiguration() print(_("WAMP client connection not established!"))
- WampSecret = LoadWampSecret(secretfname)
+ if not IsCorrectUri(WSClientConf["url"]): + print(_("WAMP url {} is not correct!".format(WSClientConf["url"]))) + WampSecret = LoadWampSecret(secretfname) + WampSecret = LoadWampSecret(_WampSecret) if WampSecret is not None:
WSClientConf["secret"] = WampSecret
@@ -162,21 +263,48 @@
session_factory.session = WampSession
# create a WAMP-over-WebSocket transport client factory
- transport_factory = ReconnectingWampWebSocketClientFactory(
+ ReconnectingWampWebSocketClientFactory( serializers=[MsgPackSerializer()])
# start the client from a Twisted endpoint
- conn = connectWS(transport_factory)
- print(_("WAMP client connecting to :"), WSClientConf["url"])
+ conn = connectWS(_transportFactory) + print(_("WAMP client connecting to :"), WSClientConf["url"]) + print(_("WAMP client can not connect to :"), WSClientConf["url"]) +def StopReconnectWampClient(): + _transportFactory.stopTrying() + return _WampSession.leave() +def StartReconnectWampClient(): + _WampSession.disconnect()
+ return _WampSession and _WampSession.is_attached() +def SetServer(pysrv, wampconf=None, wampsecret=None): + global _PySrv, _WampConf, _WampSecret + _WampSecret = wampsecret --- a/tests/wamp/.crossbar/config.json Wed Jul 04 14:10:03 2018 +0200
+++ b/tests/wamp/.crossbar/config.json Wed Jul 04 14:17:00 2018 +0200
@@ -39,13 +39,15 @@
"url": "ws://127.0.0.1:8888/",
--- a/tests/wamp/README Wed Jul 04 14:10:03 2018 +0200
+++ b/tests/wamp/README Wed Jul 04 14:17:00 2018 +0200
@@ -1,25 +1,26 @@
-Crossbar test router configuration is available in .crossbar directory.
-This project contains wamp client config to be loaded at runtime startup.
-project_files/wampconf.json
+/* This project contains wamp client config to be loaded at runtime startup. */ +./project_files/wampconf.json wampconf.json is in "Project Files", so it is copied to runtime's working directory, and then loaded after program transfer + runtime restart.
Otherwise, wamp config file path can be forced :
./Beremiz_service.py -c /path/to/my/wampconf.json /working/dir
-Otherwise, path for CRA secret can be forced :
-./Beremiz_service.py -s /path/to/my/secret /working/dir
+#sudo apt-get -y dist-upgrade +sudo apt-get -y install build-essential libssl-dev libffi-dev libreadline-dev libbz2-dev libsqlite3-dev libncurses5-dev +sudo python -m pip install -U pip +sudo pip install crossbar +/* Start Crossbar command: */ +/* Crossbar test router configuration is available in .crossbar directory. */ - Crossbar.io : 17.12.1 (Crossbar.io COMMUNITY)
- Autobahn : 17.10.1 (with JSON, MessagePack, CBOR, UBJSON)
+ Crossbar.io : 18.3.1 (Crossbar.io COMMUNITY) + Autobahn : 18.3.1 (with JSON, MessagePack, CBOR, UBJSON) Twisted : 17.9.0-EPollReactor
- Python : 2.7.12/CPython
+ Python : 2.7.12/CPython \ No newline at end of file
--- a/tests/wamp/beremiz.xml Wed Jul 04 14:10:03 2018 +0200
+++ b/tests/wamp/beremiz.xml Wed Jul 04 14:17:00 2018 +0200
@@ -1,4 +1,4 @@
<?xml version='1.0' encoding='utf-8'?>
-<BeremizRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" URI_location="WAMP://127.0.0.1:8888#Automation#wamptest">
+<BeremizRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" URI_location="WAMP://127.0.0.1:8888#Automation#WampID"> --- a/tests/wamp/project_files/wampconf.json Wed Jul 04 14:10:03 2018 +0200
+++ b/tests/wamp/project_files/wampconf.json Wed Jul 04 14:17:00 2018 +0200
@@ -1,7 +1,12 @@
- "url":"ws://127.0.0.1:8888",
- "password":"1234567890",
+ "password": "1234567890", + "autoPingInterval": 60, + "url": "ws://127.0.0.1:8888"