From c549d0344e4e8acc1e1dc5c2c164443f57801bef Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 28 Aug 2024 17:06:17 +0800 Subject: [PATCH] Thermostat: Add disconnect callback For communicating with the autotuner before the client fully disconnects Also then there's no need for explicitly resetting autotune elements --- pytec/pytec/gui/model/thermostat.py | 10 +++++++++- pytec/tec_qt.py | 13 ++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pytec/pytec/gui/model/thermostat.py b/pytec/pytec/gui/model/thermostat.py index 3d6b7b9..b0ee322 100644 --- a/pytec/pytec/gui/model/thermostat.py +++ b/pytec/pytec/gui/model/thermostat.py @@ -26,13 +26,14 @@ class Thermostat(QObject, metaclass=PropertyMeta): connection_error = pyqtSignal() connection_state_changed = pyqtSignal(ThermostatConnectionState) - def __init__(self, parent, update_s): + def __init__(self, parent, update_s, disconnect_cb=None): self._update_s = update_s self._client = AsyncioClient() self._watch_task = None self._report_mode_task = None self._poll_for_report = True self._update_params_task = None + self.disconnect_cb = disconnect_cb super().__init__(parent) async def start_session(self, host, port): @@ -113,6 +114,13 @@ class Thermostat(QObject, metaclass=PropertyMeta): async def end_session(self): await self.set_report_mode(False) self.stop_watching() + + if self.disconnect_cb is not None: + if asyncio.iscoroutinefunction(self.disconnect_cb): + await self.disconnect_cb() + else: + self.disconnect_cb() + await self._client.disconnect() self.connection_state_changed.emit(ThermostatConnectionState.DISCONNECTED) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 9cf00f6..b5c1beb 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -63,6 +63,12 @@ class MainWindow(QtWidgets.QMainWindow): self.thermostat = Thermostat(self, self.report_refresh_spin.value()) self._connecting_task = None + async def autotune_disconnect(): + for ch in range(self.NUM_CHANNELS): + if self.autotuners.get_state(ch) != PIDAutotuneState.STATE_OFF: + await self.autotuners.stop_pid_from_running(ch) + self.thermostat.disconnect_cb = autotune_disconnect + @asyncSlot() async def handle_connection_error(): self.info_box.display_info_box( @@ -145,14 +151,7 @@ class MainWindow(QtWidgets.QMainWindow): case ThermostatConnectionState.DISCONNECTED: self.connect_btn.setText("Connect") self.status_lbl.setText("Disconnected") - - self.background_task_lbl.setText("Ready.") - self.loading_spinner.hide() - self.loading_spinner.stop() self.report_box.setChecked(False) - for ch in range(self.NUM_CHANNELS): - if self.autotuners.get_state(ch) != PIDAutotuneState.STATE_OFF: - await self.autotuners.stop_pid_from_running(ch) @asyncSlot(int) async def on_report_box_stateChanged(self, enabled):