Get rid of global client

This commit is contained in:
atse 2023-07-05 16:25:13 +08:00
parent e33f8430f2
commit 3544f1ebdf

View File

@ -14,8 +14,6 @@ from qasync import asyncSlot, asyncClose
# pyuic6 -x tec_qt.ui -o ui_tec_qt.py
from ui_tec_qt import Ui_MainWindow
tec_client: Client = None
def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ master")
@ -36,9 +34,10 @@ class ClientWatcher(QObject):
report_update = pyqtSignal(object)
pid_update = pyqtSignal(object)
def __init__(self, parent, update_s):
def __init__(self, parent, client, update_s):
self.update_s = update_s
self.running = True
self.client = client
super().__init__(parent)
async def run(self):
@ -49,7 +48,7 @@ class ClientWatcher(QObject):
await asyncio.sleep(self.update_s - (loop.time() - time))
async def update_params(self):
self.fan_update.emit(await tec_client.fan())
self.fan_update.emit(await self.client.fan())
@pyqtSlot()
def stop_watching(self):
@ -70,6 +69,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.fan_power_slider.valueChanged.connect(self.fan_set)
self.fan_auto_box.stateChanged.connect(self.fan_auto_set)
self.tec_client: Client = None
self.client_watcher: ClientWatcher = None
self.client_watcher_task = None
@ -117,37 +117,33 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@asyncSlot()
async def fan_set(self):
global tec_client
if tec_client is None or self.fan_auto_box.isChecked():
if self.tec_client is None or self.fan_auto_box.isChecked():
return
await tec_client.set_param("fan", self.fan_power_slider.value())
await self.tec_client.set_param("fan", self.fan_power_slider.value())
@asyncSlot(int)
async def fan_auto_set(self, enabled):
global tec_client
if tec_client is None:
if self.tec_client is None:
return
self.fan_power_slider.setEnabled(not enabled)
if enabled:
await tec_client.set_param("fan", "auto")
await self.tec_client.set_param("fan", "auto")
else:
await tec_client.set_param("fan", self.fan_power_slider.value())
await self.tec_client.set_param("fan", self.fan_power_slider.value())
@asyncSlot()
async def connect(self):
global tec_client
ip, port = self.ip_set_line.text(), self.port_set_spin.value()
try:
if tec_client is None:
if self.tec_client is None:
self.status_lbl.setText("Connecting...")
tec_client = Client()
await tec_client.connect(host=ip, port=port, timeout=30)
self.tec_client = Client()
await self.tec_client.connect(host=ip, port=port, timeout=30)
self._on_connection_changed(True)
self._hw_rev(await tec_client.hw_rev())
# self.fan_update(await tec_client.fan())
self._hw_rev(await self.tec_client.hw_rev())
# self.fan_update(await self.tec_client.fan())
self.client_watcher = ClientWatcher(self.main_widget, self.report_refresh_spin.value())
self.client_watcher = ClientWatcher(self.main_widget, self.tec_client, self.report_refresh_spin.value())
self.client_watcher.fan_update.connect(self.fan_update)
self.report_apply_btn.clicked.connect(
lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value())
@ -155,8 +151,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
QtWidgets.QApplication.instance().aboutToQuit.connect(self.client_watcher.stop_watching)
self.client_watcher_task = asyncio.create_task(self.client_watcher.run())
else:
await tec_client.disconnect()
tec_client = None
await self.tec_client.disconnect()
self.tec_client = None
self._on_connection_changed(False)
self.client_watcher.stop_watching()