diff --git a/src/pupil_labs/realtime_api/device.py b/src/pupil_labs/realtime_api/device.py index e3a20a5..f4b7449 100644 --- a/src/pupil_labs/realtime_api/device.py +++ b/src/pupil_labs/realtime_api/device.py @@ -4,6 +4,7 @@ import logging import types import typing as T +from uuid import UUID import aiohttp import numpy as np @@ -182,6 +183,8 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"): format in TemplateDataFormat.__args__ ), f"format should be one of {TemplateDataFormat}" + self.template_definition = await self.get_template() + async with self.session.get(self.api_url(APIPath.TEMPLATE_DATA)) as response: confirmation = await response.json() if response.status != 200: @@ -193,8 +196,9 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"): if format == "api": return result elif format == "simple": - template = await self.get_template() - return template.convert_from_api_to_simple_format(result) + return self.template_definition.convert_from_api_to_simple_format( + result + ) async def post_template_data( self, @@ -248,6 +252,28 @@ async def post_template_data( logger.debug(f"[{self}.get_template_data] Send data's template: {result}") return result + async def get_question_by_id(self, question_id: T.Union[str, UUID]): + item = ( + self.template_definition.get_question_by_id(question_id) + if self.template_definition + else None + ) + + if item: + return item + + old_template = self.template_definition if self.template_definition else None + + await self.get_template() + + if ( + self.template_definition is not None + and self.template_definition is not old_template + ): + return self.template_definition.get_question_by_id(question_id) + + return None + async def close(self): await self.session.close() self.session = None diff --git a/src/pupil_labs/realtime_api/simple/device.py b/src/pupil_labs/realtime_api/simple/device.py index 9872eae..ca1434b 100644 --- a/src/pupil_labs/realtime_api/simple/device.py +++ b/src/pupil_labs/realtime_api/simple/device.py @@ -4,6 +4,7 @@ import threading import typing as T import weakref +from uuid import UUID try: from typing import Literal @@ -243,6 +244,13 @@ async def _post_template_data(): return asyncio.run(_post_template_data()) + def get_question_by_id(self, question_id: T.Union[str, UUID]): + async def _get_question_by_id(): + async with _DeviceAsync.convert_from(self) as control: + return await control.get_question_by_id(question_id) + + return asyncio.run(_get_question_by_id()) + def receive_scene_video_frame( self, timeout_seconds: T.Optional[float] = None ) -> T.Optional[SimpleVideoFrame]: