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:
parent
4116962175
commit
9524601cb6
|
@ -57,30 +57,32 @@ class KirdyDataWatcher(QObject):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
async def signal_emitter(self):
|
async def signal_emitter(self):
|
||||||
try:
|
settings_summary = await self._kirdy.device.get_settings_summary()
|
||||||
settings_summary = await self._kirdy.device.get_settings_summary()
|
self.setting_update_sig.emit(settings_summary)
|
||||||
self.setting_update_sig.emit(settings_summary)
|
if self._poll_for_report:
|
||||||
if self._poll_for_report:
|
status_report = await self._kirdy.device.get_status_report()
|
||||||
status_report = await self._kirdy.device.get_status_report()
|
self.report_update_sig.emit(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)
|
|
||||||
self._kirdy.stop_report_mode()
|
|
||||||
self.connection_error_sig.emit()
|
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
while True:
|
try:
|
||||||
asyncio.ensure_future(self.signal_emitter())
|
task = asyncio.create_task(self.signal_emitter())
|
||||||
await asyncio.sleep(self._update_s)
|
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()
|
||||||
|
|
||||||
def start_watching(self):
|
def start_watching(self):
|
||||||
self._watch_task = asyncio.create_task(self.run())
|
self._watch_task = asyncio.create_task(self.run())
|
||||||
|
|
||||||
def stop_watching(self):
|
async def stop_watching(self):
|
||||||
if self._watch_task is not None:
|
if self._watch_task is not None:
|
||||||
self._watch_task.cancel()
|
self._watch_task.cancel()
|
||||||
self._watch_task = None
|
self._watch_task = None
|
||||||
|
await self.set_report_mode(False)
|
||||||
|
|
||||||
async def set_report_mode(self, enabled: bool):
|
async def set_report_mode(self, enabled: bool):
|
||||||
self._poll_for_report = not enabled
|
self._poll_for_report = not enabled
|
||||||
|
@ -89,15 +91,15 @@ class KirdyDataWatcher(QObject):
|
||||||
else:
|
else:
|
||||||
self._kirdy.stop_report_mode()
|
self._kirdy.stop_report_mode()
|
||||||
if self._report_mode_task is not None:
|
if self._report_mode_task is not None:
|
||||||
await self._report_mode_task
|
self._report_mode_task.cancel()
|
||||||
self._report_mode_task = None
|
self._report_mode_task = None
|
||||||
|
|
||||||
async def report_mode(self):
|
async def report_mode(self):
|
||||||
try:
|
try:
|
||||||
async for status_report in self._kirdy.report_mode():
|
async for status_report in self._kirdy.report_mode():
|
||||||
self.report_update_sig.emit(status_report)
|
self.report_update_sig.emit(status_report)
|
||||||
except:
|
except Exception as e:
|
||||||
logging.error("Client connection error, disconnecting", exc_info=True)
|
logging.error(f"Encountered an error: {e}, disconnecting", exc_info=True)
|
||||||
self._kirdy.stop_report_mode()
|
self._kirdy.stop_report_mode()
|
||||||
self.connection_error_sig.emit()
|
self.connection_error_sig.emit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue