def test_process_stop_command(self):
        d = {
            'type': 'StopCommand',
            'address': 'ev3-ports:outD',
            'speed': 100,
            'stop_action': 'coast'
        }

        robot_state = RobotState()
        server = ClientSocketHandler(robot_state, None, 'left_brick')

        data = server._process_stop_command(d)
        val = self._deserialize(data)

        self.assertAlmostEqual(0.0667, val, 3)
    def test_process_data_request(self):
        d = {
            'type': 'DataRequest',
            'address': 'ev3-ports:in4',
        }

        robot_state = RobotState()
        robot_state.values['left_brick:ev3-ports:in4'] = 10
        robot_state.locks['left_brick:ev3-ports:in4'] = threading.Lock()

        server = ClientSocketHandler(robot_state, None, 'left_brick')
        data = server._process_data_request(d)
        val = self._deserialize(data)

        self.assertEqual(val, 10)
    def test_process_drive_command_pixels(self):
        d = {
            'type': 'RotateCommand',
            'address': 'ev3-ports:outB',
            'speed': 10,
            'distance': 100,
            'stop_action': 'hold'
        }

        robot_state = RobotState()
        server = ClientSocketHandler(robot_state, None, 'left_brick')

        data = server._process_drive_command(d)
        val = self._deserialize(data)

        self.assertEqual(10, val)
    def test_process_sound_command(self):
        d = {
            'type': 'SoundCommand',
            'message': 'A test is running at the moment!',
        }

        frames_per_second = get_config().get_data(
        )['exec_settings']['frames_per_second']
        frames = int(round((32 / 2.5) * frames_per_second))
        robot_state = RobotState()

        server = ClientSocketHandler(robot_state, None, 'left_brick')
        server._process_sound_command(d)

        for i in range(frames):
            self.assertIsNotNone(robot_state.next_sound_job())

        self.assertIsNone(robot_state.next_sound_job())
Example #5
0
    def run(self):
        """
        Listen for incoming connections. When a connection is established spawn a ClientSocketHandler
        to manage it. Multiple connections can be established at the same time.
        """

        port = get_simulation_settings()['exec_settings']['socket_port']

        for robot_sim in self.word_simulator.robot_simulators:
            for brick in robot_sim.robot.get_bricks():
                sock = ClientSocketHandler(robot_sim, brick.brick, brick.name)
                sock.setDaemon(True)
                sock.start()
                self.brick_sockets[(robot_sim.robot.name, brick.name)] = sock

        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server.bind(('localhost', port))
        server.listen(5)
        server.setblocking(False)
        server.settimeout(1)

        print('Listening for connections...')
        self.handle_sockets(server)
        print('Closing server')
Example #6
0
    def create_handler(self, client,
                       connection_id: str) -> ClientSocketHandler:
        """
        Start a ClientSocketHandler tread to manage the given connection.
        :param client: of the connection to manage.
        :param connection_id: of the connection.
        :return: a newly created ClientSocketHandler object.
        """

        handler = ClientSocketHandler(self.robot_state, client, connection_id)
        handler.setDaemon(True)
        handler.start()

        return handler
def create_client_socket_handler():
    robot_sim = create_robot_sim()
    return ClientSocketHandler(robot_sim, 0, 'left_brick')