From 27ce3111116c54bffb8fe261acb1cb792bbb7f99 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 14 Jul 2023 16:10:59 +0800 Subject: [PATCH] Init client_watcher once --- pytec/tec_qt.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index c8ca2ea..69a5f01 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -53,9 +53,14 @@ class ClientWatcher(QObject): def start_watching(self): self.watch_task = asyncio.create_task(self.run()) + def is_watching(self): + return self.watch_task is not None + @pyqtSlot() def stop_watching(self): - self.watch_task.cancel() + if self.watch_task is not None: + self.watch_task.cancel() + self.watch_task = None @pyqtSlot(float) def set_update_s(self, update_s): @@ -75,7 +80,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.fan_pwm_recommended = False self.tec_client = Client() - self.client_watcher: ClientWatcher = None + self.client_watcher = ClientWatcher(self, self.tec_client, self.report_refresh_spin.value()) + self.client_watcher.fan_update.connect(self.fan_update) + self.report_apply_btn.clicked.connect( + lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value()) + ) if args.connect: if args.IP: @@ -147,7 +156,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): @asyncClose async def closeEvent(self, event): - if self.client_watcher is not None: + if self.client_watcher.is_watching(): self.client_watcher.stop_watching() if self.tec_client.is_connecting() or self.tec_client.is_connected(): await self.tec_client.disconnect() @@ -171,16 +180,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self._status(await self.tec_client.hw_rev()) self.fan_update(await self.tec_client.fan()) - self.client_watcher = ClientWatcher(self, self.tec_client, self.report_refresh_spin.value()) - self.client_watcher.fan_update.connect(self.fan_update) - self.report_apply_btn.clicked.connect( - lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value()) - ) self.client_watcher.start_watching() else: - if self.client_watcher is not None: - self.client_watcher.stop_watching() - self.client_watcher = None + self.client_watcher.stop_watching() await self.tec_client.disconnect() self._on_connection_changed(False)