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
This commit is contained in:
atse 2024-08-28 17:06:17 +08:00
parent 15880290b0
commit c549d0344e
2 changed files with 15 additions and 8 deletions

View File

@ -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)

View File

@ -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):