diff --git a/pyniryo/api/tcp_client.py b/pyniryo/api/tcp_client.py index 80efed01..080ff406 100644 --- a/pyniryo/api/tcp_client.py +++ b/pyniryo/api/tcp_client.py @@ -257,6 +257,14 @@ def __transform_to_type(self, value, type_): except ValueError: self.__raise_exception_expected_type(type_.__name__, value) + def __str_to_bool(self, data): + if data == "True": + return True + elif data == "False": + return False + else: + self.__raise_exception("Expected data to be either 'True' or 'False'") + # Error Handlers def __raise_exception_expected_choice(self, expected_choice, given): raise TcpCommandException("Expected one of the following: {}.\nGiven: {}".format(expected_choice, given)) @@ -386,7 +394,7 @@ def get_learning_mode(self): :return: ``True`` if learning mode is on :rtype: bool """ - return eval(self.__send_n_receive(Command.GET_LEARNING_MODE)) + return self.__str_to_bool(self.__send_n_receive(Command.GET_LEARNING_MODE)) @learning_mode.setter def learning_mode(self, value): @@ -459,7 +467,7 @@ def collision_detected(self): :type: bool """ - return eval(self.__send_n_receive(Command.GET_COLLISION_DETECTED)) + return self.__str_to_bool(self.__send_n_receive(Command.GET_COLLISION_DETECTED)) def clear_collision_detected(self): """ @@ -1517,10 +1525,10 @@ def get_hardware_status(self): rpi_temperature = float(data[0]) hardware_version = data[1] - connection_up = eval(data[2]) + connection_up = self.__str_to_bool(data[2]) error_message = data[3] - calibration_needed = eval(data[4]) - calibration_in_progress = eval(data[5]) + calibration_needed = self.__str_to_bool(data[4]) + calibration_in_progress = self.__str_to_bool(data[5]) motor_names = data[6] motor_types = data[7] @@ -1646,7 +1654,7 @@ def get_conveyors_feedback(self): for i in range(len(conveyors_feedback)): conveyors_feedback[i]['conveyor_id'] = ConveyorID[conveyors_feedback[i]['conveyor_id']] conveyors_feedback[i]['direction'] = ConveyorDirection(conveyors_feedback[i]['direction']) - conveyors_feedback[i]['connection_state'] = eval(conveyors_feedback[i]['connection_state']) + conveyors_feedback[i]['connection_state'] = self.__str_to_bool(conveyors_feedback[i]['connection_state']) return conveyors_feedback @@ -1784,7 +1792,7 @@ def get_target_pose_from_cam(self, workspace_name, height_offset=0.0, shape=Obje height_offset, shape, color) - obj_found = eval(data_array[0]) + obj_found = self.__str_to_bool(data_array[0]) if obj_found: pose_object = PoseObject(*data_array[1]) shape_ret = data_array[2] @@ -1803,7 +1811,7 @@ def __move_with_vision(self, command, workspace_name, height_offset, shape, colo data_array = self.__send_n_receive(command, workspace_name, height_offset, shape, color, **kwargs) - obj_found = eval(data_array[0]) + obj_found = self.__str_to_bool(data_array[0]) if obj_found is True: shape_ret = data_array[1] color_ret = data_array[2] @@ -1892,7 +1900,7 @@ def detect_object(self, workspace_name, shape=ObjectShape.ANY, color=ObjectColor self.__check_enum_belonging(color, ObjectColor) data_array = self.__send_n_receive(Command.DETECT_OBJECT, workspace_name, shape, color) - obj_found = eval(data_array[0]) + obj_found = self.__str_to_bool(data_array[0]) if not obj_found: rel_pose_array = 3 * [0.0] shape = "ANY"