From da58fc7026a3376429320c47aa6f5a09e2fb0231 Mon Sep 17 00:00:00 2001 From: jocavdh Date: Thu, 23 May 2019 13:45:18 +0200 Subject: [PATCH] finally google home handler fixed --- action-play.py | 39 ++++++++++-------- logic.py | 2 +- play_scripts/demo.txt | 4 +- speaker.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 speaker.py diff --git a/action-play.py b/action-play.py index 1778340..9c9988c 100755 --- a/action-play.py +++ b/action-play.py @@ -8,6 +8,7 @@ import json from time import sleep from logic import tts, read_script from config import characters, directions +from speaker import google, on_waiting, on_message HOST = 'localhost' PORT = 1883 @@ -19,6 +20,8 @@ def on_connect(client, userdata, flags, rc): client.subscribe("hermes/nlu/intentNotRecognized") client.subscribe('hermes/intent/jocavdh:ask') client.subscribe('hermes/intent/jocavdh:answer_yes') + client.subscribe("hermes/asr/textCaptured") + client.subscribe("hermes/dialogueManager/sessionQueued") def on_introduce(client,data,msg, line_number = 0): data = json.loads(msg.payload) @@ -52,24 +55,27 @@ def on_introduce(client,data,msg, line_number = 0): break if action == 'listen_google_home': - print('ok google') - client.publish('hermes/dialogueManager/endSession', json.dumps({ - 'sessionId': sessionId - })) + # print('ok google') + # client.publish('hermes/dialogueManager/endSession', json.dumps({ + # 'sessionId': sessionId + # })) - client.publish('hermes/dialogueManager/startSession', json.dumps({ - 'siteId': 'default', - 'init': {'type': 'action', 'canBeEnqueued': True, 'sendIntentNotRecognized': True} - })) - # client.publish("hermes/asr/toggleOn") - # client.publish('hermes/asr/startListening', json.dumps({ - # 'siteId': 'default' + # client.publish('hermes/dialogueManager/startSession', json.dumps({ + # 'siteId': 'default', + # 'init': {'type': 'action', 'canBeEnqueued': True, 'sendIntentNotRecognized': True, 'intentFilter':['jocavdh:answer_yes']} # })) - return line_number + # # client.publish("hermes/asr/toggleOn") + # # client.publish('hermes/asr/startListening', json.dumps({ + # # 'siteId': 'default' + # # })) - break + # return line_number + + google() + + #break def on_answer(client,data,msg): @@ -94,10 +100,11 @@ def on_answer(client,data,msg): def onIntentNotRecognized(client, data, msg): data = json.loads(msg.payload) - start_line = on_introduce() - print('not recognized') + line_number = 1 + print('We continue on line') + print(line_number) - on_introduce(client,data,msg, start_line) + on_introduce(client,data,msg, line_number) client = mqtt.Client() diff --git a/logic.py b/logic.py index 3914c31..6efdc94 100644 --- a/logic.py +++ b/logic.py @@ -72,7 +72,7 @@ def tts(voice, input_text, speaker): # aplay -D mono3 /tmp/output_wav.wav - call(["aplay", fpath]) + call(["aplay", "-D", "sysdefault:CARD=ArrayUAC10", fpath]) else: diff --git a/play_scripts/demo.txt b/play_scripts/demo.txt index 97497c0..11507c5 100644 --- a/play_scripts/demo.txt +++ b/play_scripts/demo.txt @@ -1,2 +1,2 @@ -ROGUE: [Listens to Google Home] Give me -SAINT: Do that. \ No newline at end of file +ROGUE: [Listens to Google Home] Hi, I am Rogue. I kidnapped this little speaker. O K Google, please introduce yourself. +SAINT: I am Saint, and I have this dreadfully sad voice. \ No newline at end of file diff --git a/speaker.py b/speaker.py new file mode 100644 index 0000000..cb930a4 --- /dev/null +++ b/speaker.py @@ -0,0 +1,92 @@ +from logic import tts, read_script +from config import characters, directions + +# Fuction to RUN THE PLAY +import paho.mqtt.client as mqtt +import json +from time import sleep + +HOST = 'localhost' +PORT = 1883 + + +def on_connect(client, userdata, flags, rc): + print("Connected to {0} with result code {1}".format(HOST, rc)) + # Subscribe to the text detected topic + client.subscribe("hermes/asr/textCaptured") + client.subscribe("hermes/dialogueManager/sessionQueued") + + +def on_message(client, userdata, msg): + print('Google Home is not speaking anymore') + client.connected_flag=True + +def on_waiting(client, userdata, msg): + sessionId = json.loads(id.payload) + print('delete mistaken intent') + client.publish("hermes/dialogueManager/endSession", json.dumps({ + 'sessionId': sessionId, + })) + +def google(): + client = mqtt.Client() + client.connect(HOST, PORT, 60) + client.on_connect = on_connect + client.connected_flag=False + + listening = False + switch = 5 + + if switch == 5: + + + + # for character, line, direction in read_script('play_scripts/introduction.txt'): + # input_text = line + # voice = characters.get(character)[0] + # speaker = characters.get(character)[1] + # #speaker = 'default' + # # Some way to do something with the stage directions will come here + # action = directions.get(direction[0]) + # tts(voice, input_text, speaker) + switch = 5 + + if switch == 5: + print('Waiting for the Google Home to finish its talk') + + # # start voice activity detection + # client.publish("hermes/asr/startListening", json.dumps({ + # 'siteId': 'default', + # 'init': { + # 'type': 'action', + # 'canBeEnqueued': True + # } + # })) + + client.publish("hermes/asr/startListening", json.dumps({ + 'siteId': 'default' + })) + + # create callback + client.on_message = on_message + listening = True + + while listening: + client.loop() + + #client.on_message = on_message + client.message_callback_add('hermes/asr/textCaptured', on_message) + + if client.connected_flag: + sleep(1) + print('Continue the play') + client.connected_flag = False + client.message_callback_add('hermes/dialogueManager/sessionQueued', on_waiting) + break + + sleep(1) + + + print('The play is over.') + + # return \ No newline at end of file