1
1
import json
2
2
import logging
3
3
import secrets
4
- import time
5
4
from typing import List
6
5
7
6
from injector import inject
@@ -31,24 +30,9 @@ def on_connect(client, userdata, flags, reason_code, properties):
31
30
def on_disconnect (client , userdata , flags , reason_code , properties ):
32
31
log .warning (f"Disconnected from MQTT Broker: { reason_code } " )
33
32
34
- delay = 5
35
- max_delay = 60
36
-
37
- while True :
38
- try :
39
- if not client .reconnect ():
40
- log .info ("Successful reconnect to MQTT Broker" )
41
- break
42
- except ConnectionRefusedError :
43
- log .info ("Reconnect to MQTT Broker failed, retry in %d seconds" , delay )
44
-
45
- if delay < max_delay :
46
- delay *= 2 + secrets .randbelow (5 )
47
-
48
- time .sleep (delay )
49
-
50
33
try :
51
- self .client = mqtt_client .Client (client_id = client_id , clean_session = False , callback_api_version = CallbackAPIVersion .VERSION2 )
34
+ self .client = mqtt_client .Client (client_id = client_id , clean_session = False ,
35
+ callback_api_version = CallbackAPIVersion .VERSION2 )
52
36
# client.username_pw_set(username, password)
53
37
self .client .on_connect = on_connect
54
38
self .client .on_disconnect = on_disconnect
@@ -68,7 +52,10 @@ def store(self, data: List[dict]):
68
52
69
53
if not self .client .is_connected ():
70
54
log .warning ("client not connected, try to reconnect" )
71
- self .client .reconnect ()
55
+ try :
56
+ self .client .reconnect ()
57
+ except Exception :
58
+ log .warning ("reconnect failed" )
72
59
73
60
for entry in data :
74
61
topic , json_message = self .map_entry (entry )
0 commit comments