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 e27efe1..1a856bf 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -73,7 +73,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) @@ -154,6 +156,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: @@ -224,14 +227,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(), @@ -241,24 +243,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): @@ -358,15 +353,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 @@ -384,7 +379,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():