From 9cf33abe0657b0c5208badffe8df48febdb28685 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 5 Jul 2023 13:13:54 +0800 Subject: [PATCH] Gather client_watcher managment into connect --- pytec/tec_qt.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 81a0af2..4d3cb68 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -82,7 +82,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.connect_btn.click() def _on_connection_changed(self, result): - global client_watcher self.graph_group.setEnabled(result) self.hw_rev_lbl.setEnabled(result) self.fan_group.setEnabled(result) @@ -95,10 +94,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): if not result: self.hw_rev_lbl.setText("Thermostat vX.Y") self.fan_group.setStyleSheet("") - if client_watcher: - client_watcher.stop_watching() - client_watcher = None - self.client_watcher_task = None def _hw_rev(self, hw_rev_d: dict): logging.debug(hw_rev_d) @@ -152,18 +147,23 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self._on_connection_changed(True) self._hw_rev(await tec_client.hw_rev()) # self.fan_update(await tec_client.fan()) - if client_watcher is None: - client_watcher = ClientWatcher(self.main_widget, self.report_refresh_spin.value()) - client_watcher.fan_update.connect(self.fan_update) - self.report_apply_btn.clicked.connect( - lambda: client_watcher.set_update_s(self.report_refresh_spin.value()) - ) - QtWidgets.QApplication.instance().aboutToQuit.connect(client_watcher.stop_watching) - self.client_watcher_task = asyncio.create_task(client_watcher.run()) + + client_watcher = ClientWatcher(self.main_widget, self.report_refresh_spin.value()) + client_watcher.fan_update.connect(self.fan_update) + self.report_apply_btn.clicked.connect( + lambda: client_watcher.set_update_s(self.report_refresh_spin.value()) + ) + QtWidgets.QApplication.instance().aboutToQuit.connect(client_watcher.stop_watching) + self.client_watcher_task = asyncio.create_task(client_watcher.run()) else: await tec_client.disconnect() tec_client = None self._on_connection_changed(False) + + client_watcher.stop_watching() + client_watcher = None + await self.client_watcher_task + self.client_watcher_task = None except Exception as e: logging.error(f"Failed communicating to the {ip}:{port}: {e}") self._on_connection_changed(False)