def speak(person_name, location_name, ignore_motion, ignore_confirmation, voice_id, no_audio, simulate, fail_confirm): try: while True: click.echo("Checking for messages . . .") lm = LocationManager() loc = lm.get_location(location_name) if not ignore_motion and not loc.is_motion: print 'Exiting. No motion detected at ' + location_name exit(1) speaker = Speaker(NoAudio=no_audio) message_manager = MessageManager(LocationName=location_name) click.echo('getting files') cache_manager = CacheManager(BucketName='pollexy-media', CacheName='chimes') cache_manager.sync_remote_folder() print 'Writing speech' vid, speech = message_manager.write_speech(PersonName=person_name) if vid: voice_id = vid if not speech: print "I have nothing to say" message_manager.delete_sqs_msgs() else: print "Speech:\n\n%s" % speech try: pm = PersonManager() p = pm.get_person(person_name) do_speech = True if fail_confirm: logging.warning("FORCE FAILING confirmation") reason, do_speech = "NoResponse", False elif not no_audio and p.require_physical_confirmation and \ not ignore_confirmation: lv = LocationVerification(PersonName=person_name, LocationName=location_name, VoiceId=voice_id) do_speech, retry_count, timeout = \ lv.verify_person_at_location(SpeechMethod=say) if do_speech: speaker = Speaker(NoAudio=no_audio) speaker.generate_audio(Message=speech, TextType='ssml', VoiceId=voice_id) speaker.speak(IncludeChime=True) message_manager.succeed_speech(dont_delete=simulate) else: message_manager.fail_speech(Reason=reason) finally: speaker.cleanup() except Exception as exc: exc_type, exc_value, exc_traceback = sys.exc_info() print repr( traceback.format_exception(exc_type, exc_value, exc_traceback)) click.echo("Error: %s" % str(exc)) exit(2)
def test_bad_invalid_person_throws_error(text_mock): text_mock.return_value = invalid_user_response lv = LocationVerification(LocationName='room', PersonName='calvin') with pytest.raises(ValueError) as exc: assert lv.verify_valid_user() assert 'Person does not exist' in str(exc.value)
def test_person_req_phys_conf_sets_req_phys_conf_for_loc_verify(p_mock): pm = PersonManager() p = Person(Name='calvin') p.require_physical_confirmation = True pm.update_window_set(p) lv = LocationVerification(LocationName='room', PersonName='calvin') assert lv.person.require_physical_confirmation
def test_verify_switch_with_press_returns_true(): rpi_mock.GPIO.setmode.return_value = None rpi_mock.GPIO.setup.return_value = None rpi_mock.GPIO.input.return_value = False pm = PersonManager() p = Person(Name='calvin') p.require_physical_confirmation = True pm.update_window_set(p) l = LocationAvailability(LocationName='kitchen') id = 12 l.with_switch(HardwareId=id, Color='Red', Name='Test', Style='Circle') lm = LocationManager() lm.update_input_capabilities(l) lv = LocationVerification(LocationName='kitchen', PersonName='calvin') done, count, timeout = lv.verify_person_at_location() print done, count, timeout assert done and count == 1 and timeout < 3
def message_say(person_name, location_name, message, voice_id): try: lv = LocationVerification(PersonName=person_name, LocationName=location_name, VoiceId=voice_id) done, retry_count, timeout = \ lv.verify_person_at_location(SpeechMethod=say) if done: say(Message=message, VoiceId=voice_id) else: print "Can't verify {} at location".format(person_name) except Exception as exc: exc_type, exc_value, exc_traceback = sys.exc_info() print repr(traceback.format_exception(exc_type, exc_value, exc_traceback)) click.echo("Error: %s" % str(exc)) exit(2)
def speak(person_name, location_name, ignore_motion, ignore_confirmation, voice_id, no_audio, simulate, fail_confirm, verbose): log = logging.getLogger('PollexyCli') if verbose: os.environ['LOG_LEVEL'] = 'DEBUG' log.setLevel(logging.DEBUG) try: while True: lm = LocationManager() loc = lm.get_location(location_name) if not ignore_motion and not loc.is_motion: print 'Exiting. No motion detected at ' + location_name exit(1) speaker = Speaker(NoAudio=no_audio) message_manager = MessageManager(LocationName=location_name) bm = message_manager.get_messages(MessageType='Bot', PersonName=person_name) if bm and len(bm) > 0: log.debug('Bot count = {}'.format(len(bm))) for bot in bm: username = str(uuid.uuid4()) try: lp = LexPlayer( BotNames=bot.bot_names, Alias="$LATEST", Username=username, VoiceId=voice_id, IceBreaker=bot.ice_breaker, Introduction=bot.introduction, NoAudio=no_audio, BotsRequired=bot.required_bots) while (not lp.is_done): lp.get_user_input() except Exception as e: print 'Bot failed: {}'.format(e) raise message_manager.succeed_messages(dont_delete=simulate) cache_manager = CacheManager(BucketName='pollexy-media', CacheName='chimes') cache_manager.sync_remote_folder() vid, speech = message_manager.write_speech(PersonName=person_name) if vid: voice_id = vid if not speech: message_manager.succeed_messages(dont_delete=simulate) message_manager.delete_sqs_msgs() else: try: pm = PersonManager() p = pm.get_person(person_name) do_speech = True if fail_confirm: log.warn("FORCE FAILING confirmation") reason, do_speech = "NoResponse", False elif not no_audio and p.require_physical_confirmation and \ not ignore_confirmation: lv = LocationVerification(PersonName=person_name, LocationName=location_name, VoiceId=voice_id) do_speech, retry_count, timeout = \ lv.verify_person_at_location(SpeechMethod=say) log.debug('do_speech={}'.format(bool(do_speech))) if fail_confirm: message_manager.fail_messages(Reason=reason) else: if do_speech: log.debug('starting speech') speaker = Speaker(NoAudio=no_audio) speaker.generate_audio(Message=speech, TextType='ssml', VoiceId=voice_id) speaker.speak(IncludeChime=True) log.debug('Succeeding messages') message_manager.succeed_messages(dont_delete=simulate) finally: speaker.cleanup() except Exception as exc: exc_type, exc_value, exc_traceback = sys.exc_info() print repr(traceback.format_exception(exc_type, exc_value, exc_traceback)) click.echo("Error: %s" % str(exc)) exit(2)
def test_location_verify_sets_person(p_mock): p = Person(Name='calvin') p.require_physical_confirmation = True p_mock.return_value = p lv = LocationVerification(LocationName='room', PersonName='calvin') assert p.name == lv.person_name
def test_confirm_invalid_user_location_returns_not_understood(text_mock): text_mock.return_value = cannot_understand_response lv = LocationVerification(LocationName='room', PersonName='calvin') resp = lv.send_confirm_response(TextResponse='asdasdsdasd') assert resp == 'NotUnderstood'
def test_confirm_user_location_returns_true(text_mock): text_mock.return_value = user_confirmed_response lv = LocationVerification(LocationName='room', PersonName='calvin') resp = lv.send_confirm_response(TextResponse='OK') assert resp == 'Confirmed'
def test_can_send_verify_location_request(text_mock): text_mock.return_value = verify_location_response lv = LocationVerification(LocationName='room', PersonName='calvin') assert lv.verify_valid_user()