forked from MycroftAI/mycroft-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi_thread_skill.py
82 lines (63 loc) · 2.33 KB
/
multi_thread_skill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import threading
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill
from mycroft.util.log import getLogger
from mycroft.messagebus.message import Message
logger = getLogger(__name__)
class SkillSession(object):
def __init__(self, id, skill):
self.id = id
self.skill = skill
self.msg_event = threading.Event()
#
# async to sync
#
def wait( self ):
self.msg_event.clear()
self.msg_event.wait()
#
# core methods
#
def record( self, rec_chars={} ):
self.skill.emitter.emit(
Message("record", rec_chars,
context={ "session": self.id } ) )
def speak(self, utterance, expect_response=False ):
context={ "session": self.id }
data = {'utterance': utterance,
'expect_response': expect_response}
self.skill.emitter.emit(Message("speak", data, context = context ))
def speak_dialog(self, key, data={}, expect_response=False ):
self.speak(
self.skill.dialog_renderer.render(key, data),
expect_response = expect_response )
class MultiThreadSkill(MycroftSkill):
def __init__(self, name, session_class):
super(MultiThreadSkill,self).__init__(name)
self.session_class = session_class
self.sessions = {}
if not self.config:
self.config={}
def initialize(self):
self.on('recognizer_loop:audio_output_end')
self.on('recognizer_loop:record_end')
# Override
def register_intent( self, intent, f ):
def _wrapper(msg):
session_id = msg.context.get("session")
session = self.session_class( session_id, self )
self.sessions[session_id] = session
thread = threading.Thread( target = f, args = [ session, intent ] )
thread.start()
super(MultiThreadSkill,self).register_intent( intent, _wrapper)
def on( self, event ):
def _execute(msg):
try:
session_id = msg.context.get("session")
self.session = self.sessions.get(session_id)
if self.session:
self.session.msg_event.set()
except:
logger.error("error handling %s", event, exc_info=True)
throw
self.emitter.on(event, _execute)