-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
565 lines (476 loc) · 22.7 KB
/
node_helper.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**
* @file node_helper.js
*
* @license MIT
*
* @see https://github.com/TheStigh/MMM-VoiceCommander
/**
* @external pocketsphinx-continuous @see https://github.com/fewieden/pocketsphinx-continuous-node
* @external fs @see https://nodejs.org/api/fs.html
* @external child_process @see https://nodejs.org/api/child_process.html
* @external lmtool @see https://www.npmjs.com/package/lmtool
* @module Bytes @description Pure Magic
* @external node_helper @see https://github.com/MichMich/MagicMirror/blob/master/modules/node_modules/node_helper/index.js */
const Psc = require('pocketsphinx-continuous');
const fs = require('fs');
const exec = require('child_process').exec;
const lmtool = require('lmtool');
const bytes = require('./Bytes.js');
const NodeHelper = require('node_helper');
/** @preparing for import for new function of checkCommands() */
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
var loadingRules=fs.readFileSync('modules/MMM-VoiceCommander/checkCommands.json', 'utf8');
var importedWords = JSON.parse(loadingRules);
var firstWord, secWord, thirdWord, fourthWord, modName, trueFalse, sendNoti
//console.log(importedWords);
module.exports = NodeHelper.create({
/** @member {boolean} listening - Flag to indicate listen state. */
/** @member {(boolean|string)} mode - Contains active module mode. */
/** @member {string[]} words - List of all words that are registered by the modules. */
listening: false,
mode: false, // was false,
words: [],
start() {
console.log(`Starting module helper: ${this.name}`);
},
socketNotificationReceived(notification, payload) {
if (notification === 'START') {
/** @member {Object} config - Module config. */
this.config = payload.config;
/** @member {number} time - Time to listen after keyword. */
this.time = this.config.timeout * 1000;
/** @member {Object} modules - List of modules with their modes and commands. */
this.modules = payload.modules;
this.fillWords();
this.checkFiles();
////////////////////////////////////////////////////////////////////////
//////////////// Done by @sdetweil to ////////////////
//////////////// release mic from PocketSphinx ////////////////
//////////////// and create timer and checking ////////////////
////////////////////////////////////////////////////////////////////////
} else if(notification === 'SUSPEND_LISTENING'){
if(this.ps.isListening())
this.ps.stop()
} else if(notification === 'RESUME_LISTENING'){
if(!this.ps.isListening())
this.ps.start()
////////////////////////////////// EOC /////////////////////////////////
// notification relayed from another module (alarm clock)
} else if(notification === 'ACTIVATE_MONITOR') {
if(this.config.standByMethod === 'DPMS') /////////// Turns on laptop display and desktop PC with DVI @ Mykle1
exec('xset dpms force on', null);
if(this.config.standByMethod === 'PI') /////////// Turns off HDMI on Pi
exec('/opt/vc/bin/tvservice -p && sudo chvt 6 && sudo chvt 7', null);
this.hdmi = true;
} else if(notification === 'DEACTIVATE_MONITOR') {
if(this.config.standByMethod === 'DPMS') /////////// Turns on laptop display and desktop PC with DVI @ Mykle1
exec('xset dpms force off', null);
if(this.config.standByMethod === 'PI') /////////// Turns off HDMI on Pi
exec('/opt/vc/bin/tvservice -o', null);
this.hdmi = false;
} else if(notification ==='MUTE_MIC') {
this.config = payload;
exec('amixer sset -M Master '+this.config, null);
} else if(notification ==='RESTORE_MIC') {
this.config = payload;
exec('amixer sset -M Master '+this.config, null);
}
},
/**
* @function fillwords
* @description Sets {@link node_helper.words} with all needed words for the registered
* commands by the modules. This list has unique items and is sorted by alphabet.
*/
fillWords() {
// create array
let words = this.config.keyword.split(' ');
const temp = bytes.q.split(' ');
words = words.concat(temp);
for (let i = 0; i < this.modules.length; i += 1) {
const mode = this.modules[i].mode.split(' ');
//console.log('<<<>>> NODE HELPER modes: '+mode);
words = words.concat(mode);
for (let n = 0; n < this.modules[i].sentences.length; n += 1) {
const sentences = this.modules[i].sentences[n].split(' ');
//console.log('<<<>>> NODE HELPER SENTENCES: '+sentences);
words = words.concat(sentences);
}
}
// filter duplicates
words = words.filter((item, index, data) => data.indexOf(item) === index);
// sort array
words.sort();
this.words = words;
},
/**
* @function checkFiles
* @description Checks if words.json exists or has different entries as this.word.
*/
checkFiles() {
console.log(`${this.name}: Checking files.`);
fs.stat('modules/MMM-VoiceCommander/words.json', (error, stats) => {
if (!error && stats.isFile()) {
fs.readFile('modules/MMM-VoiceCommander/words.json', 'utf8', (err, data) => {
if (!err) {
const words = JSON.parse(data).words;
if (this.arraysEqual(this.words, words)) {
this.startPocketsphinx();
return;
}
}
this.generateDicLM();
});
} else {
this.generateDicLM();
}
});
},
/**
* @function arraysEqual
* @description Compares two arrays.
*
* @param {string[]} a - First array
* @param {string[]} b - Second array
* @returns {boolean} Are the arrays equal or not.
*/
arraysEqual(a, b) {
if (!(a instanceof Array) || !(b instanceof Array)) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
},
/**
* @function generateDicLM
* @description Generates new Dictionairy and Language Model.
*/
generateDicLM() {
console.log(`${this.name}: Generating dictionairy and language model.`);
fs.writeFile('modules/MMM-VoiceCommander/words.json', JSON.stringify({ words: this.words }), (err) => {
if (err) {
console.log(`${this.name}: Couldn't save words.json!`);
} else {
console.log(`${this.name}: Saved words.json successfully.`);
}
});
lmtool(this.words, (err, filename) => {
if (err) {
this.sendSocketNotification('ERROR', 'Couldn\'t create necessary files!');
} else {
fs.renameSync(`${filename}.dic`, 'modules/MMM-VoiceCommander/MMM-VoiceCommander.dic');
fs.renameSync(`${filename}.lm`, 'modules/MMM-VoiceCommander/MMM-VoiceCommander.lm');
this.startPocketsphinx();
fs.unlink(`${filename}.log_pronounce`, this.noOp);
fs.unlink(`${filename}.sent`, this.noOp);
fs.unlink(`${filename}.vocab`, this.noOp);
fs.unlink(`TAR${filename}.tgz`, this.noOp);
}
});
},
/**
* @function noOp
* @description Performs no operation.
*/
noOp() {},
/**
* @function startPocketsphinx
* @description Starts Pocketsphinx binary.
*/
startPocketsphinx() {
console.log(`${this.name}: Starting pocketsphinx.`);
this.ps = new Psc({
setId: this.name,
verbose: true,
microphone: this.config.microphone
});
this.ps.on('data', this.handleData.bind(this));
if (this.config.debug) {
this.ps.on('debug', this.logDebug.bind(this));
}
this.ps.on('error', this.logError.bind(this));
////////////////////////////////////////////////////////////////////////
//////////////// Done by @sdetweil to ////////////////
//////////////// release mic from PocketSphinx ////////////////
//////////////// and create timer and checking ////////////////
////////////////////////////////////////////////////////////////////////
if(typeof this.ps.start != 'function')
console.log("downlevel pocketsphinx-continuous node module... error<===============================");
////////////////////////////////// EOC /////////////////////////////////
this.sendSocketNotification('READY');
},
/**
* @function handleData
* @description Helper method to handle recognized data.
*
* @param {string} data - Recognized data
*/
handleData(data) {
if (typeof data === 'string') {
if (this.config.debug) {
console.log(`${this.name} has recognized: ${data}`);
this.sendSocketNotification('DEBUG', data);
}
if (data.includes(this.config.keyword) || this.listening) {
// if hotword only, go directly online
if (this.config.onlyHotword) {
if(this.ps.isListening())
this.ps.stop();
console.log("sending socket notification, have released mic");
this.sendSocketNotification('SUSPENDED', {ASSISTANT:this.config.onOnlyHotword});
}
this.listening = true;
this.sendSocketNotification('LISTENING');
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.listening = false;
this.sendSocketNotification('SLEEPING');
}, this.time);
} else {
return;
}
let cleanData = this.cleanData(data);
for (let i = 0; i < this.modules.length; i += 1) {
const n = cleanData.indexOf(this.modules[i].mode);
////////////////////////////////////////////////////////////////////////
//////////////// Done by @sdetweil to ////////////////
//////////////// release mic from PocketSphinx ////////////////
//////////////// and create timer and checking ////////////////
////////////////////////////////////////////////////////////////////////
if (n === 0) {
this.mode= this.modules[i].mode;
////////////////////////////////// EOC /////////////////////////////////
cleanData = cleanData.substr(n + this.modules[i].mode.length).trim();
break;
}
}
////////////////////////////////////////////////////////////////////////
//////////////// Done by @sdetweil to ////////////////
//////////////// release mic from PocketSphinx ////////////////
//////////////// and create timer and checking ////////////////
////////////////////////////////////////////////////////////////////////
this.mode='VOICE';
////////////////////////////////// EOC /////////////////////////////////
if (this.mode) {
this.sendSocketNotification('VOICE', { mode: this.mode, sentence: cleanData });
if (this.mode === 'VOICE') {
this.checkCommands(cleanData);
}
}
}
},
/**
* @function logDebug
* @description Logs debug information into debug log file.
*
* @param {string} data - Debug information
*/
logDebug(data) {
fs.appendFile('modules/MMM-VoiceCommander/debug.log', data, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
});
},
/**
* @function logError
* @description Logs error information into error log file.
*
* @param {string} data - Error information
*/
logError(error) {
if (error) {
fs.appendFile('modules/MMM-VoiceCommander/error.log', `${error}\n`, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
this.sendSocketNotification('ERROR', error);
});
}
},
/**
* @function cleanData
* @description Removes prefix/keyword and multiple spaces.
*
* @param {string} data - Recognized data to clean.
* @returns {string} Cleaned data
*/
cleanData(data) {
let temp = data;
const i = temp.indexOf(this.config.keyword);
if (i !== -1) {
temp = temp.substr(i + this.config.keyword.length);
}
temp = temp.replace(/ {2,}/g, ' ').trim();
return temp;
},
/**
* @function checkCommands
* @description Checks for commands of voice module
* @param {string} data - Recognized data
*/
checkCommands(data) {
////////////////////////////////////////////////////////////////////////
//////////////// Done by @sdetweil to ////////////////
//////////////// release mic from PocketSphinx ////////////////
//////////////// and create timer and checking ////////////////
////////////////////////////////////////////////////////////////////////
if (/(GO)/g.test(data) && /(ASSISTANT)/g.test(data)) {
if(this.ps.isListening())
this.ps.stop();
console.log("sending ASSISTANT socket notification, have released mic");
this.sendSocketNotification('SUSPENDED', {ASSISTANT:'GOOGLE'});
} else if (/(GO)/g.test(data) && /(ALEXA)/g.test(data)) {
if(this.ps.isListening())
this.ps.stop();
console.log("sending ALEXA socket notification, have released mic");
this.sendSocketNotification('SUSPENDED',{ASSISTANT:'AMAZON'});
////////////////////////////////// EOC /////////////////////////////////
} else if (/(PLEASE)/g.test(data) && /(WAKE)/g.test(data) && /(UP)/g.test(data)) {
if(this.config.standByMethod === 'DPMS') /////////// Turns on laptop display and desktop PC with DVI @ Mykle1
exec('xset dpms force on', null);
if(this.config.standByMethod === 'PI') /////////// Turns on HDMI on Pi
exec('/opt/vc/bin/tvservice -p && sudo chvt 6 && sudo chvt 7', null);
this.hdmi = true;
} else if (/(GO)/g.test(data) && /(TO)/g.test(data) && /(SLEEP)/g.test(data)) {
if(this.config.standByMethod === 'DPMS') /////////// Turns off laptop display and desktop PC with DVI @ Mykle1
exec('xset dpms force off', null);
if(this.config.standByMethod === 'PI') /////////// Turns off HDMI on Pi
exec('/opt/vc/bin/tvservice -o', null);
this.hdmi = false;
} else if (/(HIDE)/g.test(data) && /(MODULES)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
} else if (/(SHOW)/g.test(data) && /(MODULES)/g.test(data)) {
this.sendSocketNotification('SHOW_MODULES');
} else if (/(HELP)/g.test(data)) {
if (/(CLOSE)/g.test(data) || (this.help && !/(OPEN)/g.test(data))) {
this.sendSocketNotification('CLOSE_HELP');
this.help = false;
} else if (/(OPEN)/g.test(data) || (!this.help && !/(CLOSE)/g.test(data))) {
this.sendSocketNotification('OPEN_HELP');
this.help = true;
}
////////////////////////////////////////////////////////////////////////
//////////////// to toggle show/hide pages ////////////////
////////////////////////////////////////////////////////////////////////
//////////////// Page 1 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(MAIN)/g.test(data) && /(PAGE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.mainPageModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(MAIN)/g.test(data) && /(PAGE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 2 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(TWO)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageTwoModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(TWO)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 3 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(THREE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageThreeModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(THREE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 4 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(FOUR)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageFourModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(FOUR)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 5 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(FIVE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageFiveModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(FIVE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 6 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(SIX)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageSixModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(SIX)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 7 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(SEVEN)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageSevenModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(SEVEN)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 8 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(EIGHT)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageEightModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(EIGHT)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 9 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(NINE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageNineModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(NINE)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
//////////////// Page 10 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(TEN)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: this.config.pageTenModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(TEN)/g.test(data)) {
this.sendSocketNotification('HIDE_MODULES');
////////////////////////////////////////////////////////////////////////
//////////////// Enhanced by @TheStigh ////////////////
//////////////// to toggle show/hide modules ////////////////
////////////////////////////////////////////////////////////////////////
} else {
for (var xx = 0, l = importedWords.modOp.length; xx < l;) {
var firstW = JSON.stringify(importedWords.modOp[xx].wordOne);
var secW = JSON.stringify(importedWords.modOp[xx].wordTwo);
var thirdW = JSON.stringify(importedWords.modOp[xx].wordThree);
var fourthW = JSON.stringify(importedWords.modOp[xx].wordFour);
var modN = JSON.stringify(importedWords.modOp[xx].moduleName);
var Tf = JSON.stringify(importedWords.modOp[xx].toShow);
var sN = JSON.stringify(importedWords.modOp[xx].sendNoti);
firstWord = firstW.replace(/"/g, "");
secWord = secW.replace(/"/g, "");
thirdWord = thirdW.replace(/"/g, "");
fourthWord = fourthW.replace(/"/g, "");
modName = modN.replace(/"/g, "");
trueFalse = Tf.replace(/"/g, "");
sendNoti = sN.replace(/"/g, "");
this.firstWord = RegExp(firstWord);
this.secWord = RegExp(secWord);
this.thirdWord = RegExp(thirdWord);
this.fourthWord = RegExp(fourthWord);
this.modName = modName;
this.trueFalse = trueFalse;
this.sendNoti = sendNoti;
if (this.firstWord.test(data) && this.secWord.test(data) && this.thirdWord.test(data) && this.fourthWord.test(data)) {
console.log('>>> modName & sendNoti : '+this.modName+' '+this.sendNoti);
if (this.modName != "") {
if (this.trueFalse === "true") {
this.sendSocketNotification('MODULE_STATUS',{hide: [], show: [this.modName], toggle:[]});
} else {
this.sendSocketNotification('MODULE_STATUS',{hide: [this.modName], show: [], toggle:[]});
}
} else {
this.sendSocketNotification('MODULE_UPDATE',this.sendNoti);
}
};xx++;
};
}
}
});