Thermostat: Add disconnect callback

For communicating with the autotuner before the client fully
disconnects
This commit is contained in:
atse 2024-08-28 17:06:17 +08:00
parent c761f9fe94
commit 60295d2baa
2 changed files with 16 additions and 4 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

@ -79,6 +79,13 @@ class MainWindow(QtWidgets.QMainWindow):
self.autotuners = PIDAutoTuner(self, self.thermostat, 2)
self.autotuners.autotune_state_changed.connect(self.pid_autotune_handler)
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
def get_ctrl_panel_config(args):
with open(args.param_tree, "r", encoding="utf-8") as f:
return json.load(f)["ctrl_panel"]
@ -159,9 +166,6 @@ class MainWindow(QtWidgets.QMainWindow):
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):