def monitor_loop(client, callback=None, interval_seconds=1): """Call out to 'callback' when a question is seen in mission state.""" answered_id = None mission_id = None while True: time.sleep(interval_seconds) state = client.get_state() logger.debug('Got state: \n%s', str(state)) if not state.questions: continue # If there is more than one question, this question may not be for us. if len(state.questions) != 1: logger.warning( 'There are more questions (%i > 1) than I expected!', len(state.questions)) continue question = state.questions[0] # If there are unexpected answer codes, this question may not be for us. available_answer_codes = set(option.answer_code for option in question.options) if available_answer_codes != constants.valid_answer_codes: logger.warning('There are different options than I expected!') continue answer_code = callback(question.text) logger.info('Replying to question %i', question.id) try: client.answer_question(question.id, answer_code) except (RpcError, ResponseError) as exc: logger.exception('Error while answering %i', question.id)
def main(): logger = logging.getLogger(__name__) parser = argparse.ArgumentParser() bosdyn.client.util.add_common_arguments(parser) options = parser.parse_args() bosdyn.client.util.setup_logging(options.verbose) # Create an SDK that knows about the MissionClient type. sdk = bosdyn.client.create_standard_sdk('get-mission-state-example', [MissionClient]) robot = sdk.create_robot(options.hostname) robot.authenticate(options.username, options.password) client = robot.ensure_client(MissionClient.default_service_name) state = client.get_state() logger.info('Got mission state:\n%s', str(state))
def monitor_loop(client, interval_seconds=1): """Call out to 'callback' when a question is seen in mission state.""" while True: time.sleep(interval_seconds) state = client.get_state() LOGGER.debug('Got state: \n%s', str(state)) if not state.questions: continue # Just grab the first question available. There may be more! question = state.questions[0] answer_code = ask_user_for_answer(question) LOGGER.info('Replying to question %i', question.id) try: client.answer_question(question.id, answer_code) except (bosdyn.client.RpcError, bosdyn.client.ResponseError): LOGGER.exception('Error while answering %i', question.id)
def monitor_loop(client, callback, interval_seconds=1): """Call out to 'callback' when a question is seen in mission state.""" while True: time.sleep(interval_seconds) state = client.get_state() LOGGER.debug('Got state: \n%s', str(state)) if not state.questions: continue # Just grab the first question available. There may be more! question = state.questions[0] available_options = set(option.answer_code for option in question.options) if available_options != set([SUCCESS_CODE, FAILURE_CODE]): LOGGER.warning('Available options did not match expected options!') answer_code = callback(question.text) LOGGER.info('Replying to question %i', question.id) try: client.answer_question(question.id, answer_code) except (bosdyn.client.RpcError, bosdyn.client.ResponseError): LOGGER.exception('Error while answering %i', question.id)