Commit 895c3fce authored by Štěpán Henek's avatar Štěpán Henek 🌩

option to use mqtt as message bus added

parent 5d2b6ccd
......@@ -34,10 +34,17 @@ def get_arg_parser():
help="sets which app should be started (config/...)",
)
group.add_argument(
"-b", "--backend", choices=["ubus", "unix-socket"], default="ubus", help="backend type"
"-b", "--message-bus", choices=["ubus", "unix-socket", "mqtt"], default="ubus",
help="message bus type"
)
group.add_argument(
"--backend-socket", default="/var/run/ubus.sock", help="backend socket path"
"--mqtt-port", default=1883, help="mqtt port (default 1883)"
)
group.add_argument(
"--mqtt-host", default="localhost", help="mqtt host (default 'localhost')"
)
group.add_argument(
"--bus-socket", default="/var/run/ubus.sock", help="message bus socket path"
)
group.add_argument(
"--ws-port", default=0, help="websocket server port - insecure (0=autodetect)", type=int
......@@ -69,7 +76,12 @@ def main():
logger.debug("Version %s" % __version__)
# set backend
current_state.set_backend(Backend(args.backend, args.backend_socket))
if args.message_bus in ["ubus", "unix-socket"]:
current_state.set_backend(Backend(args.message_bus, path=args.bus_socket))
elif args.message_bus == "mqtt":
current_state.set_backend(
Backend(args.message_bus, host=args.mqtt_host, port=args.mqtt_port))
# update websocket
current_state.set_websocket(args.ws_port, args.ws_path, args.wss_port, args.wss_path)
# set assets path
......
......@@ -32,18 +32,25 @@ class ExceptionInBackend(Exception):
class Backend(object):
DEFAULT_TIMEOUT = 30000 # in ms
def __init__(self, name, path):
def __init__(self, name, **kwargs):
self.name = name
self.path = path
if name == "ubus":
from foris_client.buses.ubus import UbusSender
self._instance = UbusSender(path, default_timeout=self.DEFAULT_TIMEOUT)
self.path = kwargs["path"]
self._instance = UbusSender(kwargs["path"], default_timeout=self.DEFAULT_TIMEOUT)
elif name == "unix-socket":
from foris_client.buses.unix_socket import UnixSocketSender
self.path = kwargs["path"]
self._instance = UnixSocketSender(
path, default_timeout=self.DEFAULT_TIMEOUT)
kwargs["path"], default_timeout=self.DEFAULT_TIMEOUT)
elif name == "mqtt":
from foris_client.buses.mqtt import MqttSender
self.host = kwargs["host"]
self.port = kwargs["port"]
self._instance = MqttSender(kwargs["host"], kwargs["port"])
def __repr__(self):
return "%s('%s')" % (type(self._instance).__name__, self.path)
......
......@@ -41,7 +41,14 @@ class ForisState(object):
self.app = app
def set_backend(self, backend):
logger.debug("setting backend to '%s' (path %s)." % (backend.name, backend.path))
if backend.name in ["ubus", "unix-socket"]:
logger.debug("setting backend to '%s' (path %s)." % (backend.name, backend.path))
elif backend.name == "mqtt":
logger.debug(
"setting backend to '%s' (host %s:%d)." % (
backend.name, backend.host, backend.port
)
)
self.backend = backend
def set_websocket(self, ws_port, ws_path, wss_port, wss_path):
......
......@@ -78,6 +78,7 @@ setup(
'pbkdf2',
'flup',
"ubus @ git+https://gitlab.labs.nic.cz/turris/python-ubus.git",
"paho-mqtt",
],
setup_requires=[
'babel',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment