-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand_handler.py
More file actions
60 lines (46 loc) · 1.57 KB
/
Copy pathcommand_handler.py
File metadata and controls
60 lines (46 loc) · 1.57 KB
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
# -*- 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()
# Variable used for command voice control, if True the voice command is executed
self.canHear = True;
def callSubProcess(self, args, text):
try:
espeak.synth(text)
# Breaks the arguments to execute on terminal.
args = args.split(" ")
# Execute the command on terminal
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:
# For each action on list of actions if voice command is found on list and he's active, he's is executed
if voiceCommand in action.voiceCommand:
self.callSubProcess(action.args, "Executing "+voiceCommand)
return True