TSA-demo/TSA_mecha/finalflow-mqtt-dev-local.py [109:213]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                print("Distance is ", distance)
                #messages = json.dumps({"message" : "distance is %s" %distance, "lex-recognition-edge": "no"})
                #print('messages: ', messages)
                #pub = mqtt_publish(messages)
                #print('pub: ', pub)
                #distance = 25
                if 20 <= distance <= 180:
                    #Kill the AVAintro process
                    processKill()
                    print('background_thread', background_thread)
                    stop_threads = True
                    print('background_thread is_alive: ', threading.enumerate())
                    background_thread = threading.Thread(target=initialAvaIntroServo, name='initialAvaIntroServo'+str(uuid.uuid4()), args =(lambda : stop_threads, ))
                    firstloop = 0
                    stop_threadsName = False
                    stop_threadsAvadoyouhavequestion = False
                    print ("Start Lex flow")
                    try:
                        print('intro counter: ', intro)
                        if intro == 0:
                            lexcall = 0
                            print('intro counter: ', intro)
                            myNameisAvaServo_thread.start()
                            mqtt_publish('{"message": "I am TSA Bot.\\n How can I help?", "presence": "request" }')
                            welcome = '''
                            <speak>
                            Hello. I am T. S. A. Bot. How can I help?
                            You can say .<prosody volume="+12dB"> <lang xml:lang="en-ES">  Hola! </lang> </prosody> for spanish! <break time="1s"/>
                            </speak>
                            '''
                            synthesize_speech(welcome, 'ssml')
                            intro +=1
                            detect = 0
                            print('intro counter after intro: ', intro)
                        else:
                            print('intro counter follow up: ', intro)
                            if platform_sys !='Darwin':
                                set_color("white")
                                time.sleep(0.5)
                                set_color("off")
                            time.sleep(1)
                            if(langcode == 'en_US'):
                                synthesize_speech("Do you have a question?.", 'text')
                                mqtt_publish('{"message": "Do you have a question?", "presence": "request" }')
                            else:
                                synthesize_speech("<speak> <lang xml:lang='en-ES'>Tienes una pregunta? </lang> </speak>", 'ssml')
                                mqtt_publish('{"message": "Tienes una pregunta?", "presence": "request" }')
                            print('starting Avadoyouhavequestion_thread')
                            Avadoyouhavequestion_thread.start()
                            time.sleep(0.3)
                        session_id = str(uuid.uuid4())
                        if platform_sys != 'Darwin':
                            set_color('blue')
                        print('starting recording')
                        recording = record_phrase()
                        #print('recording: ', recording)
                        if detect == 0:
                            mqtt_publish('{"message": "Detecting language...", "presence": "request" }')
                            synthesize_speech('<speak> Detecting language. <amazon:effect phonation="soft"> Please wait.</amazon:effect> </speak>', 'ssml')
                            detect +=1
                        response = call_lexv2(session_id, bytes(recording), langcode)
                        #print('response: ', response)
                        #if platform_sys != 'Darwin':
                        #    set_color('off')
                        base64_message = decode(response['messages'])[0]['content']
                        #base64_message = 'Let me identify your items.'
                        print('base64_message', base64_message)
                        ##ADDED
                        if (lexcall == 0):
                            session_ides = str(uuid.uuid4())
                            responsees = call_lexv2(session_ides, bytes(recording), 'es_US')
                            print('responsesss: ',responsees)
                            base64_messagees = decode(responsees['messages'])[0]['content']
                            print('base64_messagees', base64_messagees)
                            ##END ADDED
                            if base64_messagees == 'hola':
                            #if base64_message == 'hola':
                                langcode='es_US'
                                #play_audio(responsees['audioStream'])
                                synthesize_speech("<speak> <lang xml:lang='en-ES'>Hola! Cambio de idioma to español </lang> </speak>", 'ssml')
                            lexcall +=1
                            unknownquestion = 0
                        if base64_message == 'I do not know how to answer your question.' and langcode == 'es_US' and unknownquestion == 0 :
                            synthesize_speech("<speak> <lang xml:lang='en-ES'>No sé cómo responder a la pregunta</lang> </speak>", 'ssml')
                            mqtt_publish('{"message": "No sé cómo responder a la pregunta", "presence": "request" }')
                        if base64_message == 'I do not know how to answer your question.' and unknownquestion == 0 :
                            print('Ignore')
                        else:
                            print('sending mqtt and playing audio')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(base64_message) )
                            play_audio(response['audioStream'])
                        unknownquestion += 1
                        if base64_message == 'Checking flight schedule to Boston.' :
                            envol = scanflights('BOS')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Checking flight schedule to New-York.' :
                            envol = scanflights('JFK')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Checking flight schedule to Los Angeles.' :
                            envol = scanflights('LAX')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Let me identify your items.' or base64_message == 'Permítanme identificar sus artículos.':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



