forked from baluubas/voice-command
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
44 lines (34 loc) · 998 Bytes
/
index.js
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
var EventEmitter = require('events').EventEmitter,
util = require("util"),
spawn = require('child_process').spawn;
var exe = __dirname + '/.bin/voice-command';
function VoiceCommand(cmds) {
EventEmitter.call(this);
if(!cmds.length) {
throw new Error('Invalid argument. Cmds must be a string array with at least one item.');
}
this.cmds = cmds;
}
util.inherits(VoiceCommand, EventEmitter);
VoiceCommand.prototype.startListening = function() {
if(this.childProcess) {
throw new Error('Already listening');
}
var that = this;
var lingeringLine = "";
var process = spawn(exe, this.cmds);
process.stdout.setEncoding('UTF-8');
process.stdout.on('data', function(data) {
var cmd = data.trim();
that.emit('command', cmd);
});
this.childProcess = process;
};
VoiceCommand.prototype.stopListening = function() {
if(!this.childProcess) {
throw new Error('Not listening');
}
this.childProcess.kill();
this.childProcess = null;
};
module.exports = VoiceCommand;