Skip to content

Commit 5b6e767

Browse files
authored
Merge pull request #12 from netz39/10-mqtt-internal-reconnect
Use paho-mqtt reconnect feature instead of explicit call
2 parents 45e2ed1 + 1b519ac commit 5b6e767

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/SpaceStatusObserver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def __init__(self, broker, port, topic_status, topic_lastchange, space_api_entry
99
self.space_api_entry = space_api_entry
1010

1111
self.client = mqtt.Client()
12+
self.client.reconnect_on_failure = True
1213
self.client.on_connect = self.on_connect
1314
self.client.on_disconnect = self.on_disconnect
1415
self.client.on_message = self.on_message
@@ -19,11 +20,10 @@ def on_connect(self, client, _userdata, _flags, rc):
1920
print(f"Subscribing to topic {topic}")
2021
client.subscribe(topic)
2122

22-
def on_disconnect(self, client, userdata, rc):
23+
@staticmethod
24+
def on_disconnect(_client, _userdata, rc):
2325
print(f"Disconnected with result code {rc}")
24-
if rc != 0:
25-
print("Unexpected disconnection. Reconnecting...")
26-
self.client.reconnect()
26+
# Reconnect is issued automatically by the client due to reconnect_on_failure=True
2727

2828
def on_message(self, client, userdata, msg):
2929
print(f"Message received on topic {msg.topic}: {msg.payload.decode()}")

0 commit comments

Comments
 (0)