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)))
Example #2
0
 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))
Example #3
0
 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)
Example #4
0
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))
Example #7
0
 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))
Example #8
0
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)
Example #9
0
 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
Example #10
0
 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)
Example #11
0
 def execute_championship(self, callback: CommandCallback) -> None:
     command = "championship"
     self._callback_commands[command] = callback
     self._communication_service.send_message(Message(command))
Example #12
0
 def get_position(self) -> AbsoluteCoordinate:
     self._communication_service.send_message(Message("get_position"))
     self._position_received.clear()
     self._position_received.wait()
     return self._position
Example #13
0
 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")
Example #14
0
 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))
Example #15
0
 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))
Example #16
0
 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))
Example #17
0
 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))
Example #18
0
 def execute_discharge_magnet(self, callback: CommandCallback) -> None:
     command = "discharge_magnet"
     self._callback_commands[command] = callback
     self._communication_service.send_message(Message(command))
Example #19
0
 def setUp(self) -> None:
     self.message = Message("test_message", variable1="variable1")
Example #20
0
 def execute_update_directions(self, callback: CommandCallback) -> None:
     command = "update_directions"
     self._callback_commands[command] = callback
     self._communication_service.send_message(Message(command))
Example #21
0
    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)
Example #22
0
 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())
Example #24
0
 def _lost_connection(self) -> None:
     self._communication_service.send_message(Message("lost_connection"))
Example #25
0
 def update_position(self, message: Message) -> None:
     self._position_service.set_position(message.get_data("position"))
Example #26
0
 def _command_completed(self, message: Message) -> None:
     self._command_state_notifier.completed(message.get_data("command"))
Example #27
0
 def _start_charging(self) -> None:
     self._communication_service.send_message(Message("start_charging"))
Example #28
0
 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
Example #29
0
 def _charge_done(self) -> None:
     self._communication_service.send_message(Message("charge_done"))
Example #30
0
 def set_source_pathable(self, message: Message) -> None:
     pathable = self._pathable_factory.create_from(
         message.get_data('table'))
     self._pathable_catalog.source = pathable