Skip to content

Commit cde8bb9

Browse files
authored
Merge pull request #37 from JuBiotech/make-list-files-return-file-list
Make `get_*` methods return the requested data
2 parents 96b6a2c + dfe7b71 commit cde8bb9

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

‎cri_lib/cri_controller.py‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -441,48 +441,52 @@ async def wait_for_kinematics_ready_async(self, timeout: float = 30) -> bool:
441441
async def get_board_temperatures_async(
442442
self,
443443
timeout: float | None = DEFAULT, # type: ignore
444-
) -> bool:
444+
) -> list[float]:
445445
"""Receive motor controller PCB temperatures and save in robot state
446446
447447
Parameters
448448
----------
449449
timeout: float | None
450450
timeout for waiting in seconds or None for infinite waiting
451+
452+
Returns
453+
-------
454+
list of motor controller PCB temperatures
451455
"""
452456
self._send_command("SYSTEM GetBoardTemp", True, "info_boardtemp")
453457
if (
454458
error_msg := await self._wait_for_answer_async(
455459
"info_boardtemp", timeout=timeout
456460
)
457461
) is not None:
458-
logger.debug("Error in GetBoardTemp command: %s", error_msg)
459-
return False
460-
else:
461-
return True
462+
raise CRICommandError(f"Error in GetBoardTemp command: {error_msg}")
463+
return list(self.robot_state.board_temps)
462464

463465
async def get_motor_temperatures_async(
464466
self,
465467
timeout: float | None = DEFAULT, # type: ignore
466-
) -> bool:
468+
) -> list[float]:
467469
"""Receive motor temperatures and save in robot state
468470
469471
Parameters
470472
----------
471473
timeout: float | None
472474
timeout for waiting in seconds or None for infinite waiting
475+
476+
Returns
477+
-------
478+
list of motor temperatures
473479
"""
474480
self._send_command("SYSTEM GetMotorTemp", True, "info_motortemp")
475481
if (
476482
error_msg := await self._wait_for_answer_async(
477483
"info_motortemp", timeout=timeout
478484
)
479485
) is not None:
480-
logger.debug("Error in GetMotorTemp command: %s", error_msg)
481-
return False
482-
else:
483-
return True
486+
raise CRICommandError(f"Error in GetMotorTemp command: {error_msg}")
487+
return list(self.robot_state.motor_temps)
484488

485-
async def list_files_async(self, target_directory: str = "Programs") -> bool:
489+
async def list_files_async(self, target_directory: str = "Programs") -> list[str]:
486490
"""Request a list of all files in the directory, which is relative to the /Data/ directory.
487491
488492
Parameters
@@ -492,28 +496,20 @@ async def list_files_async(self, target_directory: str = "Programs") -> bool:
492496
493497
Returns
494498
-------
495-
a list of files
499+
a list of file paths relative to `/Data`
496500
"""
497501

498502
command = f"CMD ListFiles {target_directory}"
499-
503+
self._send_command(
504+
command=command, register_answer=True, fixed_answer_name="info_filelist"
505+
)
500506
if (
501-
self._send_command(
502-
command=command, register_answer=True, fixed_answer_name="info_filelist"
503-
)
504-
is not None
505-
):
506-
if (
507-
error_msg := await self._wait_for_answer_async(
508-
"info_filelist", timeout=self.DEFAULT_ANSWER_TIMEOUT
509-
)
510-
) is not None:
511-
logger.debug("Error in ListFiles command: %s", error_msg)
512-
return False
513-
else:
514-
return True
515-
else:
516-
return False
507+
error_msg := await self._wait_for_answer_async("info_filelist")
508+
) is not None:
509+
raise CRICommandError(f"Error in ListFiles command: {error_msg}")
510+
# the obtained file list is cached in the parser
511+
with self.parser.file_list_lock:
512+
return list(self.parser.file_list)
517513

518514
def wait_for_status_update(self, timeout: float | None = None) -> None:
519515
"""Blocking wrapper around :func:`CRIClient.wait_for_status_update_async`."""
@@ -530,7 +526,7 @@ def wait_for_kinematics_ready(self, timeout: float = 30) -> bool:
530526
def get_board_temperatures(
531527
self,
532528
timeout: float | None = DEFAULT, # type: ignore
533-
) -> bool:
529+
) -> list[float]:
534530
"""Blocking wrapper around :func:`CRIClient.get_board_temperatures_async`."""
535531
return asyncio.get_event_loop().run_until_complete(
536532
self.get_board_temperatures_async(timeout=timeout)
@@ -539,13 +535,13 @@ def get_board_temperatures(
539535
def get_motor_temperatures(
540536
self,
541537
timeout: float | None = DEFAULT, # type: ignore
542-
) -> bool:
538+
) -> list[float]:
543539
"""Blocking wrapper around :func:`CRIClient.get_motor_temperatures_async`."""
544540
return asyncio.get_event_loop().run_until_complete(
545541
self.get_motor_temperatures_async(timeout=timeout)
546542
)
547543

548-
def list_files(self) -> bool:
544+
def list_files(self) -> list[str]:
549545
"""Blocking wrapper around :func:`CRIClient.list_files_async`."""
550546
return asyncio.get_event_loop().run_until_complete(self.list_files_async())
551547

0 commit comments

Comments
 (0)