Esempio n. 1
0
    def __init__(self):
        super().__init__()
        service = os.environ.get(const.ROBOT_SERVICE, '')
        service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.type = os.environ.get(const.ROBOT_TYPE, '')

        self.orion = Orion(service, service_path)
Esempio n. 2
0
    def __init__(self):
        service = os.environ.get(const.PEPPER_SERVICE, '')
        service_path = os.environ.get(const.PEPPER_SERVICEPATH, '')
        self.type = os.environ.get(const.PEPPER_TYPE, '')

        self.orion = Orion(service, service_path)
        self.pepper_2_id = os.environ.get(const.PEPPER_2_ID, '')
Esempio n. 3
0
class CheckDestinationAPI(RobotFloorMapMixin, MethodView):
    NAME = 'check-destination'

    def __init__(self):
        super().__init__()
        dest_led_service = os.environ.get(const.DEST_LED_SERVICE, '')
        dest_led_service_path = os.environ.get(const.DEST_LED_SERVICEPATH, '')
        self.dest_led_orion = Orion(dest_led_service, dest_led_service_path)

        robot_service = os.environ.get(const.ROBOT_SERVICE, '')
        robot_service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.robot_orion = Orion(robot_service, robot_service_path)

        self.dest_led_type = os.environ.get(const.DEST_LED_TYPE, '')
        self.robot_type = os.environ.get(const.ROBOT_TYPE, '')

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'ignore'}
        try:
            r_mode = get_attr_value(content, 'r_mode')
            posx = get_attr_value(content, 'x')
            posy = get_attr_value(content, 'y')
            deviceid = get_id(content)
            floor = self.get_floor_by_robot(deviceid)
            logger.info(
                f'received position: r_mode={r_mode}, posx={posx}, posy={posy}, robot_id={deviceid}, floor={floor}'
            )

            current_state = self.robot_orion.get_attrs(
                deviceid, 'r_state')['r_state']['value'].strip()

            if posx is not None and posy is not None and floor is not None and current_state == const.GUIDING:
                destination = Destination().get_destination_by_dest_led_pos(
                    posx, posy, floor)
                if destination is not None and const.DEST_LED_ID in destination and destination[
                        const.DEST_LED_ID] is not None:
                    dest_led_id = destination[const.DEST_LED_ID]
                    message = self.dest_led_orion.send_cmd(
                        dest_led_id, self.dest_led_type, 'action', 'on')
                    result['result'] = 'success'
                    result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except DestinationFormatError as e:
            logger.error(f'DestinationFormatError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 4
0
    def __init__(self):
        super().__init__()
        dest_led_service = os.environ.get(const.DEST_LED_SERVICE, '')
        dest_led_service_path = os.environ.get(const.DEST_LED_SERVICEPATH, '')
        self.dest_led_orion = Orion(dest_led_service, dest_led_service_path)

        robot_service = os.environ.get(const.ROBOT_SERVICE, '')
        robot_service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.robot_orion = Orion(robot_service, robot_service_path)

        self.dest_led_type = os.environ.get(const.DEST_LED_TYPE, '')
        self.robot_type = os.environ.get(const.ROBOT_TYPE, '')
Esempio n. 5
0
    def __init__(self, suspending_sec_max):
        super().__init__()
        self.suspending_sec_max = suspending_sec_max

        dest_led_service = os.environ.get(const.DEST_LED_SERVICE, '')
        dest_led_service_path = os.environ.get(const.DEST_LED_SERVICEPATH, '')
        self.dest_led_orion = Orion(dest_led_service, dest_led_service_path)
        self.dest_led_type = os.environ.get(const.DEST_LED_TYPE, '')

        robot_service = os.environ.get(const.ROBOT_SERVICE, '')
        robot_service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.robot_orion = Orion(robot_service, robot_service_path)
        self.robot_type = os.environ.get(const.ROBOT_TYPE, '')

        self.destService = Destination()
Esempio n. 6
0
def notify_start_movement(service, service_path, id, type, dest, visitor_id):
    dest_pos_x = dest.get(DEST_POS_X)
    dest_pos_y = dest.get(DEST_POS_Y)
    if dest_pos_x is None or dest_pos_y is None:
        raise DestinationFormatError('dest_pos_x or dest_pos_y is empty')
    try:
        destx = float(dest_pos_x)
        desty = float(dest_pos_y)
        floor = int(dest.get(DEST_FLOOR))
    except (TypeError, ValueError):
        raise DestinationFormatError('invalid dest_pos or floor')
    else:
        timestamp = datetime.datetime.now(
            pytz.timezone('Asia/Tokyo')).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
        attributes = [{
            'name': 'timestamp',
            'value': timestamp,
        }, {
            'name': 'destx',
            'value': destx,
        }, {
            'name': 'desty',
            'value': desty,
        }, {
            'name': 'floor',
            'value': floor,
        }, {
            'name': 'visitor_id',
            'value': visitor_id,
        }]
        Orion(service, service_path).update_attributes(id, type, attributes)
Esempio n. 7
0
class StartReceptionAPI(MethodView):
    NAME = 'start-reception'

    def __init__(self):
        service = os.environ.get(const.PEPPER_SERVICE, '')
        service_path = os.environ.get(const.PEPPER_SERVICEPATH, '')
        self.type = os.environ.get(const.PEPPER_TYPE, '')

        self.orion = Orion(service, service_path)
        self.pepper_1_id = os.environ.get(const.PEPPER_1_ID, '')

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'failure'}
        try:
            value = get_attr_value(content, 'state')
            if value == 'on':
                message = self.orion.send_cmd(self.pepper_1_id, self.type, 'welcome', 'start')
                result['result'] = 'success'
                result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 8
