forked from M-Labs/thermostat
Report mode functionality
This commit is contained in:
parent
728bce38b6
commit
64891231cd
@ -11,6 +11,7 @@ class Client:
|
||||
self._writer = None
|
||||
self._connecting_task = None
|
||||
self._command_lock = asyncio.Lock()
|
||||
self._report_mode_on = False
|
||||
|
||||
async def connect(self, host='192.168.1.26', port=23, timeout=None):
|
||||
"""Connect to the TEC with host and port, throws TimeoutError if
|
||||
@ -167,9 +168,11 @@ class Client:
|
||||
'pid_output': 2.067581958092247}
|
||||
"""
|
||||
await self._command("report mode", "on")
|
||||
self._report_mode_on = True
|
||||
|
||||
while True:
|
||||
line = await self._read_line()
|
||||
while self._report_mode_on:
|
||||
async with self._command_lock:
|
||||
line = await self._read_line()
|
||||
if not line:
|
||||
break
|
||||
try:
|
||||
@ -177,6 +180,11 @@ class Client:
|
||||
except json.decoder.JSONDecodeError:
|
||||
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=""):
|
||||
"""Set configuration parameters
|
||||
|
||||
|
@ -95,6 +95,7 @@ class ClientWatcher(QObject):
|
||||
self.update_s = update_s
|
||||
self.client = client
|
||||
self.watch_task = None
|
||||
self.poll_for_report = True
|
||||
super().__init__(parent)
|
||||
|
||||
async def run(self):
|
||||
@ -107,7 +108,8 @@ class ClientWatcher(QObject):
|
||||
async def update_params(self):
|
||||
self.fan_update.emit(await self.client.fan())
|
||||
self.pwm_update.emit(await self.client.get_pwm())
|
||||
self.report_update.emit(await self.client._command("report"))
|
||||
if self.poll_for_report:
|
||||
self.report_update.emit(await self.client._command("report"))
|
||||
self.pid_update.emit(await self.client.get_pid())
|
||||
self.thermistor_update.emit(await self.client.get_steinhart_hart())
|
||||
self.postfilter_update.emit(await self.client.get_postfilter())
|
||||
@ -124,6 +126,9 @@ class ClientWatcher(QObject):
|
||||
self.watch_task.cancel()
|
||||
self.watch_task = None
|
||||
|
||||
def set_report_polling(self, enabled: bool):
|
||||
self.poll_for_report = enabled
|
||||
|
||||
@pyqtSlot(float)
|
||||
def set_update_s(self, 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())
|
||||
)
|
||||
|
||||
self.report_mode_task = None
|
||||
|
||||
if args.connect:
|
||||
if 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.setToolTip("")
|
||||
self.clear_graphs()
|
||||
self.report_box.setChecked(False)
|
||||
await self.stop_report_mode()
|
||||
self.client_watcher.stop_watching()
|
||||
|
||||
def _set_fan_pwm_warning(self):
|
||||
@ -316,8 +325,29 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
else:
|
||||
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
|
||||
async def closeEvent(self, event):
|
||||
await self.stop_report_mode()
|
||||
self.client_watcher.stop_watching()
|
||||
await self.tec_client.disconnect()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user