2023-07-01 23:46:40 +08:00
|
|
|
from PyQt6 import QtWidgets
|
2023-06-27 17:34:39 +08:00
|
|
|
from PyQt6.QtCore import pyqtSignal, QObject, QSignalBlocker, pyqtSlot
|
2023-05-19 11:23:39 +08:00
|
|
|
from pyqtgraph import PlotWidget
|
|
|
|
from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, registerParameterType
|
|
|
|
import pyqtgraph as pg
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import logging
|
2023-06-26 10:20:48 +08:00
|
|
|
import asyncio
|
2023-06-27 17:34:39 +08:00
|
|
|
from pytec.aioclient import Client
|
|
|
|
import qasync
|
|
|
|
from qasync import asyncSlot, asyncClose
|
2023-05-19 11:23:39 +08:00
|
|
|
|
|
|
|
# pyuic6 -x tec_qt.ui -o ui_tec_qt.py
|
|
|
|
from ui_tec_qt import Ui_MainWindow
|
|
|
|
|
|
|
|
|
|
|
|
def get_argparser():
|
|
|
|
parser = argparse.ArgumentParser(description="ARTIQ master")
|
|
|
|
|
|
|
|
parser.add_argument("--connect", default=None, action="store_true",
|
|
|
|
help="Automatically connect to the specified Thermostat in IP:port format")
|
|
|
|
parser.add_argument('IP', metavar="ip", default=None, nargs='?')
|
|
|
|
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")
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2023-06-26 10:20:48 +08:00
|
|
|
class ClientWatcher(QObject):
|
2023-05-19 13:45:01 +08:00
|
|
|
fan_update = pyqtSignal(object)
|
|
|
|
pwm_update = pyqtSignal(object)
|
|
|
|
report_update = pyqtSignal(object)
|
|
|
|
pid_update = pyqtSignal(object)
|
|
|
|
|
2023-07-05 16:25:13 +08:00
|
|
|
def __init__(self, parent, client, update_s):
|
2023-05-19 13:45:01 +08:00
|
|
|
self.update_s = update_s
|
|
|
|
self.running = True
|
2023-07-05 16:25:13 +08:00
|
|
|
self.client = client
|
2023-05-19 13:45:01 +08:00
|
|
|
super().__init__(parent)
|
|
|
|
|
2023-06-26 10:20:48 +08:00
|
|
|
async def run(self):
|
2023-08-08 17:16:11 +08:00
|
|
|
loop = asyncio.get_running_loop()
|
2023-05-19 13:45:01 +08:00
|
|
|
while self.running:
|
2023-08-08 17:16:11 +08:00
|
|
|
time = loop.time()
|
2023-06-27 17:34:39 +08:00
|
|
|
await self.update_params()
|
2023-08-08 17:16:11 +08:00
|
|
|
await asyncio.sleep(self.update_s - (loop.time() - time))
|
2023-05-19 13:45:01 +08:00
|
|
|
|
2023-06-27 17:34:39 +08:00
|
|
|
async def update_params(self):
|
2023-07-05 16:25:13 +08:00
|
|
|
self.fan_update.emit(await self.client.fan())
|
2023-05-19 13:45:01 +08:00
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def stop_watching(self):
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
@pyqtSlot()
|
2023-07-01 23:46:40 +08:00
|
|
|
def set_update_s(self, update_s):
|
|
|
|
self.update_s = update_s
|
|
|
|
|
|
|
|
|
|
|
|
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
|
|
|
def __init__(self, args):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.setupUi(self)
|
|
|
|
|
|
|
|
self.connect_btn.clicked.connect(self.connect)
|
|
|
|
self.fan_power_slider.valueChanged.connect(self.fan_set)
|
|
|
|
self.fan_auto_box.stateChanged.connect(self.fan_auto_set)
|
|
|
|
|
2023-07-05 16:25:13 +08:00
|
|
|
self.tec_client: Client = None
|
2023-07-05 16:02:35 +08:00
|
|
|
self.client_watcher: ClientWatcher = None
|
2023-07-01 23:46:40 +08:00
|
|
|
self.client_watcher_task = None
|
|
|
|
|
|
|
|
if args.connect:
|
|
|
|
if args.IP:
|
|
|
|
self.ip_set_line.setText(args.IP)
|
|
|
|
if args.PORT:
|
|
|
|
self.port_set_spin.setValue(int(args.PORT))
|
|
|
|
self.connect_btn.click()
|
|
|
|
|
|
|
|
def _on_connection_changed(self, result):
|
|
|
|
self.graph_group.setEnabled(result)
|
|
|
|
self.hw_rev_lbl.setEnabled(result)
|
|
|
|
self.fan_group.setEnabled(result)
|
|
|
|
self.report_group.setEnabled(result)
|
|
|
|
|
|
|
|
self.ip_set_line.setEnabled(not result)
|
|
|
|
self.port_set_spin.setEnabled(not result)
|
|
|
|
self.status_lbl.setText("Connected" if result else "Disconnected")
|
|
|
|
self.connect_btn.setText("Disconnect" if result else "Connect")
|
|
|
|
if not result:
|
|
|
|
self.hw_rev_lbl.setText("Thermostat vX.Y")
|
|
|
|
self.fan_group.setStyleSheet("")
|
|
|
|
|
|
|
|
def _hw_rev(self, hw_rev_d: dict):
|
|
|
|
logging.debug(hw_rev_d)
|
|
|
|
self.hw_rev_lbl.setText(f"Thermostat v{hw_rev_d['rev']['major']}.{hw_rev_d['rev']['major']}")
|
|
|
|
self.fan_group.setEnabled(hw_rev_d["settings"]["fan_available"])
|
|
|
|
if hw_rev_d["settings"]["fan_pwm_recommended"]:
|
|
|
|
self.fan_group.setStyleSheet("")
|
|
|
|
self.fan_group.setToolTip("")
|
|
|
|
else:
|
|
|
|
self.fan_group.setStyleSheet("background-color: yellow")
|
|
|
|
self.fan_group.setToolTip("Changing the fan settings of not recommended")
|
|
|
|
|
|
|
|
def fan_update(self, fan_settings):
|
|
|
|
logging.debug(fan_settings)
|
|
|
|
if fan_settings is None:
|
|
|
|
return
|
2023-07-05 13:00:56 +08:00
|
|
|
with QSignalBlocker(self.fan_power_slider):
|
2023-07-01 23:46:40 +08:00
|
|
|
self.fan_power_slider.setValue(fan_settings["fan_pwm"])
|
|
|
|
self.fan_power_slider.setEnabled(not fan_settings["auto_mode"])
|
2023-07-05 13:00:56 +08:00
|
|
|
with QSignalBlocker(self.fan_auto_box):
|
2023-07-01 23:46:40 +08:00
|
|
|
self.fan_auto_box.setChecked(fan_settings["auto_mode"])
|
|
|
|
|
|
|
|
@asyncSlot()
|
|
|
|
async def fan_set(self):
|
2023-07-05 16:25:13 +08:00
|
|
|
if self.tec_client is None or self.fan_auto_box.isChecked():
|
2023-07-01 23:46:40 +08:00
|
|
|
return
|
2023-07-05 16:25:13 +08:00
|
|
|
await self.tec_client.set_param("fan", self.fan_power_slider.value())
|
2023-07-01 23:46:40 +08:00
|
|
|
|
|
|
|
@asyncSlot(int)
|
|
|
|
async def fan_auto_set(self, enabled):
|
2023-07-05 16:25:13 +08:00
|
|
|
if self.tec_client is None:
|
2023-07-01 23:46:40 +08:00
|
|
|
return
|
|
|
|
self.fan_power_slider.setEnabled(not enabled)
|
|
|
|
if enabled:
|
2023-07-05 16:25:13 +08:00
|
|
|
await self.tec_client.set_param("fan", "auto")
|
2023-06-27 17:34:39 +08:00
|
|
|
else:
|
2023-07-05 16:25:13 +08:00
|
|
|
await self.tec_client.set_param("fan", self.fan_power_slider.value())
|
2023-07-01 23:46:40 +08:00
|
|
|
|
|
|
|
@asyncSlot()
|
|
|
|
async def connect(self):
|
|
|
|
ip, port = self.ip_set_line.text(), self.port_set_spin.value()
|
|
|
|
try:
|
2023-07-05 16:25:13 +08:00
|
|
|
if self.tec_client is None:
|
2023-07-01 23:46:40 +08:00
|
|
|
self.status_lbl.setText("Connecting...")
|
2023-07-05 16:25:13 +08:00
|
|
|
self.tec_client = Client()
|
|
|
|
await self.tec_client.connect(host=ip, port=port, timeout=30)
|
2023-07-01 23:46:40 +08:00
|
|
|
self._on_connection_changed(True)
|
2023-07-05 16:25:13 +08:00
|
|
|
self._hw_rev(await self.tec_client.hw_rev())
|
|
|
|
# self.fan_update(await self.tec_client.fan())
|
2023-07-05 13:13:54 +08:00
|
|
|
|
2023-07-05 16:25:13 +08:00
|
|
|
self.client_watcher = ClientWatcher(self.main_widget, self.tec_client, self.report_refresh_spin.value())
|
2023-07-05 16:02:35 +08:00
|
|
|
self.client_watcher.fan_update.connect(self.fan_update)
|
2023-07-05 13:13:54 +08:00
|
|
|
self.report_apply_btn.clicked.connect(
|
2023-07-05 16:02:35 +08:00
|
|
|
lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value())
|
2023-07-05 13:13:54 +08:00
|
|
|
)
|
2023-07-05 16:02:35 +08:00
|
|
|
QtWidgets.QApplication.instance().aboutToQuit.connect(self.client_watcher.stop_watching)
|
|
|
|
self.client_watcher_task = asyncio.create_task(self.client_watcher.run())
|
2023-07-05 10:24:36 +08:00
|
|
|
else:
|
2023-07-05 16:25:13 +08:00
|
|
|
await self.tec_client.disconnect()
|
|
|
|
self.tec_client = None
|
2023-07-05 10:24:36 +08:00
|
|
|
self._on_connection_changed(False)
|
2023-07-05 13:13:54 +08:00
|
|
|
|
2023-07-05 16:02:35 +08:00
|
|
|
self.client_watcher.stop_watching()
|
|
|
|
self.client_watcher = None
|
2023-07-05 13:13:54 +08:00
|
|
|
await self.client_watcher_task
|
|
|
|
self.client_watcher_task = None
|
2023-07-01 23:46:40 +08:00
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"Failed communicating to the {ip}:{port}: {e}")
|
|
|
|
self._on_connection_changed(False)
|
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)
|
2023-06-26 10:20:48 +08:00
|
|
|
|
2023-07-01 23:46:40 +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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|