Base Thermostat connectivity UI changes to state

This commit is contained in:
atse 2024-08-22 17:11:56 +08:00
parent f9c0f140fa
commit 78ba799d56
2 changed files with 60 additions and 40 deletions

View File

@ -3,9 +3,16 @@ from qasync import asyncSlot
from pytec.gui.model.property import Property, PropertyMeta from pytec.gui.model.property import Property, PropertyMeta
import asyncio import asyncio
import logging import logging
from enum import Enum
from pytec.aioclient import AsyncioClient from pytec.aioclient import AsyncioClient
class ThermostatConnectionState(Enum):
DISCONNECTED = False
CONNECTING = "connecting"
CONNECTED = True
class Thermostat(QObject, metaclass=PropertyMeta): class Thermostat(QObject, metaclass=PropertyMeta):
hw_rev = Property(dict) hw_rev = Property(dict)
fan = Property(dict) fan = Property(dict)

View File

@ -7,7 +7,7 @@ from pytec.gui.view.live_plot_view import LiveDataPlotter
from pytec.gui.view.ctrl_panel import CtrlPanel from pytec.gui.view.ctrl_panel import CtrlPanel
from pytec.gui.view.info_box import InfoBox from pytec.gui.view.info_box import InfoBox
from pytec.gui.model.pid_autotuner import PIDAutoTuner from pytec.gui.model.pid_autotuner import PIDAutoTuner
from pytec.gui.model.thermostat import Thermostat from pytec.gui.model.thermostat import Thermostat, ThermostatConnectionState
import json import json
from autotune import PIDAutotuneState from autotune import PIDAutotuneState
from qasync import asyncSlot, asyncClose from qasync import asyncSlot, asyncClose
@ -167,20 +167,37 @@ class MainWindow(QtWidgets.QMainWindow):
self.channel_graphs.clear_graphs() self.channel_graphs.clear_graphs()
async def _on_connection_changed(self, result): async def _on_connection_changed(self, result):
self.graph_group.setEnabled(result) match result:
self.report_group.setEnabled(result) case ThermostatConnectionState.CONNECTED:
self.thermostat_settings.setEnabled(result) self.graph_group.setEnabled(True)
self.report_group.setEnabled(True)
self.thermostat_settings.setEnabled(True)
self.conn_menu.host_set_line.setEnabled(False)
self.conn_menu.port_set_spin.setEnabled(False)
self.connect_btn.setText("Disconnect")
self.conn_menu.host_set_line.setEnabled(not result)
self.conn_menu.port_set_spin.setEnabled(not result)
self.connect_btn.setText("Disconnect" if result else "Connect")
if result:
self.hw_rev_data = await self.thermostat.get_hw_rev() self.hw_rev_data = await self.thermostat.get_hw_rev()
logging.debug(self.hw_rev_data) logging.debug(self.hw_rev_data)
self._status(self.hw_rev_data) self._status(self.hw_rev_data)
self.thermostat.start_watching() self.thermostat.start_watching()
else:
case ThermostatConnectionState.CONNECTING:
self.status_lbl.setText("Connecting...")
self.connect_btn.setText("Stop")
self.conn_menu.host_set_line.setEnabled(False)
self.conn_menu.port_set_spin.setEnabled(False)
case ThermostatConnectionState.DISCONNECTED:
self.graph_group.setEnabled(False)
self.report_group.setEnabled(False)
self.thermostat_settings.setEnabled(False)
self.conn_menu.host_set_line.setEnabled(True)
self.conn_menu.port_set_spin.setEnabled(True)
self.connect_btn.setText("Connect")
self.status_lbl.setText("Disconnected") self.status_lbl.setText("Disconnected")
self.background_task_lbl.setText("Ready.") self.background_task_lbl.setText("Ready.")
self.loading_spinner.hide() self.loading_spinner.hide()
@ -233,11 +250,7 @@ class MainWindow(QtWidgets.QMainWindow):
@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()):
self.status_lbl.setText("Connecting...") await self._on_connection_changed(ThermostatConnectionState.CONNECTING)
self.connect_btn.setText("Stop")
self.conn_menu.host_set_line.setEnabled(False)
self.conn_menu.port_set_spin.setEnabled(False)
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(),
@ -251,11 +264,11 @@ class MainWindow(QtWidgets.QMainWindow):
if isinstance(exc, asyncio.CancelledError): if isinstance(exc, asyncio.CancelledError):
return return
raise raise
else:
await self._on_connection_changed(True)
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:
@ -263,7 +276,7 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncSlot() @asyncSlot()
async def bail(self): async def bail(self):
await self._on_connection_changed(False) await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self.thermostat.end_session() await self.thermostat.end_session()
@asyncSlot(object, object) @asyncSlot(object, object)
@ -402,14 +415,14 @@ 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(False) await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self.thermostat.dfu() await self.thermostat.dfu()
@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(False) await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self.thermostat.reset() await self.thermostat.reset()
await asyncio.sleep(0.1) # Wait for the reset to start await asyncio.sleep(0.1) # Wait for the reset to start