From 5cf00d1891c51df2a6b007eaa036022b4fde53c4 Mon Sep 17 00:00:00 2001 From: Markus Petke Date: Tue, 13 Aug 2024 08:05:12 +0200 Subject: [PATCH] connect only on init --- velocitas_sdk/native/middleware.py | 2 +- velocitas_sdk/native/mqtt.py | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/velocitas_sdk/native/middleware.py b/velocitas_sdk/native/middleware.py index 15332b0..a93522a 100644 --- a/velocitas_sdk/native/middleware.py +++ b/velocitas_sdk/native/middleware.py @@ -40,7 +40,7 @@ def __init__(self) -> None: self.pubsub_client = MqttClient(hostname=_hostname, port=_port) async def start(self): - pass + self.pubsub_client.init() async def wait_until_ready(self): pass diff --git a/velocitas_sdk/native/mqtt.py b/velocitas_sdk/native/mqtt.py index c7eb7e6..836e13e 100644 --- a/velocitas_sdk/native/mqtt.py +++ b/velocitas_sdk/native/mqtt.py @@ -44,13 +44,6 @@ def __init__(self, hostname: str, port: Optional[int] = None): self._sub_client.on_connect = self.on_connect self._sub_client.on_disconnect = self.on_disconnect - if self._port is None: - self._sub_client.connect(self._hostname) - self._pub_client.connect(self._hostname) - else: - self._sub_client.connect(self._hostname, self._port) - self._pub_client.connect(self._hostname, self._port) - def on_connect(self, client, userdata, flags, rc): if rc == 0: logger.debug("Mqtt native connection OK!") @@ -64,13 +57,18 @@ def on_connect(self, client, userdata, flags, rc): def on_disconnect(self, client, userdata, rc): logger.debug("Mqtt native is disconnected with reason: %d", rc) + async def init(self): + if self._port is None: + self._sub_client.connect(self._hostname) + self._pub_client.connect(self._hostname) + else: + self._sub_client.connect(self._hostname, self._port) + self._pub_client.connect(self._hostname, self._port) + async def run(self): self._sub_client.loop_start() self._pub_client.loop_start() - async def init(self): - """Do nothing""" - async def subscribe_topic(self, topic, coro): self._topics_to_subscribe.append(MqttTopicSubscription(topic, coro)) if self._sub_client.is_connected():