-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_helper.js
408 lines (336 loc) · 13.4 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
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");
// 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);
}
const localPath= __dirname.substring(__dirname.indexOf('modules'))
//console.log("localPath="+localPath);
var loadingRules=fs.readFileSync(localPath+"/checkCommands.json", "utf8");
var importedWords = JSON.parse(loadingRules);
var firstWord, secWord, thirdWord, fourthWord, modName, trueFalse, sendNoti;
//console.log(importedWords);
module.exports = NodeHelper.create({
listening: false,
mode: false,
words: [],
start() {
console.log(`Starting module helper: ${this.name}`);
},
socketNotificationReceived(notification, payload) {
//console.log("Lucy received notification="+notification);
if (notification === "START") {
this.config = payload.config;
this.time = this.config.timeout * 1000;
this.modules = payload.modules;
this.fillWords();
this.checkFiles();
} else if(notification === "ACTIVATE_MONITOR") {
if(this.config.standByMethod === "DPMS")
{exec("xset dpms force on", null);}
if(this.config.standByMethod === "PI") // Turns off HDMI on Pi by @sdetweil
{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")
{exec("xset dpms force off", null);}
if(this.config.standByMethod === "PI") // Turns off HDMI on Pi by @sdetweil
{exec("/opt/vc/bin/tvservice -o", null);}
this.hdmi = false;
}
},
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(" ");
words = words.concat(mode);
for (let n = 0; n < this.modules[i].sentences.length; n += 1) {
const sentences = this.modules[i].sentences[n].split(" ");
words = words.concat(sentences);
}
}
// filter duplicates
words = words.filter((item, index, data) => data.indexOf(item) === index);
// sort array
words.sort();
this.words = words;
},
checkFiles() {
//console.log(`${this.name}: Checking files.`);
fs.stat(localPath+"/words.json", (error, stats) => {
if (!error && stats.isFile()) {
fs.readFile(localPath+"/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();
}
});
},
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;
},
// Generates new Dictionary and Language Model.
generateDicLM() {
console.log(`${this.name}: Generating dictionairy and language model.`);
fs.writeFile(localPath+"/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`, localPath+"/Hello-Lucy.dic");
fs.renameSync(`${filename}.lm`, localPath+"/Hello-Lucy.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);
}
});
},
// Performs no operation.
noOp() {},
// 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));
if(typeof this.ps.start != "function")
{console.log("downlevel pocketsphinx-continuous node module... error<===============================");}
this.sendSocketNotification("READY");
},
// Helper method to handle 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) {
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);
if (n === 0) {
this.mode= this.modules[i].mode;
cleanData = cleanData.substr(n + this.modules[i].mode.length).trim();
break;
}
}
this.mode="LUCY";
if (this.mode) {
this.sendSocketNotification("LUCY", { mode: this.mode, sentence: cleanData });
if (this.mode === "LUCY") {
this.checkCommands(cleanData);
}
}
}
},
logDebug(data) {
fs.appendFile(localPath+"/debug.log", data, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
});
},
// Logs error information into error log file.
logError(error) {
if (error) {
fs.appendFile(localPath+"/error.log", `${error}\n`, (err) => {
if (err) {
console.log(`${this.name}: Couldn't save error to log file!`);
}
this.sendSocketNotification("ERROR", error);
});
}
},
// Removes prefix/keyword and multiple spaces. Recognized data to clean. 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;
},
// Checks for commands of lucy module. Recognized data
checkCommands(data) {
if (/(PLEASE)/g.test(data) && /(WAKE)/g.test(data) && /(UP)/g.test(data)) {
if(this.config.standByMethod === "DPMS")
{exec("xset dpms force on", null);}
if(this.config.standByMethod === "PI") /////////// Turns on HDMI on Pi @sdetweil
{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")
{exec("xset dpms force off", null);}
if(this.config.standByMethod === "PI") /////////// Turns off HDMI on Pi @sdetweil
{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");
// Help screen made better by @cowboysdude
} 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;
}
////////////////// Toggle enhanced by @TheStigh /////////////
//////////////// Page 1 commands ////////////////
} else if (/(SHOW)/g.test(data) && /(PAGE)/g.test(data) && /(ONE)/g.test(data)) {
this.sendSocketNotification("HIDE_MODULES");
this.sendSocketNotification("MODULE_STATUS",{hide: [], show: this.config.pageOneModules, toggle:[]});
} else if (/(HIDE)/g.test(data) && /(PAGE)/g.test(data) && /(ONE)/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");
} else {
for (let row of importedWords.modOp){
// These vars ARE used in the .test(data) below
// refactored
firstWord = row[0];
secWord = row[1];
thirdWord = row[2];
fourthWord = row[3];
trueFalse = row[4];
modName = row[5];
sendNoti = row[6];
this.firstWord = RegExp(firstWord);
this.secWord = RegExp(secWord);
this.thirdWord = RegExp(thirdWord);
this.fourthWord = RegExp(fourthWord);
this.trueFalse = trueFalse;
this.modName = modName;
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);
}
}
}
}
}
});