Skip to content

Commit 830b02e

Browse files
authored
connect only on init (#146)
* connect only on init * add await
1 parent 8bfb550 commit 830b02e

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

velocitas_sdk/native/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self) -> None:
4040
self.pubsub_client = MqttClient(hostname=_hostname, port=_port)
4141

4242
async def start(self):
43-
pass
43+
await self.pubsub_client.init()
4444

4545
async def wait_until_ready(self):
4646
pass

velocitas_sdk/native/mqtt.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ def __init__(self, hostname: str, port: Optional[int] = None):
4444
self._sub_client.on_connect = self.on_connect
4545
self._sub_client.on_disconnect = self.on_disconnect
4646

47-
if self._port is None:
48-
self._sub_client.connect(self._hostname)
49-
self._pub_client.connect(self._hostname)
50-
else:
51-
self._sub_client.connect(self._hostname, self._port)
52-
self._pub_client.connect(self._hostname, self._port)
53-
5447
def on_connect(self, client, userdata, flags, rc):
5548
if rc == 0:
5649
logger.debug("Mqtt native connection OK!")
@@ -64,13 +57,18 @@ def on_connect(self, client, userdata, flags, rc):
6457
def on_disconnect(self, client, userdata, rc):
6558
logger.debug("Mqtt native is disconnected with reason: %d", rc)
6659

60+
async def init(self):
61+
if self._port is None:
62+
self._sub_client.connect(self._hostname)
63+
self._pub_client.connect(self._hostname)
64+
else:
65+
self._sub_client.connect(self._hostname, self._port)
66+
self._pub_client.connect(self._hostname, self._port)
67+
6768
async def run(self):
6869
self._sub_client.loop_start()
6970
self._pub_client.loop_start()
7071

71-
async def init(self):
72-
"""Do nothing"""
73-
7472
async def subscribe_topic(self, topic, coro):
7573
self._topics_to_subscribe.append(MqttTopicSubscription(topic, coro))
7674
if self._sub_client.is_connected():

0 commit comments

Comments
 (0)