From e7ac2ca4762e04e21e4d01ad026f307be9a3618d Mon Sep 17 00:00:00 2001 From: Quentin Bernet Date: Tue, 27 May 2025 15:49:44 +0200 Subject: [PATCH] fix: confusing operaror UI status by adding a reconnection message --- tofupilot/openhtf/tofupilot.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tofupilot/openhtf/tofupilot.py b/tofupilot/openhtf/tofupilot.py index c6b310d..4fe218b 100644 --- a/tofupilot/openhtf/tofupilot.py +++ b/tofupilot/openhtf/tofupilot.py @@ -123,6 +123,7 @@ def __init__( self.mqttClient = None self.publishOptions = None self._logger = self.client._logger + self._reconnecting = False self._streaming_setup_thread = None def _upload(self, testRecord: TestRecord): @@ -259,6 +260,7 @@ def _connect_streaming(self) -> str: self.mqttClient.username_pw_set("pythonClient", token) self.mqttClient.on_message = self._on_message + self.mqttClient.on_connect = self._on_connect self.mqttClient.on_disconnect = self._on_disconnect self.mqttClient.on_unsubscribe = self._on_unsubscribe @@ -424,6 +426,16 @@ def _on_message(self, client, userdata, message): if parsed["source"] == "web": self._handle_answer(**parsed["message"]) + def _on_connect( + self, client, userdata, connect_flags, reason_code, properties, + ): + if self._reconnecting: + # Warning to be sure the log level is the same as the other message + self._logger.warning( + f"Operator UI: Reconnected" + ) + self._reconnecting = False + def _on_disconnect( self, client, userdata, disconnect_flags: mqtt.DisconnectFlags, reason_code: ReasonCode, properties ): @@ -432,7 +444,7 @@ def _on_disconnect( self._logger.warning( f"Operator UI: Unexpected disconnect (code {reason_code})" ) - + self._reconnecting = True self._connect_streaming() self.mqttClient.loop_start() test_state_dict, _ = _to_dict_with_event(self.test.state)