Init client_watcher once

This commit is contained in:
atse 2023-07-14 16:10:59 +08:00
parent 659d0d0835
commit 27ce311111
1 changed files with 13 additions and 11 deletions

View File

@ -53,9 +53,14 @@ class ClientWatcher(QObject):
def start_watching(self): def start_watching(self):
self.watch_task = asyncio.create_task(self.run()) self.watch_task = asyncio.create_task(self.run())
def is_watching(self):
return self.watch_task is not None
@pyqtSlot() @pyqtSlot()
def stop_watching(self): def stop_watching(self):
if self.watch_task is not None:
self.watch_task.cancel() self.watch_task.cancel()
self.watch_task = None
@pyqtSlot(float) @pyqtSlot(float)
def set_update_s(self, update_s): def set_update_s(self, update_s):
@ -75,7 +80,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.fan_pwm_recommended = False self.fan_pwm_recommended = False
self.tec_client = Client() 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.connect:
if args.IP: if args.IP:
@ -147,7 +156,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@asyncClose @asyncClose
async def closeEvent(self, event): async def closeEvent(self, event):
if self.client_watcher is not None: if self.client_watcher.is_watching():
self.client_watcher.stop_watching() self.client_watcher.stop_watching()
if self.tec_client.is_connecting() or self.tec_client.is_connected(): if self.tec_client.is_connecting() or self.tec_client.is_connected():
await self.tec_client.disconnect() await self.tec_client.disconnect()
@ -171,16 +180,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self._status(await self.tec_client.hw_rev()) self._status(await self.tec_client.hw_rev())
self.fan_update(await self.tec_client.fan()) 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() self.client_watcher.start_watching()
else: else:
if self.client_watcher is not None:
self.client_watcher.stop_watching() self.client_watcher.stop_watching()
self.client_watcher = None
await self.tec_client.disconnect() await self.tec_client.disconnect()
self._on_connection_changed(False) self._on_connection_changed(False)