1
0
forked from M-Labs/kirdy

gui: Improve DataWatcher operation

- Ensure there is only one instance of signal_emitter() task
- calling stop_watching should stop report_task
This commit is contained in:
linuswck 2024-04-19 13:25:00 +08:00
parent 4116962175
commit 9524601cb6

View File

@ -57,30 +57,32 @@ class KirdyDataWatcher(QObject):
super().__init__(parent)
async def signal_emitter(self):
settings_summary = await self._kirdy.device.get_settings_summary()
self.setting_update_sig.emit(settings_summary)
if self._poll_for_report:
status_report = await self._kirdy.device.get_status_report()
self.report_update_sig.emit(status_report)
async def run(self):
try:
settings_summary = await self._kirdy.device.get_settings_summary()
self.setting_update_sig.emit(settings_summary)
if self._poll_for_report:
status_report = await self._kirdy.device.get_status_report()
self.report_update_sig.emit(status_report)
# TODO: Identify the possible types of error that is connection related
except:
logging.error("Client connection error, disconnecting", exc_info=True)
task = asyncio.create_task(self.signal_emitter())
while True:
if task.done():
task = asyncio.create_task(self.signal_emitter())
await asyncio.sleep(self._update_s)
except Exception as e:
logging.error(f"Encountered an error: {e}. disconnecting.", exc_info=True)
self._kirdy.stop_report_mode()
self.connection_error_sig.emit()
async def run(self):
while True:
asyncio.ensure_future(self.signal_emitter())
await asyncio.sleep(self._update_s)
def start_watching(self):
self._watch_task = asyncio.create_task(self.run())
def stop_watching(self):
async def stop_watching(self):
if self._watch_task is not None:
self._watch_task.cancel()
self._watch_task = None
await self.set_report_mode(False)
async def set_report_mode(self, enabled: bool):
self._poll_for_report = not enabled
@ -89,15 +91,15 @@ class KirdyDataWatcher(QObject):
else:
self._kirdy.stop_report_mode()
if self._report_mode_task is not None:
await self._report_mode_task
self._report_mode_task.cancel()
self._report_mode_task = None
async def report_mode(self):
try:
async for status_report in self._kirdy.report_mode():
self.report_update_sig.emit(status_report)
except:
logging.error("Client connection error, disconnecting", exc_info=True)
except Exception as e:
logging.error(f"Encountered an error: {e}, disconnecting", exc_info=True)
self._kirdy.stop_report_mode()
self.connection_error_sig.emit()