Add stop connection button

Stuff to add to stop button
This commit is contained in:
atse 2023-07-11 13:43:09 +08:00
parent 7e1b64b72c
commit 115c7eb800
1 changed files with 15 additions and 6 deletions

View File

@ -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():