2024-09-09 15:09:04 +08:00
|
|
|
"""GUI for the Sinara 8451 Thermostat"""
|
|
|
|
|
2024-09-09 15:08:17 +08:00
|
|
|
import json
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import argparse
|
|
|
|
import importlib.resources
|
|
|
|
import qasync
|
|
|
|
from qasync import asyncSlot, asyncClose
|
|
|
|
from autotune import PIDAutotuneState
|
|
|
|
from PyQt6 import QtWidgets, QtGui, uic
|
|
|
|
from PyQt6.QtCore import pyqtSlot
|
|
|
|
from pytec.gui.model.thermostat import Thermostat, ThermostatConnectionState
|
|
|
|
from pytec.gui.model.pid_autotuner import PIDAutoTuner
|
2024-09-09 15:06:22 +08:00
|
|
|
from pytec.gui.view.zero_limits_warning_view import ZeroLimitsWarningView
|
|
|
|
from pytec.gui.view.thermostat_settings_menu import ThermostatSettingsMenu
|
|
|
|
from pytec.gui.view.connection_details_menu import ConnectionDetailsMenu
|
2024-07-10 14:52:09 +08:00
|
|
|
from pytec.gui.view.plot_options_menu import PlotOptionsMenu
|
|
|
|
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
|
2023-05-19 11:23:39 +08:00
|
|
|
|
|
|
|
|
2024-05-13 10:35:21 +08:00
|
|
|
def get_argparser():
|
|
|
|
parser = argparse.ArgumentParser(description="Thermostat Control Panel")
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--connect",
|
|
|
|
default=None,
|
|
|
|
action="store_true",
|
2024-08-23 15:43:02 +08:00
|
|
|
help="Automatically connect to the specified Thermostat in host:port format",
|
2024-05-13 10:35:21 +08:00
|
|
|
)
|
2024-08-23 15:43:02 +08:00
|
|
|
parser.add_argument("HOST", metavar="host", default=None, nargs="?")
|
2024-05-13 10:35:21 +08:00
|
|
|
parser.add_argument("PORT", metavar="port", default=None, nargs="?")
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--log",
|
|
|
|
dest="logLevel",
|
|
|
|
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
|
|
|
help="Set the logging level",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--param_tree",
|
2024-07-10 14:52:09 +08:00
|
|
|
default=importlib.resources.files("pytec.gui.view").joinpath("param_tree.json"),
|
2024-05-13 10:35:21 +08:00
|
|
|
help="Param Tree Description JSON File",
|
|
|
|
)
|
2023-05-19 11:23:39 +08:00
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2024-05-13 10:35:21 +08:00
|
|
|
class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
NUM_CHANNELS = 2
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-05-13 10:35:21 +08:00
|
|
|
def __init__(self, args):
|
2024-08-28 17:22:59 +08:00
|
|
|
super().__init__()
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-07-10 14:52:09 +08:00
|
|
|
ui_file_path = importlib.resources.files("pytec.gui.view").joinpath("tec_qt.ui")
|
2024-05-13 10:35:21 +08:00
|
|
|
uic.loadUi(ui_file_path, self)
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-09-09 15:00:44 +08:00
|
|
|
self._info_box = InfoBox()
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-08-28 17:20:14 +08:00
|
|
|
# Models
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat = Thermostat(self, self.report_refresh_spin.value())
|
2024-08-21 18:10:19 +08:00
|
|
|
self._connecting_task = None
|
2024-09-09 12:03:19 +08:00
|
|
|
self._thermostat.connection_state_update.connect(
|
|
|
|
self._on_connection_state_changed
|
|
|
|
)
|
2024-06-20 15:25:04 +08:00
|
|
|
|
2024-09-09 12:01:42 +08:00
|
|
|
self._autotuners = PIDAutoTuner(self, self._thermostat, 2)
|
2024-09-09 12:03:19 +08:00
|
|
|
self._autotuners.autotune_state_changed.connect(
|
|
|
|
self._on_pid_autotune_state_changed
|
|
|
|
)
|
2024-08-28 17:20:14 +08:00
|
|
|
|
|
|
|
# Handlers for disconnections
|
2024-08-28 17:06:17 +08:00
|
|
|
async def autotune_disconnect():
|
|
|
|
for ch in range(self.NUM_CHANNELS):
|
2024-09-09 12:01:42 +08:00
|
|
|
if self._autotuners.get_state(ch) != PIDAutotuneState.STATE_OFF:
|
|
|
|
await self._autotuners.stop_pid_from_running(ch)
|
2024-09-09 15:10:30 +08:00
|
|
|
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat.disconnect_cb = autotune_disconnect
|
2024-08-28 17:06:17 +08:00
|
|
|
|
2024-08-29 12:11:49 +08:00
|
|
|
@pyqtSlot()
|
|
|
|
def handle_connection_error():
|
2024-09-09 15:00:44 +08:00
|
|
|
self._info_box.display_info_box(
|
2024-06-20 15:25:04 +08:00
|
|
|
"Connection Error", "Thermostat connection lost. Is it unplugged?"
|
|
|
|
)
|
2024-09-09 15:10:30 +08:00
|
|
|
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat.connection_error.connect(handle_connection_error)
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-08-28 17:20:14 +08:00
|
|
|
# Control Panel
|
2024-05-13 10:35:21 +08:00
|
|
|
def get_ctrl_panel_config(args):
|
2024-08-14 11:54:15 +08:00
|
|
|
with open(args.param_tree, "r", encoding="utf-8") as f:
|
2024-05-13 10:35:21 +08:00
|
|
|
return json.load(f)["ctrl_panel"]
|
|
|
|
|
2024-09-09 12:01:42 +08:00
|
|
|
self._ctrl_panel_view = CtrlPanel(
|
|
|
|
self._thermostat,
|
|
|
|
self._autotuners,
|
2024-09-09 15:00:44 +08:00
|
|
|
self._info_box,
|
2024-05-13 10:35:21 +08:00
|
|
|
[self.ch0_tree, self.ch1_tree],
|
|
|
|
get_ctrl_panel_config(args),
|
|
|
|
)
|
2024-06-28 10:02:43 +08:00
|
|
|
|
2024-08-28 17:20:14 +08:00
|
|
|
# Graphs
|
2024-09-09 12:01:42 +08:00
|
|
|
self._channel_graphs = LiveDataPlotter(
|
|
|
|
self._thermostat,
|
2024-05-13 10:35:21 +08:00
|
|
|
[
|
|
|
|
[getattr(self, f"ch{ch}_t_graph"), getattr(self, f"ch{ch}_i_graph")]
|
|
|
|
for ch in range(self.NUM_CHANNELS)
|
2024-09-09 15:10:30 +08:00
|
|
|
],
|
2024-05-13 10:35:21 +08:00
|
|
|
)
|
2023-06-30 11:27:31 +08:00
|
|
|
|
2024-08-28 17:20:14 +08:00
|
|
|
# Bottom bar menus
|
2024-09-09 15:06:22 +08:00
|
|
|
self.connection_details_menu = ConnectionDetailsMenu(
|
|
|
|
self._thermostat, self.connect_btn
|
|
|
|
)
|
|
|
|
self.connect_btn.setMenu(self.connection_details_menu)
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-09-09 15:06:22 +08:00
|
|
|
self._thermostat_settings_menu = ThermostatSettingsMenu(
|
2024-09-09 15:00:44 +08:00
|
|
|
self._thermostat, self._info_box, self.style()
|
2024-08-27 17:00:16 +08:00
|
|
|
)
|
2024-09-09 15:06:22 +08:00
|
|
|
self.thermostat_settings.setMenu(self._thermostat_settings_menu)
|
2023-08-16 17:35:13 +08:00
|
|
|
|
2024-09-09 12:01:42 +08:00
|
|
|
self._plot_options_menu = PlotOptionsMenu(self._channel_graphs)
|
|
|
|
self.plot_settings.setMenu(self._plot_options_menu)
|
2024-08-28 17:20:14 +08:00
|
|
|
|
|
|
|
# Status line
|
2024-09-09 15:06:22 +08:00
|
|
|
self._zero_limits_warning_view = ZeroLimitsWarningView(
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat, self.style(), self.limits_warning
|
2024-08-28 17:20:14 +08:00
|
|
|
)
|
2023-08-16 17:35:13 +08:00
|
|
|
self.loading_spinner.hide()
|
|
|
|
|
2024-08-28 17:20:14 +08:00
|
|
|
self.report_apply_btn.clicked.connect(
|
2024-09-09 12:01:42 +08:00
|
|
|
lambda: self._thermostat.set_update_s(self.report_refresh_spin.value())
|
2024-08-28 17:20:14 +08:00
|
|
|
)
|
|
|
|
|
2024-08-29 11:26:11 +08:00
|
|
|
@asyncClose
|
|
|
|
async def closeEvent(self, _event):
|
|
|
|
try:
|
2024-09-09 12:01:42 +08:00
|
|
|
await self._thermostat.end_session()
|
|
|
|
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
|
2024-08-29 11:26:11 +08:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2024-08-29 12:51:18 +08:00
|
|
|
@pyqtSlot(ThermostatConnectionState)
|
2024-09-09 12:03:19 +08:00
|
|
|
def _on_connection_state_changed(self, state):
|
2024-08-28 10:24:37 +08:00
|
|
|
self.graph_group.setEnabled(state == ThermostatConnectionState.CONNECTED)
|
2024-08-28 10:24:25 +08:00
|
|
|
self.thermostat_settings.setEnabled(
|
2024-08-28 10:24:37 +08:00
|
|
|
state == ThermostatConnectionState.CONNECTED
|
2024-08-28 10:24:25 +08:00
|
|
|
)
|
2024-08-29 11:47:39 +08:00
|
|
|
self.report_group.setEnabled(state == ThermostatConnectionState.CONNECTED)
|
2024-08-28 10:24:25 +08:00
|
|
|
|
2024-08-28 10:24:37 +08:00
|
|
|
match state:
|
2024-08-22 17:11:56 +08:00
|
|
|
case ThermostatConnectionState.CONNECTED:
|
|
|
|
self.connect_btn.setText("Disconnect")
|
2024-08-27 18:01:27 +08:00
|
|
|
self.status_lbl.setText(
|
2024-08-28 16:32:31 +08:00
|
|
|
"Connected to Thermostat v"
|
2024-09-09 12:01:42 +08:00
|
|
|
f"{self._thermostat.hw_rev['rev']['major']}."
|
|
|
|
f"{self._thermostat.hw_rev['rev']['minor']}"
|
2024-08-27 18:01:27 +08:00
|
|
|
)
|
2024-08-22 17:11:56 +08:00
|
|
|
|
|
|
|
case ThermostatConnectionState.CONNECTING:
|
|
|
|
self.connect_btn.setText("Stop")
|
2024-08-28 10:50:48 +08:00
|
|
|
self.status_lbl.setText("Connecting...")
|
2024-08-22 17:11:56 +08:00
|
|
|
|
|
|
|
case ThermostatConnectionState.DISCONNECTED:
|
|
|
|
self.connect_btn.setText("Connect")
|
|
|
|
self.status_lbl.setText("Disconnected")
|
2023-06-30 11:27:31 +08:00
|
|
|
|
2024-08-29 12:51:18 +08:00
|
|
|
@pyqtSlot(int, PIDAutotuneState)
|
2024-09-09 12:03:19 +08:00
|
|
|
def _on_pid_autotune_state_changed(self, _ch, _state):
|
2024-09-09 15:10:16 +08:00
|
|
|
autotuning_channels = []
|
2024-08-29 11:26:11 +08:00
|
|
|
for ch in range(self.NUM_CHANNELS):
|
2024-09-09 12:01:42 +08:00
|
|
|
if self._autotuners.get_state(ch) in {
|
2024-08-29 11:26:11 +08:00
|
|
|
PIDAutotuneState.STATE_READY,
|
|
|
|
PIDAutotuneState.STATE_RELAY_STEP_UP,
|
|
|
|
PIDAutotuneState.STATE_RELAY_STEP_DOWN,
|
|
|
|
}:
|
2024-09-09 15:10:16 +08:00
|
|
|
autotuning_channels.append(ch)
|
2023-06-30 11:27:31 +08:00
|
|
|
|
2024-09-09 15:10:16 +08:00
|
|
|
if len(autotuning_channels) == 0:
|
2024-08-29 11:26:11 +08:00
|
|
|
self.background_task_lbl.setText("Ready.")
|
|
|
|
self.loading_spinner.hide()
|
|
|
|
self.loading_spinner.stop()
|
|
|
|
else:
|
|
|
|
self.background_task_lbl.setText(
|
2024-09-09 15:10:16 +08:00
|
|
|
f"Autotuning channel {autotuning_channels}..."
|
2024-08-29 11:26:11 +08:00
|
|
|
)
|
|
|
|
self.loading_spinner.start()
|
|
|
|
self.loading_spinner.show()
|
2023-06-30 11:27:31 +08:00
|
|
|
|
|
|
|
@asyncSlot()
|
|
|
|
async def on_connect_btn_clicked(self):
|
2024-09-09 12:01:42 +08:00
|
|
|
match self._thermostat.connection_state:
|
2024-09-09 11:41:17 +08:00
|
|
|
case ThermostatConnectionState.DISCONNECTED:
|
|
|
|
self._connecting_task = asyncio.create_task(
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat.start_session(
|
2024-09-09 15:06:22 +08:00
|
|
|
host=self.connection_details_menu.host_set_line.text(),
|
|
|
|
port=self.connection_details_menu.port_set_spin.value(),
|
2024-09-09 11:41:17 +08:00
|
|
|
)
|
|
|
|
)
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat.connection_state = ThermostatConnectionState.CONNECTING
|
2024-09-09 11:42:12 +08:00
|
|
|
await self._connecting_task
|
|
|
|
self._connecting_task = None
|
2024-09-09 12:01:42 +08:00
|
|
|
self._thermostat.connection_state = ThermostatConnectionState.CONNECTED
|
|
|
|
self._thermostat.start_watching()
|
2024-09-09 11:41:17 +08:00
|
|
|
|
|
|
|
case ThermostatConnectionState.CONNECTING:
|
|
|
|
self._connecting_task.cancel()
|
2024-09-09 14:52:33 +08:00
|
|
|
self._connecting_task = None
|
2024-09-09 11:42:12 +08:00
|
|
|
await self._thermostat.end_session()
|
|
|
|
self._thermostat.connection_state = (
|
2024-09-09 11:41:17 +08:00
|
|
|
ThermostatConnectionState.DISCONNECTED
|
2024-08-21 18:10:19 +08:00
|
|
|
)
|
2023-06-30 11:27:31 +08:00
|
|
|
|
2024-09-09 11:41:17 +08:00
|
|
|
case ThermostatConnectionState.CONNECTED:
|
2024-09-09 12:01:42 +08:00
|
|
|
await self._thermostat.end_session()
|
|
|
|
self._thermostat.connection_state = (
|
2024-09-09 11:41:17 +08:00
|
|
|
ThermostatConnectionState.DISCONNECTED
|
|
|
|
)
|
2023-06-27 17:34:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def coro_main():
|
2023-05-19 11:23:39 +08:00
|
|
|
args = get_argparser().parse_args()
|
|
|
|
if args.logLevel:
|
|
|
|
logging.basicConfig(level=getattr(logging, args.logLevel))
|
|
|
|
|
2023-06-27 17:34:39 +08:00
|
|
|
app_quit_event = asyncio.Event()
|
2023-06-26 10:20:48 +08:00
|
|
|
|
2023-06-27 17:34:39 +08:00
|
|
|
app = QtWidgets.QApplication.instance()
|
|
|
|
app.aboutToQuit.connect(app_quit_event.set)
|
2024-05-13 10:35:21 +08:00
|
|
|
app.setWindowIcon(
|
2024-07-03 14:40:13 +08:00
|
|
|
QtGui.QIcon(
|
|
|
|
str(importlib.resources.files("pytec.gui.resources").joinpath("artiq.ico"))
|
|
|
|
)
|
2024-05-13 10:35:21 +08:00
|
|
|
)
|
2023-06-26 10:20:48 +08:00
|
|
|
|
2023-06-30 11:27:31 +08:00
|
|
|
main_window = MainWindow(args)
|
2023-05-19 11:23:39 +08:00
|
|
|
main_window.show()
|
2023-06-26 10:20:48 +08:00
|
|
|
|
2024-08-29 10:36:18 +08:00
|
|
|
if args.connect:
|
|
|
|
if args.HOST:
|
2024-09-09 15:06:22 +08:00
|
|
|
main_window.connection_details_menu.host_set_line.setText(args.HOST)
|
2024-08-29 10:36:18 +08:00
|
|
|
if args.PORT:
|
2024-09-09 15:06:22 +08:00
|
|
|
main_window.connection_details_menu.port_set_spin.setValue(int(args.PORT))
|
2024-08-29 10:36:18 +08:00
|
|
|
main_window.connect_btn.click()
|
|
|
|
|
2023-06-27 17:34:39 +08:00
|
|
|
await app_quit_event.wait()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
qasync.run(coro_main())
|
2023-05-19 11:23:39 +08:00
|
|
|
|
|
|
|
|
2024-05-13 10:35:21 +08:00
|
|
|
if __name__ == "__main__":
|
2023-05-19 11:23:39 +08:00
|
|
|
main()
|