Skip to content

Commit

Permalink
improve paho reconnect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Mar 8, 2022
1 parent c7660d3 commit cc0546e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions util/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
limitations under the License.
"""

__all__ = ("MQTTClient", )
__all__ = ("MQTTClient",)


from .logger import get_logger
from .config import conf
import paho.mqtt.client
import time

import mgw_dc
import paho.mqtt.client

from paho.mqtt.client import MQTT_ERR_SUCCESS, MQTT_ERR_NO_CONN

from .config import conf
from .logger import get_logger

logger = get_logger(__name__.split(".", 1)[-1])

Expand Down Expand Up @@ -61,16 +63,16 @@ def __on_message(self, client, userdata, message: paho.mqtt.client.MQTTMessage):
self.on_message(message.topic, message.payload)

def start(self):
while True:
code = MQTT_ERR_NO_CONN
while code != MQTT_ERR_SUCCESS:
try:
self.__client.connect(conf.MsgBroker.host, conf.MsgBroker.port, keepalive=conf.Client.keep_alive)
self.__client.loop_forever()
break
code = self.__client.connect(conf.MsgBroker.host, conf.MsgBroker.port, keepalive=conf.Client.keep_alive)
except Exception as ex:
logger.error(
"could not connect to '{}' on '{}' - {}".format(conf.MsgBroker.host, conf.MsgBroker.port, ex)
"could not connect to '{}' on '{}' - {}".format(conf.MsgBroker.host, conf.MsgBroker.port, str(ex))
)
time.sleep(5)
self.__client.loop_forever()

def subscribe(self, topic: str, qos: int) -> None:
res = self.__client.subscribe(topic=topic, qos=qos)
Expand Down

0 comments on commit cc0546e

Please sign in to comment.