Broadcast ConnectionState changes from Thermostat

This commit is contained in:
atse 2024-08-26 17:09:29 +08:00
parent 7e235c3fea
commit f29095a0da
2 changed files with 15 additions and 15 deletions

View File

@ -24,6 +24,7 @@ class Thermostat(QObject, metaclass=PropertyMeta):
report = Property(list) report = Property(list)
info_box_trigger = pyqtSignal(str, str) info_box_trigger = pyqtSignal(str, str)
connection_error = pyqtSignal() connection_error = pyqtSignal()
connection_state_changed = pyqtSignal(ThermostatConnectionState)
def __init__(self, parent, update_s): def __init__(self, parent, update_s):
self._update_s = update_s self._update_s = update_s
@ -36,7 +37,9 @@ class Thermostat(QObject, metaclass=PropertyMeta):
super().__init__(parent) super().__init__(parent)
async def start_session(self, host, port): async def start_session(self, host, port):
self.connection_state_changed.emit(ThermostatConnectionState.CONNECTING)
await self._client.connect(host, port) await self._client.connect(host, port)
self.connection_state_changed.emit(ThermostatConnectionState.CONNECTED)
hw_rev_data = await self.get_hw_rev() hw_rev_data = await self.get_hw_rev()
self.start_watching() self.start_watching()
return hw_rev_data return hw_rev_data
@ -113,10 +116,12 @@ class Thermostat(QObject, metaclass=PropertyMeta):
self.report[i]["interval"] for i in range(len(self.report)) self.report[i]["interval"] for i in range(len(self.report))
] ]
@asyncSlot()
async def end_session(self): async def end_session(self):
await self.set_report_mode(False) await self.set_report_mode(False)
self.stop_watching() self.stop_watching()
await self._client.disconnect() await self._client.disconnect()
self.connection_state_changed.emit(ThermostatConnectionState.DISCONNECTED)
self.connection_errored = False self.connection_errored = False
async def set_ipv4(self, ipv4): async def set_ipv4(self, ipv4):

View File

@ -73,7 +73,9 @@ class MainWindow(QtWidgets.QMainWindow):
self.thermostat.connection_error.connect(handle_connection_error) self.thermostat.connection_error.connect(handle_connection_error)
self.thermostat.connection_error.connect(self.thermostat.timed_out) 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) self.autotuners = PIDAutoTuner(self, self.thermostat, 2)
@ -154,6 +156,7 @@ class MainWindow(QtWidgets.QMainWindow):
def clear_graphs(self): def clear_graphs(self):
self.channel_graphs.clear_graphs() self.channel_graphs.clear_graphs()
@asyncSlot(ThermostatConnectionState)
async def _on_connection_changed(self, result): async def _on_connection_changed(self, result):
match result: match result:
case ThermostatConnectionState.CONNECTED: case ThermostatConnectionState.CONNECTED:
@ -224,14 +227,13 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncClose @asyncClose
async def closeEvent(self, _event): async def closeEvent(self, _event):
try: try:
await self.bail() await self.thermostat.end_session()
except: except:
pass pass
@asyncSlot() @asyncSlot()
async def on_connect_btn_clicked(self): async def on_connect_btn_clicked(self):
if (self._connecting_task is None) and (not self.thermostat.connected()): 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._connecting_task = asyncio.create_task(
self.thermostat.start_session( self.thermostat.start_session(
host=self.conn_menu.host_set_line.text(), host=self.conn_menu.host_set_line.text(),
@ -241,23 +243,16 @@ class MainWindow(QtWidgets.QMainWindow):
try: try:
self.hw_rev_data = await self._connecting_task self.hw_rev_data = await self._connecting_task
except (OSError, asyncio.CancelledError) as exc: except (OSError, asyncio.CancelledError) as exc:
await self.bail() await self.thermostat.end_session()
if isinstance(exc, asyncio.CancelledError): if isinstance(exc, asyncio.CancelledError):
return return
raise raise
finally: finally:
self._connecting_task = None self._connecting_task = None
await self._on_connection_changed(ThermostatConnectionState.CONNECTED)
elif self._connecting_task is not None: elif self._connecting_task is not None:
self._connecting_task.cancel() self._connecting_task.cancel()
else: 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() @asyncSlot()
@ -358,15 +353,15 @@ class MainWindow(QtWidgets.QMainWindow):
async def dfu_request(self, _): async def dfu_request(self, _):
assert self.thermostat.connected() assert self.thermostat.connected()
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self.thermostat.dfu() await self.thermostat.dfu()
await self.thermostat.end_session()
@asyncSlot(bool) @asyncSlot(bool)
async def reset_request(self, _): async def reset_request(self, _):
assert self.thermostat.connected() assert self.thermostat.connected()
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self.thermostat.reset() await self.thermostat.reset()
await self.thermostat.end_session()
await asyncio.sleep(0.1) # Wait for the reset to start await asyncio.sleep(0.1) # Wait for the reset to start
self.connect_btn.click() # Reconnect self.connect_btn.click() # Reconnect
@ -384,7 +379,7 @@ class MainWindow(QtWidgets.QMainWindow):
assert self.thermostat.connected() assert self.thermostat.connected()
await self.thermostat.set_ipv4(ipv4_settings) await self.thermostat.set_ipv4(ipv4_settings)
await self.bail() await self.thermostat.end_session()
async def coro_main(): async def coro_main():