From 659d0d08354d6c0bba0221b8dbbc03fa6292fb92 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 14 Jul 2023 16:01:53 +0800 Subject: [PATCH] Init client once No none-ing --- pytec/pytec/aioclient.py | 9 +++++++++ pytec/tec_qt.py | 12 +++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pytec/pytec/aioclient.py b/pytec/pytec/aioclient.py index f6964a9..b474c8e 100644 --- a/pytec/pytec/aioclient.py +++ b/pytec/pytec/aioclient.py @@ -19,13 +19,22 @@ class Client: await self._check_zero_limits() + def is_connecting(self): + return self._connecting_task is not None + + def is_connected(self): + return self._reader is not None + async def disconnect(self): if self._connecting_task is not None: self._connecting_task.cancel() + self._connecting_task = None return self._writer.close() await self._writer.wait_closed() + self._reader = None + self._writer = None async def _check_zero_limits(self): pwm_report = await self.get_pwm() diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 6b66ffc..c8ca2ea 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -74,7 +74,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.fan_pwm_recommended = False - self.tec_client: Client = None + self.tec_client = Client() self.client_watcher: ClientWatcher = None if args.connect: @@ -128,7 +128,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): @asyncSlot(int) async def fan_set(self, value): - if self.tec_client is None or self.fan_auto_box.isChecked(): + if not self.tec_client.is_connected() or self.fan_auto_box.isChecked(): return await self.tec_client.set_param("fan", value) if not self.fan_pwm_recommended: @@ -136,7 +136,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): @asyncSlot(int) async def fan_auto_set(self, enabled): - if self.tec_client is None: + if not self.tec_client.is_connected(): return self.fan_power_slider.setEnabled(not enabled) if enabled: @@ -149,20 +149,19 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): async def closeEvent(self, event): if self.client_watcher is not None: self.client_watcher.stop_watching() - if self.tec_client is not None: + if self.tec_client.is_connecting() or self.tec_client.is_connected(): await self.tec_client.disconnect() @asyncSlot() async def connect(self): ip, port = self.ip_set_line.text(), self.port_set_spin.value() try: - if self.tec_client is None: + if not (self.tec_client.is_connecting() or self.tec_client.is_connected()): self.status_lbl.setText("Connecting...") self.ip_set_line.setEnabled(False) self.port_set_spin.setEnabled(False) self.connect_btn.setText("Stop") - self.tec_client = Client() try: await self.tec_client.connect(host=ip, port=port, timeout=30) except asyncio.CancelledError: @@ -184,7 +183,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.client_watcher = None await self.tec_client.disconnect() - self.tec_client = None self._on_connection_changed(False) except (OSError, TimeoutError) as e: