Refactor MQTT listening
This commit is contained in:
@@ -78,6 +78,8 @@ class Mqtt:
|
|||||||
if "lastIssuedSeqId" in j:
|
if "lastIssuedSeqId" in j:
|
||||||
self._sequence_id = j["lastIssuedSeqId"]
|
self._sequence_id = j["lastIssuedSeqId"]
|
||||||
|
|
||||||
|
log.debug("MQTT payload: %s", j)
|
||||||
|
|
||||||
# Call the external callback
|
# Call the external callback
|
||||||
self._on_message(message.topic, j)
|
self._on_message(message.topic, j)
|
||||||
|
|
||||||
@@ -207,16 +209,18 @@ class Mqtt:
|
|||||||
path="/chat?sid={}".format(session_id), headers=headers
|
path="/chat?sid={}".format(session_id), headers=headers
|
||||||
)
|
)
|
||||||
|
|
||||||
def listen(self):
|
def loop_once(self):
|
||||||
while True:
|
"""Run the listening loop once.
|
||||||
|
|
||||||
|
Returns whether to keep listening or not.
|
||||||
|
"""
|
||||||
rc = self._mqtt.loop(timeout=1.0)
|
rc = self._mqtt.loop(timeout=1.0)
|
||||||
if rc == paho.mqtt.client.MQTT_ERR_SUCCESS:
|
|
||||||
continue # No errors
|
|
||||||
|
|
||||||
# If disconnect() has been called
|
# If disconnect() has been called
|
||||||
if self._mqtt._state == paho.mqtt.client.mqtt_cs_disconnecting:
|
if self._mqtt._state == paho.mqtt.client.mqtt_cs_disconnecting:
|
||||||
break
|
return False # Stop listening
|
||||||
|
|
||||||
|
if rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
|
||||||
# Wait before reconnecting
|
# Wait before reconnecting
|
||||||
self._mqtt._reconnect_wait()
|
self._mqtt._reconnect_wait()
|
||||||
|
|
||||||
@@ -232,7 +236,7 @@ class Mqtt:
|
|||||||
):
|
):
|
||||||
log.debug("MQTT connection failed")
|
log.debug("MQTT connection failed")
|
||||||
|
|
||||||
# self._mqtt.loop_forever() # TODO: retry_first_connection=True?
|
return True # Keep listening
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self._mqtt.disconnect()
|
self._mqtt.disconnect()
|
||||||
|
Reference in New Issue
Block a user