Compare commits

..

1 Commits

Author SHA1 Message Date
2b6b7f4db0 State 2024-08-23 11:37:03 +08:00
2 changed files with 46 additions and 57 deletions

View File

@ -8,9 +8,9 @@ from pytec.aioclient import AsyncioClient
class ThermostatConnectionState(Enum):
DISCONNECTED = False
CONNECTING = "connecting"
CONNECTED = True
STATE_DISCONNECTED = "disconnected"
STATE_CONNECTING = "connecting"
STATE_CONNECTED = "connected"
class Thermostat(QObject, metaclass=PropertyMeta):
@ -37,7 +37,6 @@ class Thermostat(QObject, metaclass=PropertyMeta):
async def start_session(self, host, port):
await self._client.connect(host, port)
self.start_watching()
async def run(self):
self.task = asyncio.create_task(self.update_params())
@ -105,8 +104,6 @@ class Thermostat(QObject, metaclass=PropertyMeta):
]
async def end_session(self):
await self.set_report_mode(False)
self.stop_watching()
await self._client.disconnect()
self.connection_errored = False

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.info_box import InfoBox
from pytec.gui.model.pid_autotuner import PIDAutoTuner
from pytec.gui.model.thermostat import Thermostat, ThermostatConnectionState
from pytec.gui.model.thermostat import Thermostat
import json
from autotune import PIDAutotuneState
from qasync import asyncSlot, asyncClose
@ -176,36 +176,20 @@ class MainWindow(QtWidgets.QMainWindow):
self.channel_graphs.clear_graphs()
async def _on_connection_changed(self, result):
match result:
case ThermostatConnectionState.CONNECTED:
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.graph_group.setEnabled(result)
self.report_group.setEnabled(result)
self.thermostat_settings.setEnabled(result)
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()
logging.debug(self.hw_rev_data)
self._status(self.hw_rev_data)
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.thermostat.start_watching()
else:
self.status_lbl.setText("Disconnected")
self.background_task_lbl.setText("Ready.")
self.loading_spinner.hide()
@ -221,6 +205,8 @@ class MainWindow(QtWidgets.QMainWindow):
self.autotuners.autotuners[ch].setOff()
else:
await self.autotuners.stop_pid_from_running(ch)
await self.thermostat.set_report_mode(False)
self.thermostat.stop_watching()
def _status(self, hw_rev_d: dict):
logging.debug(hw_rev_d)
@ -253,13 +239,19 @@ class MainWindow(QtWidgets.QMainWindow):
except:
pass
def _connecting(self):
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)
@asyncSlot()
async def on_connect_btn_clicked(self):
if (self._connecting_task is None) and (not self.thermostat.connected()):
host = self.conn_menu.host_set_line.text()
port = self.conn_menu.port_set_spin.value()
await self._on_connection_changed(ThermostatConnectionState.CONNECTING)
self._connecting()
self._connecting_task = asyncio.create_task(
self.thermostat.start_session(host=host, port=port)
)
@ -270,11 +262,11 @@ class MainWindow(QtWidgets.QMainWindow):
if isinstance(exc, asyncio.CancelledError):
return
raise
else:
await self._on_connection_changed(True)
finally:
self._connecting_task = None
await self._on_connection_changed(ThermostatConnectionState.CONNECTED)
elif self._connecting_task is not None:
self._connecting_task.cancel()
else:
@ -282,7 +274,7 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncSlot()
async def bail(self):
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self._on_connection_changed(False)
await self.thermostat.end_session()
@asyncSlot(object, object)
@ -421,14 +413,14 @@ class MainWindow(QtWidgets.QMainWindow):
async def dfu_request(self, _):
assert self.thermostat.connected()
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self._on_connection_changed(False)
await self.thermostat.dfu()
@asyncSlot(bool)
async def reset_request(self, _):
assert self.thermostat.connected()
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self._on_connection_changed(False)
await self.thermostat.reset()
await asyncio.sleep(0.1) # Wait for the reset to start
@ -448,7 +440,7 @@ class MainWindow(QtWidgets.QMainWindow):
await self.thermostat.set_ipv4(ipv4_settings)
await self.thermostat.end_session()
await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED)
await self._on_connection_changed(False)
async def coro_main():