0
class ChangeRobotStateAPI(MethodView):
    NAME = 'change-robot-state'

    def __init__(self):
        super().__init__()
        service = os.environ.get(const.ROBOT_SERVICE, '')
        service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.type = os.environ.get(const.ROBOT_TYPE, '')

        self.orion = Orion(service, service_path)

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'failure'}
        try:
            robot_id = get_id(content)
            state = get_attr_value(content, 'r_state')
            message = self.orion.send_cmd(robot_id + const.TABLET_SUFFIX,
                                          self.type, 'state', state)
            result['result'] = 'success'
            result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 9
0
    def __init__(self):
        super().__init__()
        service = os.environ.get(const.ROBOT_SERVICE, '')
        service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.type = os.environ.get(const.ROBOT_TYPE, '')
        if const.ROBOT_STATE_CHECK_WAIT_SEC in os.environ:
            try:
                self.robot_state_check_wait_sec = int(
                    os.environ[const.ROBOT_STATE_CHECK_WAIT_SEC])
            except (TypeError, ValueError):
                self.robot_state_check_wait_sec = current_app.config[
                    const.DEFAULT_ROBOT_STATE_CHECK_WAIT_SEC]
        else:
            self.robot_state_check_wait_sec = current_app.config[
                const.DEFAULT_ROBOT_STATE_CHECK_WAIT_SEC]

        self.orion = Orion(service, service_path)
