@@ -173,6 +173,21 @@ def sub_data_incoming(client, userdata, flags, rc, properties=None):
173
173
LOGGER .info (f'subscribe to: { s } ' )
174
174
client .subscribe (s , qos = 1 )
175
175
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' } " )
176
191
177
192
def main ():
178
193
"""
@@ -199,12 +214,13 @@ def main():
199
214
client_id = f"wis2box-data-subscriber-{ CENTRE_ID } "
200
215
client = mqtt .Client (client_id = client_id , protocol = mqtt .MQTTv5 )
201
216
client .on_connect = sub_data_incoming
217
+ client .on_disconnect = log_disconnect
202
218
client .on_message = process_data
203
219
client .username_pw_set (broker_url .username , broker_url .password )
204
220
if broker_url .scheme == 'mqtts' :
205
221
LOGGER .info ("mqtts: set tls_version=2" )
206
222
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 )
208
224
client .loop_forever ()
209
225
except Exception as err :
210
226
LOGGER .error (f"Failed to setup MQTT-client with error: { err } " )
0 commit comments