From 981c28ac2763c35a5e1ad84af200305d899c4c7f Mon Sep 17 00:00:00 2001 From: atse Date: Tue, 18 Jul 2023 11:01:51 +0800 Subject: [PATCH] Conslidate connect & disconnect actions --- pytec/pytec/aioclient.py | 6 +++++- pytec/tec_qt.py | 11 ++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pytec/pytec/aioclient.py b/pytec/pytec/aioclient.py index e7c18e3..e87aa96 100644 --- a/pytec/pytec/aioclient.py +++ b/pytec/pytec/aioclient.py @@ -14,10 +14,14 @@ class Client: async def connect(self, host='192.168.1.26', port=23, timeout=None): self._connecting_task = asyncio.create_task(asyncio.open_connection(host, port)) - self._reader, self._writer = await self._connecting_task + try: + self._reader, self._writer = await self._connecting_task + except asyncio.CancelledError: + return False self._connecting_task = None await self._check_zero_limits() + return True def is_connecting(self): return self._connecting_task is not None diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index b1eea22..0965ddf 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -104,7 +104,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.status_lbl.setText("Disconnected") self.fan_pwm_warning.setPixmap(QtGui.QPixmap()) self.fan_pwm_warning.setToolTip("") + self.client_watcher.stop_watching() else: + self.client_watcher.start_watching() self._status(await self.tec_client.hw_rev()) self.fan_update(await self.tec_client.fan()) @@ -171,16 +173,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.ip_set_line.setEnabled(False) self.port_set_spin.setEnabled(False) - try: - await self.tec_client.connect(host=ip, port=port, timeout=30) - except asyncio.CancelledError: + connected = await self.tec_client.connect(host=ip, port=port, timeout=30) + if not connected: return - await self._on_connection_changed(True) - self.client_watcher.start_watching() else: - self.client_watcher.stop_watching() - await self.tec_client.disconnect() await self._on_connection_changed(False)