forked from andrenatal/voice-addon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice-adapter.js
474 lines (436 loc) · 13.4 KB
/
voice-adapter.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
/**
* voice-adapter.js - Voice adapter.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
'use strict';
const mqtt = require('mqtt');
const https = require('https');
const spawn = require('child_process').spawn;
const fs = require('fs');
const {Adapter, Device, Property, Event} = require('gateway-addon');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
let token, keyword, speaker, microphone;
let pixel_ring_service;
class ActiveProperty extends Property {
constructor(device, name, propertyDescription) {
super(device, name, propertyDescription);
this.setCachedValue(propertyDescription.value);
this.device.notifyPropertyChanged(this);
}
/**
* Set the value of the property.
*
* @param {*} value The new value to set
* @returns a promise which resolves to the updated value.
*
* @note it is possible that the updated value doesn't match
* the value passed in.
*/
setValue(value) {
if (value) {
// spawn training
console.log('spawn training');
this.training_process = spawn(
'python2',
['script_recording.py', keyword],
{cwd: __dirname}
);
this.training_process.stdout.setEncoding('utf8');
this.training_process.stdout.on('data', (data) => {
console.log(`DATA: ${data.toString()}`);
});
this.training_process.stderr.on('data', (data) => {
console.log(`ERROR: ${data.toString()}`);
});
this.training_process.on('close', (code) => {
console.log(`process exit code ${code}`);
this.setCachedValue(false);
this.device.notifyPropertyChanged(this);
});
this.device.eventNotify(new Event(this.device,
'training',
'started'));
} else {
console.log('shutdown training');
// shutdown training
if (this.training_process) {
this.training_process.stderr.pause();
this.training_process.stdout.pause();
this.training_process.stdin.pause();
this.training_process.kill('SIGTERM');
this.device.eventNotify(new Event(this.device,
'training',
'ended'));
}
}
return new Promise((resolve, reject) => {
super.setValue(value).then((updatedValue) => {
resolve(updatedValue);
this.device.notifyPropertyChanged(this);
}).catch((err) => {
reject(err);
});
});
}
}
class VoiceDevice extends Device {
constructor(adapter, id, deviceDescription) {
super(adapter, id);
this.name = deviceDescription.name;
this.type = deviceDescription.type;
this['@type'] = deviceDescription['@type'];
this.description = deviceDescription.description;
for (const propertyName in deviceDescription.properties) {
const propertyDescription = deviceDescription.properties[propertyName];
const property = new ActiveProperty(this, propertyName,
propertyDescription);
this.properties.set(propertyName, property);
}
for (const event in deviceDescription.events) {
console.log('addedEvent', deviceDescription.events[event].name,deviceDescription.events[event].metadata);
this.addEvent(deviceDescription.events[event].name,
deviceDescription.events[event].metadata);
}
this.mqttListener = new MqttListener(this);
this.mqttListener.connect();
}
}
class MqttListener {
constructor(device) {
// connect to snips mqtt
this.client = mqtt.connect('mqtt://127.0.0.1');
this.HERMES_KWS = 'hermes/hotword/default/detected';
this.HERMES_ASR = 'hermes/asr/textCaptured';
setInterval(this.call_things_api.bind(this), 10000);
this.things = [];
this.device = device;
}
connect() {
this.client.on('connect', function() {
console.log('conectado');
this.call_things_api();
this.client.subscribe(this.HERMES_KWS, function(err) {
if (err) {
console.log('mqtt error hermes/hotword/default/detected');
}
});
this.client.subscribe(this.HERMES_ASR, function(err) {
if (err) {
console.log('mqtt error hermes/asr/textCaptured');
}
});
}.bind(this));
this.client.on('message', function(topic, message) {
if (topic === this.HERMES_ASR) {
console.log(`mensagem no mqtt no addon ${message}`);
this.call_commands_api(JSON.parse(message));
this.device.eventNotify(new Event(this.device,
'speechinput',
'detected'));
} else if (topic === this.HERMES_KWS) {
this.device.eventNotify(new Event(this.device,
'wakeword',
'detected'));
spawn(
'aplay',
['end_spot.wav'],
{cwd: __dirname + "/assets"}
);
}
}.bind(this));
}
call_commands_api(command) {
try {
const postData = JSON.stringify({
text: command.text,
});
this.doHTTPRequest('/commands', postData);
this.device.eventNotify(new Event(this.device,
'command',
command.text));
} catch (err) {
console.log(`Error calling commands api: ${err}`)
this.device.eventNotify(new Event(this.device,
'command',
`Error calling commands api: ${err}`));
}
}
call_things_api() {
this.doHTTPRequest('/things', null, (response) => {
try {
const json_things = JSON.parse(response);
const temp_things = [];
for (const i in json_things) {
for (const key in json_things[i]) {
if (key === 'title') {
temp_things.push(json_things[i][key]);
}
}
}
if (JSON.stringify(temp_things.sort()) !==
JSON.stringify(this.things.sort())) {
console.log('different set of things. retrain: ');
const train_json = {
operations: [['addFromVanilla', {thing: []}]],
};
train_json.operations[0][1].thing = temp_things;
this.things = temp_things;
this.client.publish('hermes/injection/perform',
JSON.stringify(train_json));
}
} catch (err) {
console.log(`Error calling things api: ${err}`)
}
});
}
doHTTPRequest(command, postData, callback) {
if (token === '') {
console.log(`Token not set. Aborting call`);
return;
}
let method = 'POST';
if (postData === null) {
postData = '';
method = 'GET';
}
const options = {
hostname: '127.0.0.1',
port: 4443,
path: `${command}`,
method: `${method}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'Content-Length': Buffer.byteLength(postData),
'Content-Type': 'application/json',
},
};
const req = https.request(options, (res) => {
let chunks = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
chunks += chunk;
});
res.on('end', () => {
if (callback) {
callback(chunks);
}
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
}
}
class VoiceAdapter extends Adapter {
constructor(addonManager, packageName) {
super(addonManager, 'VoiceAdapter', packageName);
addonManager.addAdapter(this);
}
/**
* Example process to add a new device to the adapter.
*
* The important part is to call: `this.handleDeviceAdded(device)`
*
* @param {String} deviceId ID of the device to add.
* @param {String} deviceDescription Description of the device to add.
* @return {Promise} which resolves to the device added.
*/
addDevice(deviceId, deviceDescription) {
return new Promise((resolve, reject) => {
if (deviceId in this.devices) {
reject(`Device: ${deviceId} already exists.`);
} else {
const device = new VoiceDevice(this, deviceId, deviceDescription);
this.handleDeviceAdded(device);
resolve(device);
}
});
}
/**
* Example process ro remove a device from the adapter.
*
* The important part is to call: `this.handleDeviceRemoved(device)`
*
* @param {String} deviceId ID of the device to remove.
* @return {Promise} which resolves to the device removed.
*/
removeDevice(deviceId) {
return new Promise((resolve, reject) => {
const device = this.devices[deviceId];
if (device) {
this.handleDeviceRemoved(device);
resolve(device);
} else {
reject(`Device: ${deviceId} not found.`);
}
});
}
/**
* Start the pairing/discovery process.
*
* @param {Number} timeoutSeconds Number of seconds to run before timeout
*/
startPairing(_timeoutSeconds) {
console.log('VoiceAdapter:', this.name,
'id', this.id, 'pairing started');
}
/**
* Cancel the pairing/discovery process.
*/
cancelPairing() {
console.log('VoiceAdapter:', this.name, 'id', this.id,
'pairing cancelled');
}
/**
* Unpair the provided the device from the adapter.
*
* @param {Object} device Device to unpair with
*/
removeThing(device) {
console.log('VoiceAdapter:', this.name, 'id', this.id,
'removeThing(', device.id, ') started');
this.removeDevice(device.id).then(() => {
console.log('VoiceAdapter: device:', device.id, 'was unpaired.');
}).catch((err) => {
console.error('VoiceAdapter: unpairing', device.id, 'failed');
console.error(err);
});
}
/**
* Cancel unpairing process.
*
* @param {Object} device Device that is currently being paired
*/
cancelRemoveThing(device) {
console.log('VoiceAdapter:', this.name, 'id', this.id,
'cancelRemoveThing(', device.id, ')');
}
// cleanup
unload() {
return new Promise((resolve, reject) => {
if (pixel_ring_service) {
pixel_ring_service.stderr.pause();
pixel_ring_service.stdout.pause();
pixel_ring_service.stdin.pause();
pixel_ring_service.kill('SIGTERM');
}
console.log(`unloaded addon ${pixel_ring_service}`);
const snips_uninstall = spawn(
'bash',
['install_deps.sh', 'uninstall'],
{cwd: __dirname + '/deps'}
);
snips_uninstall.stdout.on('data', (data) => {
console.log(`DATA snips_uninstall: ${data.toString()}`);
});
snips_uninstall.stderr.on('data', (data) => {
console.log(`Error executing install_script.sh ${data}`);
});
snips_uninstall.on('close', (code) => {
console.log(`End of snips_uninstall ${code}`);
resolve();
});
})
}
}
function loadVoiceAdapter(addonManager, manifest, _errorCallback) {
checkInstallation();
token = manifest.moziot.config.token;
keyword = manifest.moziot.config.keyword;
speaker = manifest.moziot.config.speaker;
microphone = manifest.moziot.config.microphone;
console.log(`microphone ${microphone}`);
console.log(`speaker ${speaker}`);
let capture_pcm = "";
let playback_pcm = "";
if (microphone === 'USB') {
capture_pcm = "capture.pcm { \n type plug \n slave.pcm 'hw:1,0' \n }"
}
if (speaker === 'USB') {
playback_pcm = "playback.pcm { \n type plug \n slave.pcm 'hw:1,0' \n }"
} else {
playback_pcm = "playback.pcm { \n type plug \n slave.pcm 'hw:0,0' \n }"
}
console.log('writing asound.conf');
let asound_tpl = `pcm.!default { \n type asym \n ${playback_pcm} \n ${capture_pcm} \n } \n`
fs.writeFileSync(`${__dirname}/asound.conf`, asound_tpl)
const snips_installation = spawn(
'bash',
['install_asound.sh'],
{cwd: __dirname}
);
console.log('asound.conf written');
const restart_audio_server = spawn(
'sudo',
['systemctl', 'restart', 'snips-audio-server'],
{cwd: __dirname}
);
const adapter = new VoiceAdapter(addonManager, manifest.name);
const device = new VoiceDevice(adapter, 'voice-controller', {
name: 'voice-controller',
'@type': ['OnOffSwitch'],
type: 'onOffSwitch',
description: 'Voice Controller',
properties: {
on: {
'@type': 'OnOffProperty',
label: 'On/Off',
name: 'on',
type: 'boolean',
value: false,
},
},
events: [
{
name: 'wakeword',
metadata: {
description: 'A wakeword was deteced',
type: 'string',
},
},
{
name: 'speechinput',
metadata: {
description: 'A voice command was detected',
type: 'string',
},
},
{
name: 'command',
metadata: {
description: 'A web thing command was executed',
type: 'string',
},
},
{
name: 'training',
metadata: {
description: 'Wakeword training started',
type: 'string',
},
},
],
});
adapter.handleDeviceAdded(device);
}
function checkInstallation() {
const snips_installation = spawn(
'bash',
['install_deps.sh', 'install'],
{cwd: __dirname + '/deps'}
);
snips_installation.stdout.on('data', (data) => {
console.log(`DATA snips_installation: ${data.toString()}`);
});
snips_installation.stderr.on('data', (data) => {
console.log(`Error executing install_script.sh ${data}`);
});
}
module.exports = loadVoiceAdapter;