From 23a91325eb76ab63afecc1b99ddeffa92af59305 Mon Sep 17 00:00:00 2001 From: Ellis Percival <flyte@failcode.co.uk> Date: Mon, 17 Jul 2017 13:08:54 +0100 Subject: [PATCH] Add MQTT client logging handler. --- pi_mqtt_gpio/server.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/pi_mqtt_gpio/server.py b/pi_mqtt_gpio/server.py index c53a470..c28eef8 100644 --- a/pi_mqtt_gpio/server.py +++ b/pi_mqtt_gpio/server.py @@ -2,6 +2,7 @@ import argparse import logging import yaml import sys +import socket from time import sleep from importlib import import_module @@ -11,6 +12,13 @@ import cerberus from pi_mqtt_gpio.modules import PinPullup, PinDirection, BASE_SCHEMA +LOG_LEVEL_MAP = { + mqtt.MQTT_LOG_INFO: logging.INFO, + mqtt.MQTT_LOG_NOTICE: logging.INFO, + mqtt.MQTT_LOG_WARNING: logging.WARNING, + mqtt.MQTT_LOG_ERR: logging.ERROR, + mqtt.MQTT_LOG_DEBUG: logging.DEBUG +} RECONNECT_DELAY_SECS = 5 GPIO_MODULES = {} LAST_STATES = {} @@ -80,6 +88,19 @@ def on_disconnect(client, userdata, rc): rc = client.reconnect() +def on_log(client, userdata, level, buf): + """ + Called when MQTT client wishes to log something. + :param client: MQTT client instance + :param userdata: Any user data set in the client + :param level: MQTT log level + :param buf: The log message buffer + :return: None + :rtype: NoneType + """ + _LOG.log(LOG_LEVEL_MAP[level], "MQTT client: %s" % buf) + + def install_missing_requirements(module): """ Some of the modules require external packages to be installed. This gets @@ -178,7 +199,8 @@ def init_mqtt(config, digital_outputs): "Invalid client identifier used to connect to MQTT broker.") sys.exit(1) elif rc == 3: - _LOG.warning("MQTT broker unavailable. Retrying...") + _LOG.warning("MQTT broker unavailable. Retrying in %s secs...") + sleep(RECONNECT_DELAY_SECS) client.reconnect() elif rc == 4: _LOG.fatal( @@ -231,6 +253,7 @@ def init_mqtt(config, digital_outputs): client.on_disconnect = on_disconnect client.on_connect = on_conn client.on_message = on_msg + client.on_log = on_log return client @@ -330,7 +353,11 @@ if __name__ == "__main__": for out_conf in digital_outputs: initialise_digital_output(out_conf, GPIO_MODULES[out_conf["module"]]) - client.connect(config["mqtt"]["host"], config["mqtt"]["port"], 60) + try: + client.connect(config["mqtt"]["host"], config["mqtt"]["port"], 60) + except socket.error as err: + _LOG.fatal("Unable to connect to MQTT server: %s" % err) + sys.exit(1) client.loop_start() topic_prefix = config["mqtt"]["topic_prefix"] -- GitLab