diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index e1098ae..7956b88 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -165,10 +165,14 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.status_lbl.setText("Connecting...") self.ip_set_line.setEnabled(False) self.port_set_spin.setEnabled(False) - self.connect_btn.setEnabled(False) + self.connect_btn.setText("Stop") self.tec_client = Client() - await self.tec_client.connect(host=ip, port=port, timeout=30) + self.connect_task = asyncio.create_task(self.tec_client.connect(host=ip, port=port, timeout=30)) + try: + await self.connect_task + except asyncio.exceptions.CancelledError: + return self.connect_btn.setEnabled(True) self._on_connection_changed(True) @@ -182,8 +186,14 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): ) self.client_watcher.start_watching() else: - self.client_watcher.stop_watching() - self.client_watcher = None + if self.client_watcher is None: + self.connect_task.cancel() + self.tec_client = None + self.on_connection_changed(False) + return + else: + self.client_watcher.stop_watching() + self.client_watcher = None await self.tec_client.disconnect() self.tec_client = None @@ -191,9 +201,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): except Exception as e: logging.error(f"Failed communicating to {ip}:{port}: {e}") + await self.tec_client.disconnect() self._on_connection_changed(False) - self.connect_btn.setEnabled(True) - self.tec_client = None async def coro_main():