Skip to content

Commit c12cdd9

Browse files
committed
add log on disconnect and keepalive=5
1 parent 0e90c8b commit c12cdd9

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

wis2box-data-subscriber.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ def sub_data_incoming(client, userdata, flags, rc, properties=None):
173173
LOGGER.info(f'subscribe to: {s}')
174174
client.subscribe(s, qos=1)
175175

176+
def log_disconnect(client, userdata, flags, rc, properties=None):
177+
"""
178+
function executed 'on_disconnect' for paho.mqtt.client
179+
logs the disconnect event
180+
181+
:param client: client-object associated to 'on_disconnect'
182+
:param userdata: userdata
183+
:param flags: flags
184+
:param rc: return-code received 'on_disconnect'
185+
:param properties: properties
186+
187+
:returns: `None`
188+
"""
189+
LOGGER.error(f"disconnected: {mqtt.error_string(rc)}")
190+
LOGGER.error(f"reason: {properties.reason_string if properties else 'no reason provided'}")
176191

177192
def main():
178193
"""
@@ -199,12 +214,13 @@ def main():
199214
client_id = f"wis2box-data-subscriber-{CENTRE_ID}"
200215
client = mqtt.Client(client_id=client_id, protocol=mqtt.MQTTv5)
201216
client.on_connect = sub_data_incoming
217+
client.on_disconnect = log_disconnect
202218
client.on_message = process_data
203219
client.username_pw_set(broker_url.username, broker_url.password)
204220
if broker_url.scheme == 'mqtts':
205221
LOGGER.info("mqtts: set tls_version=2")
206222
client.tls_set(tls_version=2)
207-
client.connect(broker_url.hostname, broker_url.port)
223+
client.connect(broker_url.hostname, broker_url.port, 5)
208224
client.loop_forever()
209225
except Exception as err:
210226
LOGGER.error(f"Failed to setup MQTT-client with error: {err}")

0 commit comments

Comments
 (0)