Move report mode bookkeeping into ClientWatcher

This commit is contained in:
atse 2023-08-16 13:07:26 +08:00
parent 898a6891cf
commit aeecde09af
1 changed files with 17 additions and 21 deletions

View File

@ -115,6 +115,7 @@ class ClientWatcher(QObject):
self._update_s = update_s
self._client = client
self._watch_task = None
self._report_mode_task = None
self._poll_for_report = True
super().__init__(parent)
@ -146,8 +147,20 @@ class ClientWatcher(QObject):
self._watch_task.cancel()
self._watch_task = None
def set_report_polling(self, enabled: bool):
self._poll_for_report = enabled
async def set_report_mode(self, enabled: bool):
self._poll_for_report = not enabled
if enabled:
self._report_mode_task = asyncio.create_task(self.report_mode())
else:
self._client.stop_report_mode()
if self._report_mode_task is not None:
await self._report_mode_task
self._report_mode_task = None
async def report_mode(self):
async for report in self._client.report_mode():
self.report_update.emit(report)
@pyqtSlot(float)
def set_update_s(self, update_s):
@ -213,8 +226,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value())
)
self.report_mode_task = None
if args.connect:
if args.IP:
self.host_set_line.setText(args.IP)
@ -516,7 +527,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.fan_pwm_warning.setToolTip("")
self.clear_graphs()
self.report_box.setChecked(False)
await self.stop_report_mode()
await self.client_watcher.set_report_mode(False)
self.client_watcher.stop_watching()
self.status_lbl.setText("Disconnected")
@ -570,22 +581,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@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.client.stop_report_mode()
async def report_mode(self):
async for report in self.client.report_mode():
self.plot(report)
self.update_report(report)
async def stop_report_mode(self):
if self.report_mode_task is not None:
self.client.stop_report_mode()
await self.report_mode_task
self.report_mode_task = None
await self.client_watcher.set_report_mode(enabled)
@asyncClose
async def closeEvent(self, event):