From 8a13ce2b4791d1402eb3059b2da158aa5a52b8d5 Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 26 Aug 2024 17:09:29 +0800 Subject: [PATCH] Tie Thermostat ConnectionState to Qt signal for now Change this to callback-based later for decoupling from Qt --- pytec/pytec/gui/model/thermostat.py | 5 +++++ pytec/tec_qt.py | 26 ++++++++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pytec/pytec/gui/model/thermostat.py b/pytec/pytec/gui/model/thermostat.py index 15e8b19..31c1deb 100644 --- a/pytec/pytec/gui/model/thermostat.py +++ b/pytec/pytec/gui/model/thermostat.py @@ -24,6 +24,7 @@ class Thermostat(QObject, metaclass=PropertyMeta): report = Property(list) info_box_trigger = pyqtSignal(str, str) connection_error = pyqtSignal() + connection_state_changed = pyqtSignal(ThermostatConnectionState) def __init__(self, parent, update_s): self._update_s = update_s @@ -36,7 +37,9 @@ class Thermostat(QObject, metaclass=PropertyMeta): super().__init__(parent) async def start_session(self, host, port): + self.connection_state_changed.emit(ThermostatConnectionState.CONNECTING) await self._client.connect(host, port) + self.connection_state_changed.emit(ThermostatConnectionState.CONNECTED) hw_rev_data = await self.get_hw_rev() self.start_watching() return hw_rev_data @@ -113,10 +116,12 @@ class Thermostat(QObject, metaclass=PropertyMeta): self.report[i]["interval"] for i in range(len(self.report)) ] + @asyncSlot() async def end_session(self): await self.set_report_mode(False) self.stop_watching() await self._client.disconnect() + self.connection_state_changed.emit(ThermostatConnectionState.DISCONNECTED) self.connection_errored = False async def set_ipv4(self, ipv4): diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 8945501..e3c78c7 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -75,7 +75,9 @@ class MainWindow(QtWidgets.QMainWindow): self.thermostat.connection_error.connect(handle_connection_error) self.thermostat.connection_error.connect(self.thermostat.timed_out) - self.thermostat.connection_error.connect(self.bail) + self.thermostat.connection_error.connect(self.thermostat.end_session) + + self.thermostat.connection_state_changed.connect(self._on_connection_changed) self.autotuners = PIDAutoTuner(self, self.thermostat, 2) @@ -163,6 +165,7 @@ class MainWindow(QtWidgets.QMainWindow): def clear_graphs(self): self.channel_graphs.clear_graphs() + @asyncSlot(ThermostatConnectionState) async def _on_connection_changed(self, result): match result: case ThermostatConnectionState.CONNECTED: @@ -233,15 +236,13 @@ class MainWindow(QtWidgets.QMainWindow): @asyncClose async def closeEvent(self, _event): try: - await self.bail() + await self.thermostat.end_session() except: pass @asyncSlot() async def on_connect_btn_clicked(self): if (self._connecting_task is None) and (not self.thermostat.connected()): - await self._on_connection_changed(ThermostatConnectionState.CONNECTING) - self._connecting_task = asyncio.create_task( self.thermostat.start_session( host=self.conn_menu.host_set_line.text(), @@ -251,24 +252,17 @@ class MainWindow(QtWidgets.QMainWindow): try: self.hw_rev_data = await self._connecting_task except (OSError, asyncio.CancelledError) as exc: - await self.bail() + await self.thermostat.end_session() if isinstance(exc, asyncio.CancelledError): return raise finally: self._connecting_task = None - await self._on_connection_changed(ThermostatConnectionState.CONNECTED) - elif self._connecting_task is not None: self._connecting_task.cancel() else: - await self.bail() - - @asyncSlot() - async def bail(self): - await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED) - await self.thermostat.end_session() + await self.thermostat.end_session() @asyncSlot() async def pid_auto_tune_request(self, ch=0): @@ -368,15 +362,15 @@ class MainWindow(QtWidgets.QMainWindow): async def dfu_request(self, _): assert self.thermostat.connected() - await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED) await self.thermostat.dfu() + await self.thermostat.end_session() @asyncSlot(bool) async def reset_request(self, _): assert self.thermostat.connected() - await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED) await self.thermostat.reset() + await self.thermostat.end_session() await asyncio.sleep(0.1) # Wait for the reset to start self.connect_btn.click() # Reconnect @@ -394,7 +388,7 @@ class MainWindow(QtWidgets.QMainWindow): assert self.thermostat.connected() await self.thermostat.set_ipv4(ipv4_settings) - await self.bail() + await self.thermostat.end_session() async def coro_main():