def translate_to_rotate_operation(message: Message) -> RotateOperation: try: angle = float(message.get_data("angle")) except ValueError: logger.warning("cannot translate angle value to float") raise TranslationError return RotateOperation(Angle(angle * (2 * pi)))
def update(self) -> None: position = self._vision_service.get_robot() robot_position = Position(position.coordinate.to_centimeters(), position.orientation) data = robot_position.serialize() self._communication_service.send_message( Message("position", position=data))
def _update_base(self) -> None: self._vision_service.update() update_message = Message("update", light_on=self._display_service.is_light_on(), image_data=self._vision_service.get_image(), capacitor_charge=self._chargeable.get_charge(), objective=self._vision_service.get_objective()) self._communication_service.send_message(update_message)
class TestCommunicationService(TestCase): def setUp(self) -> None: self.robot_connector = Mock() self.connection = Mock() self.robot_connector.connect_robot.return_value = self.connection self.test_message = Message("test_message", variable1="variable1") def test_when_created_then_creates_a_socket_from_connector(self) -> None: CommunicationService(self.robot_connector) self.robot_connector.connect_robot.assert_called_once() def test_when_send_message_then_message_is_sent_on_socket(self) -> None: communication_service = CommunicationService(self.robot_connector) communication_service.send_message(self.test_message) self.connection.send_msg.assert_called_once() def test_when_receive_message_then_message_is_correct(self) -> None: message_data = self.test_message.serialize() self.connection.recv_msg.return_value = message_data communication_service = CommunicationService(self.robot_connector) received_message = communication_service.receive_message() self.assertEqual(self.test_message, received_message)
def receive_message(self) -> Message: while True: data = self._connection.recv_msg() try: return Message.deserialize(data) except JSONDecodeError: logger.error("failed deserializing message: {}".format(data))
def translate_to_translate_operation( message: Message) -> Union[TranslateOperation, CarefulOperation]: distance = message.get_data("distance") speed = message.get_data("speed") direction = message.get_data("direction") try: angle = directionAngleMapping[direction] except KeyError: logger.warning("invalid direction {}".format(direction)) raise TranslationError() try: distance = float(distance) except ValueError: logger.warning('Cannot translate distance to float') raise TranslationError() if speed == "fast": return TranslateOperation(angle, Distance(distance)) else: return CarefulOperation(angle, Distance(distance))
def execute_directional_movement(self, direction: str, speed: str, distance: float, callback: CommandCallback) -> None: command = "directional_movement" self._callback_commands[command] = callback self._communication_service.send_message( Message(command, direction=direction, speed=speed, distance=distance))
class TestMessage(TestCase): def setUp(self) -> None: self.message = Message("test_message", variable1="variable1") def test_when_deserializing_then_message_is_equivalent(self) -> None: original_data = self.message.serialize() deserialized_message = Message.deserialize(original_data) self.assertEqual(self.message, deserialized_message)
def _update(self, message: Message) -> None: try: light_on: bool = message.get_data("light_on") self._light_service.update_light(Light(light_on)) except KeyError: pass try: image_data: str = message.get_data("image_data") self._robot_camera_service.update_image(image_data) except KeyError: pass try: capacitor_charge: float = message.get_data("capacitor_charge") self._prehensor_service.update_prehensor( Prehensor(capacitor_charge)) except KeyError: pass try: objective_text: str = message.get_data("objective") self._objective_service.update_objective(Objective(objective_text)) except KeyError: pass
def translate_to_objective(message: Message) -> Objective: target = message.get_data('target') if target in shapes.keys(): shape = shapes[target] else: shape = Shape.NoShape if target in colors.keys(): color = colors[target] else: color = Color.NoColor zone = target[-1] if zone in ['0', '1', '2', '3']: destination = int(zone) else: destination = 0 return Objective(destination, shape, color)
def execute_championship(self, callback: CommandCallback) -> None: command = "championship" self._callback_commands[command] = callback self._communication_service.send_message(Message(command))
def get_position(self) -> AbsoluteCoordinate: self._communication_service.send_message(Message("get_position")) self._position_received.clear() self._position_received.wait() return self._position
def setUp(self) -> None: self.robot_connector = Mock() self.connection = Mock() self.robot_connector.connect_robot.return_value = self.connection self.test_message = Message("test_message", variable1="variable1")
def execute_switch_light_subroutine(self, callback: CommandCallback) -> None: command = "switch_light_subroutine" self._callback_commands[command] = callback self._communication_service.send_message(Message(command))
def execute_drop_subroutine(self, target: str, callback: CommandCallback) -> None: command = "drop_subroutine" self._callback_commands[command] = callback self._communication_service.send_message( Message(command, target=target))
def _pathable_goal(self, message: Message) -> None: path = self._pathfinding_service.get_goal() data = path.serialize() self._communication_service.send_message( Message("goal_pathable", table=data))
def execute_rotational_movement(self, angle: float, callback: CommandCallback) -> None: command = "rotational_movement" self._callback_commands[command] = callback self._communication_service.send_message(Message(command, angle=angle))
def execute_discharge_magnet(self, callback: CommandCallback) -> None: command = "discharge_magnet" self._callback_commands[command] = callback self._communication_service.send_message(Message(command))
def setUp(self) -> None: self.message = Message("test_message", variable1="variable1")
def execute_update_directions(self, callback: CommandCallback) -> None: command = "update_directions" self._callback_commands[command] = callback self._communication_service.send_message(Message(command))
def test_when_deserializing_then_message_is_equivalent(self) -> None: original_data = self.message.serialize() deserialized_message = Message.deserialize(original_data) self.assertEqual(self.message, deserialized_message)
def execute_look_ahead(self, callback: CommandCallback) -> None: command = "look_ahead" self._callback_commands[command] = callback self._communication_service.send_message(Message(command))
def send_message(self, message: Message) -> None: self._connection.send_msg(message.serialize())
def _lost_connection(self) -> None: self._communication_service.send_message(Message("lost_connection"))
def update_position(self, message: Message) -> None: self._position_service.set_position(message.get_data("position"))
def _command_completed(self, message: Message) -> None: self._command_state_notifier.completed(message.get_data("command"))
def _start_charging(self) -> None: self._communication_service.send_message(Message("start_charging"))
def set_charge_station_pathable(self, message: Message) -> None: pathable = self._pathable_factory.create_from( message.get_data('table')) self._pathable_catalog.charge_station = pathable
def _charge_done(self) -> None: self._communication_service.send_message(Message("charge_done"))
def set_source_pathable(self, message: Message) -> None: pathable = self._pathable_factory.create_from( message.get_data('table')) self._pathable_catalog.source = pathable