|
|
@ -1,87 +1,33 @@
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Libraries
|
|
|
|
# Fuction recognize intents and connect them to the right actions
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
from time import sleep
|
|
|
|
from time import sleep
|
|
|
|
from logic import tts, read_script
|
|
|
|
from logic import tts, read_script
|
|
|
|
from config import characters, directions
|
|
|
|
from config import characters, directions
|
|
|
|
from speaker import google, on_waiting, on_message
|
|
|
|
from speaker import listen_to_speaker, on_waiting, on_message
|
|
|
|
from pixel_ring import pixel_ring
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# === SETUP OF MQTT PART 1 ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Location of the MQTT server
|
|
|
|
HOST = 'localhost'
|
|
|
|
HOST = 'localhost'
|
|
|
|
PORT = 1883
|
|
|
|
PORT = 1883
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Subscribe to relevant MQTT topics
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
print("Connected to {0} with result code {1}".format(HOST, rc))
|
|
|
|
print("Connected to {0} with result code {1}".format(HOST, rc))
|
|
|
|
# Subscribe to the text detected topic
|
|
|
|
client.subscribe('hermes/intent/jocavdh:play_intro_act') # to check for intent to play the act
|
|
|
|
client.subscribe("hermes/nlu/intentNotRecognized")
|
|
|
|
client.subscribe('hermes/intent/jocavdh:question_continue_act') # to check for the intent to continue to the next act
|
|
|
|
client.subscribe('hermes/intent/jocavdh:ask')
|
|
|
|
client.subscribe("hermes/nlu/intentNotRecognized") # to check if the speaker didn't understand the user, trigger for fallback function
|
|
|
|
client.subscribe('hermes/intent/jocavdh:ready')
|
|
|
|
|
|
|
|
client.subscribe('hermes/intent/jocavdh:answer_yes')
|
|
|
|
|
|
|
|
client.subscribe("hermes/asr/textCaptured")
|
|
|
|
|
|
|
|
client.subscribe("hermes/dialogueManager/sessionQueued")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_ready(client,data,msg, line_number = 0):
|
|
|
|
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
|
|
|
|
sessionId = data['sessionId']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
script_lines = read_script('play_scripts/interruption.txt')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for character, line, direction in script_lines[line_number:]:
|
|
|
|
# === FUNCTIONS THAT ARE TRIGGERED WHEN AN INTENT IS DETECTED ===
|
|
|
|
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)
|
|
|
|
|
|
|
|
line_number +=1
|
|
|
|
|
|
|
|
print('say this sentence')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if action == 'listen_audience':
|
|
|
|
|
|
|
|
print('listen to the audience')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.publish('hermes/dialogueManager/endSession', json.dumps({
|
|
|
|
# Function which is triggered when the intent play_intro_act is activated
|
|
|
|
'sessionId': sessionId
|
|
|
|
def on_play_intro_act(client,data,msg):
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.publish('hermes/dialogueManager/startSession', json.dumps({
|
|
|
|
|
|
|
|
'siteId': 'default',
|
|
|
|
|
|
|
|
'init': {'type': 'action', 'canBeEnqueued': True, 'intentFilter':['jocavdh:answer_yes']}
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if action == 'listen_google_home':
|
|
|
|
|
|
|
|
# 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, 'intentFilter':['jocavdh:answer_yes']}
|
|
|
|
|
|
|
|
# }))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # client.publish("hermes/asr/toggleOn")
|
|
|
|
|
|
|
|
# # client.publish('hermes/asr/startListening', json.dumps({
|
|
|
|
|
|
|
|
# # 'siteId': 'default'
|
|
|
|
|
|
|
|
# # }))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return line_number
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
google()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#break
|
|
|
|
|
|
|
|
sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_introduce(client,data,msg, line_number = 0):
|
|
|
|
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
sessionId = data['sessionId']
|
|
|
|
sessionId = data['sessionId']
|
|
|
|
|
|
|
|
|
|
|
@ -113,29 +59,10 @@ def on_introduce(client,data,msg, line_number = 0):
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if action == 'listen_google_home':
|
|
|
|
if action == 'listen_google_home':
|
|
|
|
# print('ok google')
|
|
|
|
listen_to_speaker()
|
|
|
|
# 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, 'intentFilter':['jocavdh:answer_yes']}
|
|
|
|
|
|
|
|
# }))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # client.publish("hermes/asr/toggleOn")
|
|
|
|
|
|
|
|
# # client.publish('hermes/asr/startListening', json.dumps({
|
|
|
|
|
|
|
|
# # 'siteId': 'default'
|
|
|
|
|
|
|
|
# # }))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return line_number
|
|
|
|
# Function which is triggered when the intent question_continue_act is activated
|
|
|
|
|
|
|
|
def on_question_continue_act(client,data,msg):
|
|
|
|
google()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_answer(client,data,msg):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
answer_value = data['slots'][0]['value']['value']
|
|
|
|
answer_value = data['slots'][0]['value']['value']
|
|
|
@ -169,6 +96,7 @@ def on_answer(client,data,msg):
|
|
|
|
|
|
|
|
|
|
|
|
print('The play is over.')
|
|
|
|
print('The play is over.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Function which is triggered when no intent is recognized
|
|
|
|
def onIntentNotRecognized(client, data, msg):
|
|
|
|
def onIntentNotRecognized(client, data, msg):
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
data = json.loads(msg.payload)
|
|
|
|
line_number = 1
|
|
|
|
line_number = 1
|
|
|
@ -178,13 +106,17 @@ def onIntentNotRecognized(client, data, msg):
|
|
|
|
on_introduce(client,data,msg, line_number)
|
|
|
|
on_introduce(client,data,msg, line_number)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# === SETUP OF MQTT PART 2 ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initialise MQTT client
|
|
|
|
client = mqtt.Client()
|
|
|
|
client = mqtt.Client()
|
|
|
|
client.connect(HOST, PORT, 60)
|
|
|
|
client.connect(HOST, PORT, 60)
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.message_callback_add('hermes/intent/jocavdh:ready', on_ready)
|
|
|
|
|
|
|
|
client.message_callback_add('hermes/intent/jocavdh:ask', on_introduce)
|
|
|
|
# Connect each MQTT topic to which you subscribed to a handler function
|
|
|
|
client.message_callback_add('hermes/intent/jocavdh:answer_yes', on_answer)
|
|
|
|
client.message_callback_add('hermes/intent/jocavdh:play_intro_act', on_play_intro_act)
|
|
|
|
|
|
|
|
client.message_callback_add('hermes/intent/jocavdh:question_continue_act', on_question_continue_act)
|
|
|
|
client.message_callback_add("hermes/nlu/intentNotRecognized", onIntentNotRecognized)
|
|
|
|
client.message_callback_add("hermes/nlu/intentNotRecognized", onIntentNotRecognized)
|
|
|
|
print('main')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Keep checking for new MQTT messages
|
|
|
|
client.loop_forever()
|
|
|
|
client.loop_forever()
|