forked from dielfilho-zz/linuxspeech
/
command_handler.py
56 lines (42 loc) · 1.3 KB
/
command_handler.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
# -*- coding: utf-8 -*-
import subprocess
import shlex
from voice_recognizer import VoiceRecognizer
from espeak import espeak
from action_manager import ActionManager
class CommandHandler(object):
CONST_TURN_OFF = "turn off"
CONST_TURN_ON = "turn on"
CONST_GOODBYE = "goodbye"
def __init__(self):
espeak.synth("Hello Master!")
self.rec = VoiceRecognizer()
self.actions = ActionManager().loadActions()
self.canHear = True;
def callSubProcess(self, args, text):
try:
espeak.synth(text)
args = args.split(" ")
subprocess.Popen(args)
except Exception as e:
print "Error on callSubProcess: "+str(e)
espeak.synth("Sorry, I can't "+text)
def handle(self):
voiceCommand = self.rec.speechToText().lower()
if voiceCommand != "":
if self.CONST_TURN_OFF in voiceCommand:
espeak.synth("Turning off the microphone")
self.canHear = False
elif self.CONST_TURN_ON in voiceCommand:
espeak.synth("Turning on the microphone")
self.canHear = True
return True
if self.canHear:
if self.CONST_GOODBYE in voiceCommand:
espeak.synth("bye")
return False
espeak.synth("You said "+voiceCommand)
for action in self.actions:
if voiceCommand in action.voiceCommand:
self.callSubProcess(action.args, "Executing "+voiceCommand)
return True