Esempio n. 10
0
class FinishReceptionAPI(MethodView):
    NAME = 'finish-reception'

    def __init__(self):
        service = os.environ.get(const.PEPPER_SERVICE, '')
        service_path = os.environ.get(const.PEPPER_SERVICEPATH, '')
        self.type = os.environ.get(const.PEPPER_TYPE, '')

        self.orion = Orion(service, service_path)
        self.pepper_2_id = os.environ.get(const.PEPPER_2_ID, '')

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'failure'}
        try:
            value = get_attr_value(content, 'dest')
            dest = Destination().get_destination_by_name(value)
            dest_name = dest.get(DEST_NAME)
            if not dest_name:
                raise DestinationFormatError('dest_name is empty')
            try:
                dest_floor = int(dest.get(DEST_FLOOR))
            except (TypeError, ValueError):
                raise DestinationFormatError('dest_floor is invalid')

            if const.SLACK_WEBHOOK in dest:
                slack.send_message_to_slack(dest[const.SLACK_WEBHOOK], dest_name)

            if dest_floor == 2:
                logger.info(f'call facedetect to pepper({self.pepper_2_id}), dest_name={dest_name}, floor={dest_floor}')
                message = self.orion.send_cmd(self.pepper_2_id, self.type, 'facedetect', 'start')
            else:
                message = f'nothing to do, dest_name={dest_name}, floor={dest_floor}'
                logger.info(message)

            result['result'] = 'success'
            result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except DestinationDoesNotExist as e:
            logger.error(f'DestinationDoesNotFound: {str(e)}')
            raise BadRequest(str(e))
        except DestinationFormatError as e:
            logger.error(f'DestinationFormatError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 11
0
class AutoReturner(RobotFloorMapMixin):
    def __init__(self, suspending_sec_max):
        super().__init__()
        self.suspending_sec_max = suspending_sec_max

        dest_led_service = os.environ.get(const.DEST_LED_SERVICE, '')
        dest_led_service_path = os.environ.get(const.DEST_LED_SERVICEPATH, '')
        self.dest_led_orion = Orion(dest_led_service, dest_led_service_path)
        self.dest_led_type = os.environ.get(const.DEST_LED_TYPE, '')

        robot_service = os.environ.get(const.ROBOT_SERVICE, '')
        robot_service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.robot_orion = Orion(robot_service, robot_service_path)
        self.robot_type = os.environ.get(const.ROBOT_TYPE, '')

        self.destService = Destination()

    def check_robot(self, floor):
        robot_id = self.get_available_robot_from_floor(floor)
        robot_attrs = self.robot_orion.get_attrs(robot_id, 'r_state,destx,desty')
        current_state = robot_attrs['r_state']['value']
        current_destx = robot_attrs['destx']['value']
        current_desty = robot_attrs['desty']['value']
        current_datetime = parser.parse(robot_attrs['r_state']['metadata']['TimeInstant']['value'])
        now = datetime.datetime.now(timezone('Asia/Tokyo'))
        delta_sec = (now - current_datetime).total_seconds()

        if current_state != const.SUSPENDING or delta_sec < self.suspending_sec_max:
            logger.debug(f'nothing to do')
        else:
            initial = self.destService.get_initial_of_floor(floor)
            initial_pos_x = initial.get(const.DEST_POS_X)
            initial_pos_y = initial.get(const.DEST_POS_Y)
            if initial_pos_x is None or initial_pos_y is None:
                raise DestinationFormatError('initial dest_pos_x or dest_pos_y is empty')

            attributes = [
                {
                    'name': 'r_state',
                    'value': const.RETURNING,
                }, {
                    'name': 'destx',
                    'value': '',
                }, {
                    'name': 'desty',
                    'value': '',
                }, {
                    'name': 'visitor',
                    'value': '',
                }
            ]
            self.robot_orion.update_attributes(robot_id, self.robot_type, attributes)

            value = f'r_cmd|Navi|x|{initial_pos_x}|y|{initial_pos_y}'
            self.robot_orion.send_cmd(robot_id, self.robot_type, 'robot_request', value)
            logger.info(f'guide_robot({robot_id}) return to the initial position of floor({floor}) automatically')

            params = {
                'filter': f'{const.DEST_POS_X}|{current_destx},{const.DEST_POS_Y}|{current_desty},{const.DEST_FLOOR}|{floor}'
            }
            destinations = self.destService.get_destinations(params)
            for target_dest in destinations:
                if (const.DEST_LED_ID in target_dest and target_dest[const.DEST_LED_ID] is not None):
                    dest_led_id = target_dest[const.DEST_LED_ID]
                    self.dest_led_orion.send_cmd(dest_led_id, self.dest_led_type, 'action', 'off')
                    logger.info(f'destination led({dest_led_id}) turn to off automatically')
Esempio n. 12
0
class ArrivalAPI(RobotFloorMapMixin, MethodView):
    NAME = 'arrival'

    def __init__(self):
        super().__init__()
        dest_led_service = os.environ.get(const.DEST_LED_SERVICE, '')
        dest_led_service_path = os.environ.get(const.DEST_LED_SERVICEPATH, '')
        self.dest_led_orion = Orion(dest_led_service, dest_led_service_path)

        robot_service = os.environ.get(const.ROBOT_SERVICE, '')
        robot_service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.robot_orion = Orion(robot_service, robot_service_path)

        self.dest_led_type = os.environ.get(const.DEST_LED_TYPE, '')
        self.robot_type = os.environ.get(const.ROBOT_TYPE, '')

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'ignore'}
        try:
            arrival = get_attr_value(content, 'arrival')
            if arrival is not None:
                id = get_id(content)
                destService = Destination()
                destination = destService.get_destination_by_dest_human_sensor_id(
                    id)

                if destination is not None and const.DEST_FLOOR in destination:
                    try:
                        floor = int(destination[const.DEST_FLOOR])
                    except (TypeError, ValueError):
                        raise DestinationFormatError('dest_floor is invalid')

                    robot_id = self.get_available_robot_from_floor(floor)
                    robot_attrs = self.robot_orion.get_attrs(
                        robot_id, 'r_state,destx,desty')
                    current_state = robot_attrs['r_state']['value']
                    try:
                        destx = float(robot_attrs['destx']['value'])
                        desty = float(robot_attrs['desty']['value'])
                    except (TypeError, ValueError):
                        destx = float('inf')
                        desty = float('inf')

                    if (current_state != const.SUSPENDING or
                            destx != float(destination.get(const.DEST_POS_X))
                            or
                            desty != float(destination.get(const.DEST_POS_Y))):
                        message = f'cannot accept command at Arrival, current_state={current_state}, robot_id={robot_id}'\
                            f'destx={destx}, desty={desty}, floor={floor}'
                        logger.warning(message)
                        result['result'] = 'not acceptable'
                        result['message'] = message
                    else:
                        if const.DEST_LED_ID in destination and destination[
                                const.DEST_LED_ID] is not None:
                            dest_led_id = destination[const.DEST_LED_ID]
                            self.dest_led_orion.send_cmd(
                                dest_led_id, self.dest_led_type, 'action',
                                'off')

                        initial = destService.get_initial_of_floor(floor)
                        initial_pos_x = initial.get(const.DEST_POS_X)
                        initial_pos_y = initial.get(const.DEST_POS_Y)
                        if initial_pos_x is None or initial_pos_y is None:
                            raise DestinationFormatError(
                                'initial dest_pos_x or dest_pos_y is empty')

                        attributes = [{
                            'name': 'r_state',
                            'value': const.RETURNING,
                        }, {
                            'name': 'destx',
                            'value': '',
                        }, {
                            'name': 'desty',
                            'value': '',
                        }]
                        self.robot_orion.update_attributes(
                            robot_id, self.robot_type, attributes)

                        value = f'r_cmd|Navi|x|{initial_pos_x}|y|{initial_pos_y}'
                        message = self.robot_orion.send_cmd(
                            robot_id, self.robot_type, 'robot_request', value)
                        logger.info(
                            f'guide_robot({robot_id}) return to the initial position of floor({floor})'
                        )
                        result['result'] = 'success'
                        result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except DestinationFormatError as e:
            logger.error(f'DestinationFormatError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 13
0
class StartMovementAPI(RobotFloorMapMixin, MethodView):
    NAME = 'start-movement'

    def __init__(self):
        super().__init__()
        service = os.environ.get(const.ROBOT_SERVICE, '')
        service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.type = os.environ.get(const.ROBOT_TYPE, '')

        self.orion = Orion(service, service_path)

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'failure'}
        try:
            destx = get_attr_value(content, 'destx')
            desty = get_attr_value(content, 'desty')
            visitor_id = get_attr_value(content, 'visitor_id')

            try:
                floor = int(get_attr_value(content, 'floor'))
            except (TypeError, ValueError):
                raise DestinationFormatError('dest_floor is invalid')

            if destx is not None and desty is not None and floor in (
                    1, 2) and visitor_id is not None:
                robot_id = self.get_available_robot_from_floor(floor)
                current_state = self.orion.get_attrs(
                    robot_id, 'r_state')['r_state']['value'].strip()

                if current_state != const.WAITING:
                    message = f'cannot accept command at StartMovementAPI, current_state={current_state}, robot_id={robot_id} '\
                        f'destx={destx}, desty={desty}, floor={floor}, visitor_id={visitor_id}'
                    logger.warning(message)
                    result['result'] = 'not acceptable'
                    result['message'] = message
                else:
                    attributes = [{
                        'name': 'r_state',
                        'value': const.GUIDING,
                    }, {
                        'name': 'destx',
                        'value': destx,
                    }, {
                        'name': 'desty',
                        'value': desty,
                    }, {
                        'name': 'visitor',
                        'value': visitor_id,
                    }]
                    self.orion.update_attributes(robot_id, self.type,
                                                 attributes)

                    value = f'r_cmd|Navi|x|{destx}|y|{desty}'
                    message = self.orion.send_cmd(robot_id, self.type,
                                                  'robot_request', value)
                    result['result'] = 'success'
                    result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except DestinationFormatError as e:
            logger.error(f'DestinationFormatError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)
Esempio n. 14
0
class StopMovementAPI(RobotFloorMapMixin, MethodView):
    NAME = 'stop-movement'

    def __init__(self):
        super().__init__()
        service = os.environ.get(const.ROBOT_SERVICE, '')
        service_path = os.environ.get(const.ROBOT_SERVICEPATH, '')
        self.type = os.environ.get(const.ROBOT_TYPE, '')
        if const.ROBOT_STATE_CHECK_WAIT_SEC in os.environ:
            try:
                self.robot_state_check_wait_sec = int(
                    os.environ[const.ROBOT_STATE_CHECK_WAIT_SEC])
            except (TypeError, ValueError):
                self.robot_state_check_wait_sec = current_app.config[
                    const.DEFAULT_ROBOT_STATE_CHECK_WAIT_SEC]
        else:
            self.robot_state_check_wait_sec = current_app.config[
                const.DEFAULT_ROBOT_STATE_CHECK_WAIT_SEC]

        self.orion = Orion(service, service_path)

    def post(self):
        content = request.data.decode('utf-8')
        logger.info(f'request content={content}')

        result = {'result': 'failure'}
        try:
            r_mode = get_attr_value(content, 'r_mode')
            posx = get_attr_value(content, 'x')
            posy = get_attr_value(content, 'y')
            deviceid = get_id(content)
            floor = self.get_floor_by_robot(deviceid)
            logger.info(
                f'received position: r_mode={r_mode}, posx={posx}, posy={posy}, robot_id={deviceid}, floor={floor}'
            )

            if r_mode == 'Standby':
                content = self.orion.get_attrs(deviceid, 'r_state')['r_state']
                current_state = content['value'].strip()
                state_datetime = parser.parse(
                    content['metadata']['TimeInstant']['value'].strip())
                now = datetime.datetime.now(timezone('Asia/Tokyo'))
                delta_sec = (now - state_datetime).total_seconds()

                if current_state not in (
                        const.GUIDING, const.RETURNING
                ) or delta_sec < self.robot_state_check_wait_sec:
                    message = f'cannot accept command at StopMovmentAPI, current_state={current_state}, deviceid={deviceid} '\
                        f'r_mode={r_mode}, posx={posx}, posy={posy}, floor={floor}, delta_sec={delta_sec}'
                    logger.debug(message)
                    result['result'] = 'not acceptable'
                    result['message'] = message
                else:
                    if current_state == const.GUIDING:
                        r_state = const.SUSPENDING
                    else:
                        r_state = const.WAITING

                    attributes = [{
                        'name': 'r_state',
                        'value': r_state,
                    }]
                    message = self.orion.update_attributes(
                        deviceid, self.type, attributes)
                    result['result'] = 'success'
                    result['message'] = message
        except AttrDoesNotExist as e:
            logger.error(f'AttrDoesNotExist: {str(e)}')
            raise BadRequest(str(e))
        except NGSIPayloadError as e:
            logger.error(f'NGSIPayloadError: {str(e)}')
            raise BadRequest(str(e))
        except DestinationFormatError as e:
            logger.error(f'DestinationFormatError: {str(e)}')
            raise BadRequest(str(e))
        except Exception as e:
            logger.exception(e)
            raise e

        return jsonify(result)