From d4afc04b72feb7b2c3e76c91708cd406db362a4e Mon Sep 17 00:00:00 2001 From: Vladimir Vitkov Date: Mon, 12 Feb 2024 11:15:29 +0200 Subject: [PATCH] Ugrade and refactor paho to 2.0.0 --- Pipfile | 2 +- luftdaten2mqtt.py | 40 ++++++++++++++++++++-------------------- requirements.txt | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Pipfile b/Pipfile index 64f3be5..de3f03d 100644 --- a/Pipfile +++ b/Pipfile @@ -5,7 +5,7 @@ name = "pypi" [packages] bottle = "==0.12.25" -paho-mqtt = "==1.6.1" +paho-mqtt = "==2.0.0" [dev-packages] pre-commit = "*" diff --git a/luftdaten2mqtt.py b/luftdaten2mqtt.py index 777643b..8d4d86e 100644 --- a/luftdaten2mqtt.py +++ b/luftdaten2mqtt.py @@ -239,27 +239,27 @@ def route_index(): """on_connect handler to disconnect and die in case of error""" -def on_connect(client, userdata, flags, rc): - if rc == 0: +def on_connect(client, userdata, flags, reason_code, properties): + if reason_code == 0: logging.info("Connected to broker %s", MQTT_HOST) - elif rc == 1: - logging.error("Can't connect to MQTT Broker %s. Incorrect protocol", MQTT_HOST) - os._exit(rc) - elif rc == 2: - logging.error("MQTT Broker %s Refused connection. Invalid Client ID", MQTT_HOST) - os._exit(rc) - elif rc == 3: - logging.error("MQTT Broker %s available ", MQTT_HOST) - os._exit(rc) - elif rc == 4: - logging.error("MQTT Broker %s Refused connection. Bad Username/Password", MQTT_HOST) - os._exit(rc) - elif rc == 5: - logging.error("MQTT Broker %s Refused connection. Not Authorized", MQTT_HOST) - os._exit(rc) + elif reason_code == "Unsupported protocol version": + logging.error("Can't connect to MQTT Broker %s. Unsupported protocol version", MQTT_HOST) + os._exit(1) + elif reason_code == "Client identifier not valid": + logging.error("MQTT Broker %s Refused connection. Client identifier not valid", MQTT_HOST) + os._exit(2) + elif reason_code == "Server unavailable": + logging.error("MQTT Broker %s Server unavailable", MQTT_HOST) + os._exit(3) + elif reason_code == "Bad user name or password": + logging.error("MQTT Broker %s Refused connection. Bad user name or password", MQTT_HOST) + os._exit(4) + elif reason_code == "Not authorized": + logging.error("MQTT Broker %s Refused connection. Not authorized", MQTT_HOST) + os._exit(5) else: - logging.info("Reserved error code (%s), from Broker %s", rc, MQTT_HOST) - os._exit(rc) + logging.info("Reserved error code (%s), from Broker %s", reason_code, MQTT_HOST) + os._exit(99) def setup(): @@ -279,7 +279,7 @@ def setup(): logging.debug("connecting to mqtt broker %s", MQTT_HOST) global CLIENT - CLIENT = mqtt.Client(clean_session=True) + CLIENT = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, clean_session=True) CLIENT.on_connect = on_connect if MQTT_USER and MQTT_PASS: diff --git a/requirements.txt b/requirements.txt index f1be6a4..37c7a42 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ bottle==0.12.25 -paho-mqtt==1.6.1 +paho-mqtt==2.0.0