Skip to content

Commit

Permalink
Plumb timeouts through API calls (#142)
Browse files Browse the repository at this point in the history
My tests in greeneye_monitor for the case where a device doesn't respond to API calls take forever to run.
Plumbing a timeout override into the API calls lets me fix that.
  • Loading branch information
jkeljo authored May 14, 2023
1 parent 897fab5 commit 35a4040
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions siobrultech_protocols/gem/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ async def call_api(
api: ApiCall[T, R],
protocol: BidirectionalProtocol,
serial_number: Optional[int] = None,
timeout: timedelta = TIMEOUT,
timeout: Optional[timedelta] = None,
) -> AsyncIterator[Callable[[T], Coroutine[Any, None, R]]]:
if timeout is None:
timeout = TIMEOUT

async def send(arg: T) -> R:
future = asyncio.get_event_loop().create_future()
protocol.invoke_api(api, arg, future)
Expand Down Expand Up @@ -109,9 +112,11 @@ def parse_ecm_serial_number_from_settings(binary: bytes) -> int | None:


async def get_serial_number(
protocol: BidirectionalProtocol, serial_number: Optional[int] = None
protocol: BidirectionalProtocol,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> int:
async with call_api(GET_SERIAL_NUMBER, protocol, serial_number) as f:
async with call_api(GET_SERIAL_NUMBER, protocol, serial_number, timeout) as f:
return await f(None)


Expand Down Expand Up @@ -150,47 +155,59 @@ async def get_serial_number(


async def set_date_and_time(
protocol: BidirectionalProtocol, time: datetime, serial_number: Optional[int] = None
protocol: BidirectionalProtocol,
time: datetime,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> bool:
async with call_api(SET_DATE_AND_TIME, protocol, serial_number) as f:
async with call_api(SET_DATE_AND_TIME, protocol, serial_number, timeout) as f:
return await f(time)


async def set_packet_format(
protocol: BidirectionalProtocol,
format: PacketFormatType,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> bool:
async with call_api(SET_PACKET_FORMAT, protocol, serial_number) as f:
async with call_api(SET_PACKET_FORMAT, protocol, serial_number, timeout) as f:
return await f(format)


async def set_packet_send_interval(
protocol: BidirectionalProtocol,
send_interval_seconds: int,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> bool:
if send_interval_seconds < 0 or send_interval_seconds > 256:
raise ValueError("send_interval must be a postive number no greater than 256")
async with call_api(SET_PACKET_SEND_INTERVAL, protocol, serial_number) as f:
async with call_api(
SET_PACKET_SEND_INTERVAL, protocol, serial_number, timeout
) as f:
return await f(send_interval_seconds)


async def set_secondary_packet_format(
protocol: BidirectionalProtocol,
format: PacketFormatType,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> bool:
async with call_api(SET_SECONDARY_PACKET_FORMAT, protocol, serial_number) as f:
async with call_api(
SET_SECONDARY_PACKET_FORMAT, protocol, serial_number, timeout
) as f:
return await f(format)


async def synchronize_time(
protocol: BidirectionalProtocol, serial_number: Optional[int] = None
protocol: BidirectionalProtocol,
serial_number: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> bool:
"""
Synchronizes the clock on the device to the time on the local device, accounting for the
time waited for packets to clear.
"""
time = datetime.now() + protocol.packet_delay_clear_time
return await set_date_and_time(protocol, time, serial_number)
return await set_date_and_time(protocol, time, serial_number, timeout)

0 comments on commit 35a4040

Please sign in to comment.