From 4a06a7579f859f7e2d61dd5174bd862b927fc001 Mon Sep 17 00:00:00 2001 From: linuswck Date: Fri, 19 Apr 2024 14:49:01 +0800 Subject: [PATCH] gui: handle hard reset in _on_connection_changed() - Ensure correct gui disconnection seq during Kirdy hard reset --- pykirdy/kirdy_qt.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pykirdy/kirdy_qt.py b/pykirdy/kirdy_qt.py index fa121de..5b85cfe 100644 --- a/pykirdy/kirdy_qt.py +++ b/pykirdy/kirdy_qt.py @@ -70,6 +70,8 @@ class KirdyDataWatcher(QObject): if task.done(): task = asyncio.create_task(self.signal_emitter()) await asyncio.sleep(self._update_s) + except asyncio.CancelledError: + pass except Exception as e: logging.error(f"Encountered an error: {e}. disconnecting.", exc_info=True) self._kirdy.stop_report_mode() @@ -81,6 +83,7 @@ class KirdyDataWatcher(QObject): async def stop_watching(self): if self._watch_task is not None: self._watch_task.cancel() + await self._watch_task self._watch_task = None await self.set_report_mode(False) @@ -89,9 +92,9 @@ class KirdyDataWatcher(QObject): if enabled: self._report_mode_task = asyncio.create_task(self.report_mode()) else: - self._kirdy.stop_report_mode() if self._report_mode_task is not None: - self._report_mode_task.cancel() + self._kirdy.stop_report_mode() + await self._report_mode_task self._report_mode_task = None async def report_mode(self): @@ -610,7 +613,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): tree.setParameters(self.params[3], showTop=False) self.params[3].sigTreeStateChanged.connect(self.send_command) - async def _on_connection_changed(self, result): + async def _on_connection_changed(self, result, hard_reset=False): def ctrl_panel_setEnable(result): self.ld_status.setEnabled(result) self.ld_tree.setEnabled(result) @@ -659,8 +662,10 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.status_lbl.setText("Disconnected") self.clear_graphs() self.report_box.setChecked(False) - await self.kirdy_data_watcher.set_report_mode(False) - self.kirdy_data_watcher.stop_watching() + await self.kirdy_data_watcher.stop_watching() + if hard_reset: + await self.kirdy.device.hard_reset() + await self.kirdy.end_session() self.status_lbl.setText("Disconnected") def _status(self):