예제 #1
0
    def post(self):
        logger.debug('StaticRoutePlanner.post')
        body = request.json

        if body is None or body.get('planId') is None or body.get('robotId') is None:
            msg = f'"planId" and/or "robotId" do not exist, body={body}'
            logger.warning(f'status=400, {msg}')
            abort(400, {
                'result': 'failure',
                'message': msg,
            })

        plan_id = body['planId']
        robot_id = body['robotId']
        payload = self._make_stop_cmd()
        result = orion.send_command(const.FIWARE_SERVICE, const.FIWARE_SERVICEPATH, const.ROBOT_TYPE, robot_id, payload)
        logger.info(f'send a "{const.STOP_COMMAND}" command to orion, '
                    f'result_status={result.status_code}, payload={json.dumps(payload)}')

        if const.COMMAND_RESULT_WAIT_ENABLE:
            for i in range(const.COMMAND_RESULT_WAIT_MAX_NUM):
                entity = orion.get_entity(const.FIWARE_SERVICE, const.FIWARE_SERVICEPATH, const.ROBOT_TYPE, robot_id)
                logger.warning(entity['naviCmd_status']['value'])
                if entity['naviCmd_status']['value'] == const.COMMAND_RESULT_OK:
                    break
                logger.debug(f'wait until naviCmd_status is OK, cnt={i}')
                time.sleep(const.COMMAND_RESULT_WAIT_SEC)
            else:
                return jsonify({
                    'result': 'failure',
                    'planId': plan_id,
                    'robotId': robot_id,
                    'reason': 'naviCmd_satus is still PENDING'
                }), 409
        else:
            time.sleep(const.COMMAND_RESULT_WAIT_SEC)

        plan = orion.get_entity(const.FIWARE_SERVICE, const.FIWARE_SERVICEPATH, const.PLAN_TYPE, plan_id)
        payload = self._make_start_cmd(plan['waypoints']['value'])
        result = orion.send_command(const.FIWARE_SERVICE, const.FIWARE_SERVICEPATH, const.ROBOT_TYPE, robot_id, payload)

        logger.info(f'send a "{const.START_COMMAND}" command to orion, '
                    f'result_status={result.status_code}, payload={json.dumps(payload)}')

        return jsonify({
            'result': 'success',
            'planId': plan_id,
            'robotId': robot_id,
            'orion_status': result.status_code,
        }), 201
예제 #2
0
    def calc_state(self, is_navi, robot_id, robot_entity=None):
        if is_navi:
            return const.STATE_MOVING
        else:
            if not robot_entity:
                robot_entity = orion.get_entity(
                    const.FIWARE_SERVICE, const.DELIVERY_ROBOT_SERVICEPATH,
                    const.DELIVERY_ROBOT_TYPE, robot_id)
            navigating_waypoints = robot_entity['navigating_waypoints'][
                'value']
            order = robot_entity['order']['value']

            if not (isinstance(navigating_waypoints, dict) and 'to'
                    in navigating_waypoints and isinstance(order, dict)
                    and 'source' in order and 'destination' in order
                    and 'via' in order and isinstance(order['via'], list)):
                return const.STATE_STANDBY
            else:
                to = navigating_waypoints['to']
                if to == order['source']:
                    return const.STATE_STANDBY
                elif to == order['destination']:
                    try:
                        caller = Caller.value_of(
                            robot_entity['caller']['value'])
                        return const.STATE_DELIVERING if caller == Caller.ORDERING else const.STATE_PICKING
                    except ValueError as e:
                        logger.warning(
                            f'unkown caller (estimate "state" as const.STATE_PICKING), {e}'
                        )
                        return const.STATE_PICKING
                elif to in order['via']:
                    return const.STATE_PICKING
                else:
                    return const.STATE_MOVING
예제 #3
0
    def __check_navi(self, robot_id):
        current_mode = orion.get_entity(const.FIWARE_SERVICE,
                                        const.DELIVERY_ROBOT_SERVICEPATH,
                                        const.DELIVERY_ROBOT_TYPE,
                                        robot_id)['mode']['value']

        return current_mode == const.MODE_NAVI
예제 #4
0
 def get_destination_id(self, robot_id):
     navigating_waypoints = orion.get_entity(
         const.FIWARE_SERVICE, const.DELIVERY_ROBOT_SERVICEPATH,
         const.DELIVERY_ROBOT_TYPE,
         robot_id)['navigating_waypoints']['value']
     if not isinstance(navigating_waypoints,
                       dict) or 'destination' not in navigating_waypoints:
         return ''
     else:
         return navigating_waypoints['destination']
