2021-01-01 22:46:49 +01:00
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
|
2020-12-21 16:08:28 +01:00
|
|
|
from mycroft import MycroftSkill, intent_file_handler
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
def emit(osd_url, event, data):
|
|
|
|
json = {
|
|
|
|
"event": event,
|
|
|
|
"data": data
|
|
|
|
}
|
|
|
|
print(json)
|
|
|
|
return requests.post(osd_url, json=json)
|
|
|
|
|
|
|
|
class CarManual(MycroftSkill):
|
|
|
|
def __init__(self):
|
|
|
|
MycroftSkill.__init__(self)
|
2021-01-01 22:46:49 +01:00
|
|
|
# Generate the files
|
|
|
|
root_path = str(Path(__file__).absolute().parent)
|
|
|
|
root_locale_path = root_path + "/locale/en-us"
|
|
|
|
with open(root_path + "/questions.json") as questions_file:
|
|
|
|
questions = json.load(questions_file)
|
|
|
|
self.questions = questions
|
|
|
|
|
|
|
|
for qid, question in questions.items():
|
|
|
|
with open(f"{root_locale_path}/{qid}.intent", "w") as intent_file:
|
|
|
|
for intent in question["questions"]:
|
|
|
|
intent_file.write(intent)
|
|
|
|
intent_file.write("\n")
|
|
|
|
|
|
|
|
with open(f"{root_locale_path}/{qid}.dialog", "w") as dialog_file:
|
|
|
|
for dialog in question["answers"]:
|
|
|
|
dialog_file.write(dialog)
|
|
|
|
dialog_file.write("\n")
|
|
|
|
|
|
|
|
def initialize(self):
|
2021-01-03 14:09:04 +01:00
|
|
|
def handler(intent):
|
|
|
|
return lambda message: self.generic_handler(intent, message)
|
2021-01-01 22:46:49 +01:00
|
|
|
for key in self.questions:
|
2021-01-03 14:09:04 +01:00
|
|
|
self.register_intent(f"{key}.intent", handler(key))
|
2020-12-21 16:08:28 +01:00
|
|
|
|
2021-01-01 22:46:49 +01:00
|
|
|
def generic_handler(self, intent_id, message):
|
|
|
|
title = self.questions[intent_id]["title"]
|
|
|
|
dialog = self.dialog_renderer.render(intent_id, {})
|
2020-12-21 16:08:28 +01:00
|
|
|
url = self.settings.get("OSD_API", "http://localhost:5050/send")
|
|
|
|
emission = {
|
|
|
|
"plugin": "manual",
|
|
|
|
"data": {
|
2021-01-01 22:46:49 +01:00
|
|
|
"title": title,
|
2020-12-21 16:08:28 +01:00
|
|
|
"description": dialog
|
|
|
|
},
|
2021-01-04 18:44:57 +01:00
|
|
|
"time": len(dialog) * 50 + 3000
|
2020-12-21 16:08:28 +01:00
|
|
|
}
|
|
|
|
emit(url, "switchPlugin", emission)
|
|
|
|
self.speak(dialog)
|
|
|
|
|
|
|
|
|
|
|
|
def create_skill():
|
|
|
|
return CarManual()
|
|
|
|
|