Report mode functionality

This commit is contained in:
atse 2023-07-26 09:47:24 +08:00
parent 728bce38b6
commit 64891231cd
2 changed files with 41 additions and 3 deletions

View File

@ -11,6 +11,7 @@ class Client:
self._writer = None self._writer = None
self._connecting_task = None self._connecting_task = None
self._command_lock = asyncio.Lock() self._command_lock = asyncio.Lock()
self._report_mode_on = False
async def connect(self, host='192.168.1.26', port=23, timeout=None): async def connect(self, host='192.168.1.26', port=23, timeout=None):
"""Connect to the TEC with host and port, throws TimeoutError if """Connect to the TEC with host and port, throws TimeoutError if
@ -167,8 +168,10 @@ class Client:
'pid_output': 2.067581958092247} 'pid_output': 2.067581958092247}
""" """
await self._command("report mode", "on") await self._command("report mode", "on")
self._report_mode_on = True
while True: while self._report_mode_on:
async with self._command_lock:
line = await self._read_line() line = await self._read_line()
if not line: if not line:
break break
@ -177,6 +180,11 @@ class Client:
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
pass pass
await self._command("report mode", "off")
def stop_report_mode(self):
self._report_mode_on = False
async def set_param(self, topic, channel, field="", value=""): async def set_param(self, topic, channel, field="", value=""):
"""Set configuration parameters """Set configuration parameters

View File

@ -95,6 +95,7 @@ class ClientWatcher(QObject):
self.update_s = update_s self.update_s = update_s
self.client = client self.client = client
self.watch_task = None self.watch_task = None
self.poll_for_report = True
super().__init__(parent) super().__init__(parent)
async def run(self): async def run(self):
@ -107,6 +108,7 @@ class ClientWatcher(QObject):
async def update_params(self): async def update_params(self):
self.fan_update.emit(await self.client.fan()) self.fan_update.emit(await self.client.fan())
self.pwm_update.emit(await self.client.get_pwm()) self.pwm_update.emit(await self.client.get_pwm())
if self.poll_for_report:
self.report_update.emit(await self.client._command("report")) self.report_update.emit(await self.client._command("report"))
self.pid_update.emit(await self.client.get_pid()) self.pid_update.emit(await self.client.get_pid())
self.thermistor_update.emit(await self.client.get_steinhart_hart()) self.thermistor_update.emit(await self.client.get_steinhart_hart())
@ -124,6 +126,9 @@ class ClientWatcher(QObject):
self.watch_task.cancel() self.watch_task.cancel()
self.watch_task = None self.watch_task = None
def set_report_polling(self, enabled: bool):
self.poll_for_report = enabled
@pyqtSlot(float) @pyqtSlot(float)
def set_update_s(self, update_s): def set_update_s(self, update_s):
self.update_s = update_s self.update_s = update_s
@ -175,6 +180,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value()) lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value())
) )
self.report_mode_task = None
if args.connect: if args.connect:
if args.IP: if args.IP:
self.ip_set_line.setText(args.IP) self.ip_set_line.setText(args.IP)
@ -265,6 +272,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.fan_pwm_warning.setPixmap(QtGui.QPixmap()) self.fan_pwm_warning.setPixmap(QtGui.QPixmap())
self.fan_pwm_warning.setToolTip("") self.fan_pwm_warning.setToolTip("")
self.clear_graphs() self.clear_graphs()
self.report_box.setChecked(False)
await self.stop_report_mode()
self.client_watcher.stop_watching() self.client_watcher.stop_watching()
def _set_fan_pwm_warning(self): def _set_fan_pwm_warning(self):
@ -316,8 +325,29 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
else: else:
await self.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 on_report_box_stateChanged(self, enabled):
self.client_watcher.set_report_polling(not enabled)
if enabled:
self.report_mode_task = asyncio.create_task(self.report_mode())
else:
self.tec_client.stop_report_mode()
await self.report_mode_task
self.report_mode_task = None
async def report_mode(self):
async for report in self.tec_client.report_mode():
self.client_watcher.report_update.emit(report)
async def stop_report_mode(self):
if self.report_mode_task is not None:
self.tec_client.stop_report_mode()
await self.report_mode_task
self.report_mode_task = None
@asyncClose @asyncClose
async def closeEvent(self, event): async def closeEvent(self, event):
await self.stop_report_mode()
self.client_watcher.stop_watching() self.client_watcher.stop_watching()
await self.tec_client.disconnect() await self.tec_client.disconnect()