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.status_lbl.setText("Connecting...")
self.ip_set_line.setEnabled(False) self.ip_set_line.setEnabled(False)
self.port_set_spin.setEnabled(False) self.port_set_spin.setEnabled(False)
self.connect_btn.setEnabled(False) self.connect_btn.setText("Stop")
self.tec_client = Client() 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.connect_btn.setEnabled(True)
self._on_connection_changed(True) self._on_connection_changed(True)
@ -181,6 +185,12 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value()) lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value())
) )
self.client_watcher.start_watching() self.client_watcher.start_watching()
else:
if self.client_watcher is None:
self.connect_task.cancel()
self.tec_client = None
self.on_connection_changed(False)
return
else: else:
self.client_watcher.stop_watching() self.client_watcher.stop_watching()
self.client_watcher = None self.client_watcher = None
@ -191,9 +201,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
except Exception as e: except Exception as e:
logging.error(f"Failed communicating to {ip}:{port}: {e}") logging.error(f"Failed communicating to {ip}:{port}: {e}")
await self.tec_client.disconnect()
self._on_connection_changed(False) self._on_connection_changed(False)
self.connect_btn.setEnabled(True)
self.tec_client = None
async def coro_main(): async def coro_main():