Get rid of global client

This commit is contained in:
atse 2023-07-05 16:25:13 +08:00
parent e33f8430f2
commit 3544f1ebdf
1 changed files with 17 additions and 21 deletions

View File

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