diff --git a/cri_lib/cri_controller.py b/cri_lib/cri_controller.py index 8bae446..84fbd61 100644 --- a/cri_lib/cri_controller.py +++ b/cri_lib/cri_controller.py @@ -37,6 +37,9 @@ class CRIController: def __init__(self) -> None: self.robot_state: RobotState = RobotState() self.robot_state_lock = threading.Lock() + + self.program_list: list = [] + self.program_list_lock: Lock = threading.Lock() self.parser = CRIProtocolParser(self.robot_state, self.robot_state_lock) @@ -1514,6 +1517,36 @@ def get_motor_temperatures( else: return False + def list_flies(self, target_directory: str = "Programs" ) -> bool: + """request a list of all files in the directory, which is relative to the /Data/ directory. + + Parameters + ---------- + directory : str + directory on iRC `/Data/` in which files are located, e.g. `Programs` for normal robot programs + + Returns + ------- + a list of files? + """ + + command = f"CMD ListFiles {target_directory}" + + if ( + self._send_command(command=command, register_answer=True, fixed_answer_name="info_flielist") + is not None + ): + if ( + error_msg := self._wait_for_answer( + "info_flielist", timeout=self.DEFAULT_ANSWER_TIMEOUT + ) + ) is not None: + logger.debug("Error in GetBoardTemp command: %s", error_msg) + return False + else: + return True + else: + return False # Monkey patch to maintain backward compatibility CRIController.MotionType = MotionType # type: ignore diff --git a/cri_lib/cri_protocol_parser.py b/cri_lib/cri_protocol_parser.py index bdd0a4c..cc718f0 100644 --- a/cri_lib/cri_protocol_parser.py +++ b/cri_lib/cri_protocol_parser.py @@ -25,9 +25,11 @@ class CRIProtocolParser: """Class handling the parsing of CRI messages to the robot state.""" - def __init__(self, robot_state: RobotState, robot_state_lock: Lock): + def __init__(self, robot_state: RobotState, robot_state_lock: Lock, program_list : list, program_list_lock: Lock): self.robot_state = robot_state self.robot_state_lock = robot_state_lock + self.program_list = program_list + self.program_list_lock = program_list_lock def parse_message( self, message: str @@ -598,6 +600,13 @@ def _parse_info(self, parameters: list[str]) -> str | None: self.robot_state.motor_temps = temperatures return "info_motortemp" + elif parameters[0] == "FileList": + with self.program_list_lock: + self.program_list.clear() # direct point to parameters[] will break the shared reference + self.program_list.extend(parameters[2:]) # first element is the target_folder + print(self.program_list) + return "info_flielist" + else: return None