Пример #1
0
 def send(self, message):
     if self.client:
         logger.info('Sending message: {}'.format(message))
         message = json.dumps(message)
         self.client.send(bytearray(message, 'ascii'))
     else:
         logger.error(
             'There is no client to send message: {}'.format(message))
Пример #2
0
def main_loop():
    logger.info('Script was launched')

    executor = CommandExecutor()
    communicator = CommunicationService(executor)

    while True:
        communicator.receive()
        dont_crash()
Пример #3
0
    def dispose(self):
        logger.info('Shutting down whole system')

        drive_control.stop()

        self.stop_streaming()
        self.stop_network_streaming()
        self.communicator.dispose()
        sys.exit(0)
Пример #4
0
    def get_connection(self):
        """
        Keeps on listening for incoming connection request and connects a client when there is no current session
        """

        self.sock.bind((HOST, PORT))
        self.sock.listen(10)

        conn, addr = self.sock.accept()
        logger.info('Client connected from {}'.format(addr))
        self.client = conn
Пример #5
0
    def network_stream_worker(self):
        logger.info('Starting streaming service')

        process = subprocess.Popen(shlex.split(START_NETWORK_STREAM),
                                   stdout=subprocess.PIPE)

        while self.does_network_streaming:
            output = process.stdout.readline()
            if output == '' and process.poll() is not None:
                break
            if output:
                self.communicator.send(CANT_OPEN_STREAM)
                logger.error(output.strip())

        rc = process.poll()
        return rc
Пример #6
0
    def receive(self):
        while True:
            if self.client:
                try:
                    bits = self.client.recv(BUFF_SIZE)
                    message = bits.decode('ascii')

                    if message:
                        logger.info('d message: {}'.format(message))

                        try:
                            message = json.loads(message)
                            if type(message) == str:
                                self.cmd_executor.execute(message)
                        except ValueError:
                            logger.error('Broken message')

                except socket.error as e:
                    logger.error(str(e))
                    logger.info('Connection with client was closed')
                    self.cmd_executor.execute(CLIENT_LOST)
                    self.dispose()
Пример #7
0
    def execute(self, message):
        logger.info('Received command: {}'.format(message))

        data = message.split(' ')

        command = data[0]
        arguments = data[1:]

        if command == START_STREAM_CMD:
            self.start_streaming()

        elif command == STOP_STREAM_CMD:
            self.stop_streaming()

        elif command == START_NETWORK_STREAM_CMD:
            self.start_network_streaming()

        elif command == STOP_NETWORK_STREAM_CMD:
            self.stop_network_streaming()

        elif command == CLIENT_LOST:
            drive_control.stop()
            self.communicator.get_connection()

        elif command == SHUTDOWN_CMD:
            self.dispose()

        elif command == RUN_FORWARD_CMD:
            drive_control.forward()

        elif command == RUN_BACKWARD_CMD:
            drive_control.backward()

        elif command == RUN_LEFT_CMD:
            drive_control.left()

        elif command == RUN_RIGHT_CMD:
            drive_control.right()

        elif command == STOP_ROBOT_CMD:
            drive_control.stop()

        elif command == SET_SPEED_CMD:
            speed = int(arguments[0])
            drive_control.set_speed(speed)

        elif command == CAMERA_DOWN_CMD:
            drive_control.turn_camera_down()

        elif command == CAMERA_UP_CMD:
            drive_control.turn_camera_up()

        elif command == CAMERA_RIGHT_CMD:
            drive_control.turn_camera_right()

        elif command == CAMERA_LEFT_CMD:
            drive_control.turn_camera_left()

        elif command == TURN_SENSOR_LEFT_CMD:
            distance_sensor.turn_left()

        elif command == TURN_SENSOR_RIGHT_CMD:
            distance_sensor.turn_right()

        elif command == MEASURE_DISTANCE_CMD:
            angle, distance = distance_sensor.range()
            data = {'angle': angle, 'distance': distance}
            self.communicator.send(data)

        elif command == SENSOR_LEFT_CMD:
            distance_sensor.turn_left()

        elif command == SENSOR_RIGHT_CMD:
            distance_sensor.turn_right()

        elif command == SET_SENSOR_DELTA_CMD:
            delta = int(arguments[0])
            distance_sensor.set_delta(delta)
Пример #8
0
 def stop_network_streaming(self):
     if self.network_streaming_thread:
         self.does_network_streaming = False
         self.network_streaming_thread.join()
         logger.info('Network streaming service closed')
Пример #9
0
 def stop_streaming(self):
     if self.streaming_thread:
         self.does_streaming = False
         self.streaming_thread.join()
         logger.info('Streaming service closed')