TSA-demo/TSA_mecha/finalflow-mqtt-dev-rasp.py [95:193]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                print("Distance is ", distance)
                if 20 <= distance <= 180:
                    #Kill the AVAintro process
                    processKill()
                    print('background_thread', background_thread)
                    stop_threads = True
                    print('background_thread is_alive: ', threading.enumerate())
                    background_thread = threading.Thread(target=initialAvaIntroServo, name='initialAvaIntroServo'+str(uuid.uuid4()), args =(lambda : stop_threads, ))
                    firstloop = 0
                    stop_threadsName = False
                    stop_threadsAvadoyouhavequestion = False
                    print ("Start Lex flow")
                    try:
                        print('intro counter: ', intro)
                        if intro == 0:
                            lexcall = 0
                            print('intro counter: ', intro)
                            myNameisAvaServo_thread.start()
                            mqtt_publish('{"message": "I am TSA Bot.\\n How can I help?", "presence": "request" }')
                            welcome = '''
                            <speak>
                            Hello. I am T. S. A. Bot. How can I help?
                            You can say .<prosody volume="+12dB"> <lang xml:lang="en-ES">  Hola! </lang> </prosody> for spanish! <break time="1s"/>
                            </speak>
                            '''
                            synthesize_speech(welcome, 'ssml')
                            intro +=1
                            detect = 0
                            print('intro counter after intro: ', intro)
                        else:
                            print('intro counter follow up: ', intro)
                            if platform_sys !='Darwin':
                                set_color("white")
                                time.sleep(0.5)
                                set_color("off")
                            time.sleep(1)
                            if(langcode == 'en_US'):
                                synthesize_speech("Do you have a question?.", 'text')
                                mqtt_publish('{"message": "Do you have a question?", "presence": "request" }')
                            else:
                                synthesize_speech("<speak> <lang xml:lang='en-ES'>Tienes una pregunta? </lang> </speak>", 'ssml')
                                mqtt_publish('{"message": "Tienes una pregunta?", "presence": "request" }')
                            print('starting Avadoyouhavequestion_thread')
                            Avadoyouhavequestion_thread.start()
                            time.sleep(0.3)
                        session_id = str(uuid.uuid4())
                        if platform_sys != 'Darwin':
                            set_color('blue')
                        print('starting recording')
                        recording = record_phrase()
                        #print('recording: ', recording)
                        if detect == 0:
                            mqtt_publish('{"message": "Detecting language...", "presence": "request" }')
                            synthesize_speech('<speak> Detecting language. <amazon:effect phonation="soft"> Please wait.</amazon:effect> </speak>', 'ssml')
                            detect +=1
                        response = call_lexv2(session_id, bytes(recording), langcode)
                        #print('response: ', response)
                        #if platform_sys != 'Darwin':
                        #    set_color('off')
                        base64_message = decode(response['messages'])[0]['content']
                        #base64_message = 'Let me identify your items.'
                        print('base64_message', base64_message)
                        ##ADDED
                        if (lexcall == 0):
                            session_ides = str(uuid.uuid4())
                            responsees = call_lexv2(session_ides, bytes(recording), 'es_US')
                            print('responsesss: ',responsees)
                            base64_messagees = decode(responsees['messages'])[0]['content']
                            print('base64_messagees', base64_messagees)
                            ##END ADDED
                            if base64_messagees == 'hola':
                                langcode='es_US'
                                #play_audio(responsees['audioStream'])
                                synthesize_speech("<speak> <lang xml:lang='en-ES'>Hola! Cambio de idioma to español </lang> </speak>", 'ssml')
                            lexcall +=1
                            unknownquestion = 0
                        if base64_message == 'I do not know how to answer your question.' and langcode == 'es_US' and unknownquestion == 0 :
                            synthesize_speech("<speak> <lang xml:lang='en-ES'>No sé cómo responder a la pregunta</lang> </speak>", 'ssml')
                            mqtt_publish('{"message": "No sé cómo responder a la pregunta", "presence": "request" }')
                        if base64_message == 'I do not know how to answer your question.' and unknownquestion == 0 :
                            print('Ignore')
                        else:
                            print('sending mqtt and playing audio')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(base64_message) )
                            play_audio(response['audioStream'])
                        unknownquestion += 1
                        if base64_message == 'Checking flight schedule to Boston.' :
                            envol = scanflights('BOS')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Checking flight schedule to New-York.' :
                            envol = scanflights('JFK')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Checking flight schedule to Los Angeles.' :
                            envol = scanflights('LAX')
                            mqtt_publish('{"message": "%s", "presence": "request" }' %(str(envol)) )
                            synthesize_speech("%s" %(str(envol)), 'text')
                        if base64_message == 'Let me identify your items.' or base64_message == 'Permítanme identificar sus artículos.':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