예제 #5
0
    def get_destination_name(self, robot_id):
        navigating_waypoints_to = self.get_destination_id(robot_id)
        if navigating_waypoints_to == '':
            return ''

        destination = orion.get_entity(const.FIWARE_SERVICE,
                                       const.DELIVERY_ROBOT_SERVICEPATH,
                                       const.PLACE_TYPE,
                                       navigating_waypoints_to)
        if not isinstance(destination, dict) or 'name' not in destination:
            return ''

        return destination['name']['value']
예제 #6
0
        def _move(cmd):
            payload = orion.make_delivery_robot_command(
                cmd, cmd_waypoints, navigating_waypoints,
                remaining_waypoints_list, current_routes, order, caller)
            orion.send_command(const.FIWARE_SERVICE,
                               const.DELIVERY_ROBOT_SERVICEPATH,
                               const.DELIVERY_ROBOT_TYPE, robot_id, payload)

            cnt = 0
            while cnt < const.MOVENEXT_WAIT_MAX_NUM:
                cnt += 1
                robot_entity = orion.get_entity(
                    const.FIWARE_SERVICE, const.DELIVERY_ROBOT_SERVICEPATH,
                    const.DELIVERY_ROBOT_TYPE, robot_id)
                if robot_entity['send_cmd_status']['value'] == 'OK':
                    break
                sleep(const.MOVENEXT_WAIT_MSEC / 1000.0)
            else:
                msg = f'send_cmd_status still pending, robot_id={robot_id}, ' \
                    f'wait_msec={const.MOVENEXT_WAIT_MSEC}, wait_count={cnt}'
                logger.error(msg)
                abort(500, {'message': msg})

            cmd_info = robot_entity['send_cmd_info']['value']
            if not (isinstance(cmd_info, dict) and 'result' in cmd_info):
                msg = f'invalid send_cmd_info, {cmd_info}'
                logger.error(msg)
                abort(500, {
                    'message': msg,
                })
            if cmd_info['result'] not in ['ack', 'ignore']:
                msg = f'move robot error, robot_id={robot_id}, errors="{cmd_info["errors"] if "errors" in cmd_info else ""}"'
                logger.error(msg)
                abort(500, {
                    'message': msg,
                })
            return cmd_info['result']
예제 #7
0
 def get_remaining_waypoints_list(self, robot_id):
     return orion.get_entity(const.FIWARE_SERVICE,
                             const.DELIVERY_ROBOT_SERVICEPATH,
                             const.DELIVERY_ROBOT_TYPE,
                             robot_id)['remaining_waypoints_list']['value']
예제 #8
0
    def post(self):
        logger.debug(f'RobotNotificationAPI.post')
        ignored_data = []
        processed_data = []

        for data in request.json['data']:
            robot_id = data['id']
            next_mode = data['mode']['value']
            time = dateutil.parser.parse(data['time']['value'])

            try:
                MongoThrottling.lock(robot_id, time)
                robot_entity = orion.get_entity(
                    const.FIWARE_SERVICE, const.DELIVERY_ROBOT_SERVICEPATH,
                    const.DELIVERY_ROBOT_TYPE, robot_id)

                next_state = self.calc_state(next_mode == const.MODE_NAVI,
                                             robot_id, robot_entity)
                current_mode = robot_entity['current_mode']['value']
                current_state = robot_entity['current_state']['value']
                last_processed_time = dateutil.parser.parse(
                    robot_entity['last_processed_time']['value'])
                ui_id = const.ID_TABLE[robot_id]

                payload = orion.make_updatelastprocessedtime_command(time)
                orion.send_command(const.FIWARE_SERVICE,
                                   const.DELIVERY_ROBOT_SERVICEPATH,
                                   const.DELIVERY_ROBOT_TYPE, robot_id,
                                   payload)
                logger.debug(
                    f'update robot last_processed_time, robot_id={robot_id}, '
                    f'time={time}, last_processed_time={last_processed_time}')

                if next_mode != current_mode:
                    payload = orion.make_updatemode_command(next_mode)
                    orion.send_command(const.FIWARE_SERVICE,
                                       const.DELIVERY_ROBOT_SERVICEPATH,
                                       const.DELIVERY_ROBOT_TYPE, robot_id,
                                       payload)
                    logger.info(
                        f'update robot state, robot_id={robot_id}, '
                        f'current_mode={current_mode}, next_mode={next_mode}')

                    self._action(robot_id, ui_id, robot_entity, next_mode)
                    self._send_state(robot_id, ui_id, next_state,
                                     current_state)
                    processed_data.append(data)
                else:
                    logger.debug(
                        f'ignore notification, next_mode={next_mode} current_mode={current_mode}'
                    )
                    ignored_data.append(data)
            except MongoLockError as e:
                logger.warning(str(e))
                ignored_data.append(data)

        logger.debug(
            f'processed_data = {processed_data}, ignored_data = {ignored_data}'
        )
        return jsonify({
            'result': 'success',
            'processed_data': processed_data,
            'ignored_data': ignored_data
        }), 200