Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Let wait_for_ecu() recognize the cli --timeout value #174

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/gallia/uds/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UDSClient:
def __init__(
self,
transport: BaseTransport,
timeout: Optional[float] = None,
timeout: float,
max_retry: int = 1,
):
self.transport = transport
Expand Down
29 changes: 15 additions & 14 deletions src/gallia/uds/ecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ECU(UDSClient):
def __init__(
self,
transport: BaseTransport,
timeout: Optional[float] = None,
timeout: float,
max_retry: int = 1,
power_supply: Optional[PowerSupply] = None,
) -> None:
Expand Down Expand Up @@ -311,24 +311,13 @@ async def transmit_data(
resp = await self.request_transfer_exit(config=config)
raise_for_error(resp)

async def wait_for_ecu(self, timeout: float = 60) -> bool:
"""wait for ecu to be alive again (eg. after reset)
Wait at most timeout"""
ret = False
try:
await asyncio.wait_for(self._wait_for_ecu(), timeout=timeout)
ret = True
except asyncio.TimeoutError:
self.logger.log_critical("Timeout while waiting for ECU!")
return ret

async def _wait_for_ecu(self) -> None:
async def _wait_for_ecu(self, sleep_time: float) -> None:
"""wait for ecu to be alive again (eg. after reset)
Internal method without timeout"""
self.logger.log_info("waiting for ECU…")
while True:
try:
await asyncio.sleep(1)
await asyncio.sleep(sleep_time)
await self.reconnect()
await self.ping()
break
Expand All @@ -340,6 +329,18 @@ async def _wait_for_ecu(self) -> None:
self.logger.log_debug(f"ECU not ready: {g_repr(e)}")
self.logger.log_info("ECU ready")

async def wait_for_ecu(
self,
timeout: Optional[float] = None,
) -> None:
"""wait for ecu to be alive again (eg. after reset)
Wait at most timeout"""
t = timeout if timeout is not None else self.timeout
try:
await asyncio.wait_for(self._wait_for_ecu(t * 0.8), timeout=t)
except asyncio.TimeoutError:
self.logger.log_critical("Timeout while waiting for ECU!")

async def update_state(
self, request: service.UDSRequest, response: service.UDSResponse
) -> None:
Expand Down