45 lines
974 B
JavaScript
45 lines
974 B
JavaScript
const { spawn } = require('child_process');
|
|
const path = require('path');
|
|
|
|
let wakewordProcess = null;
|
|
let running = false;
|
|
|
|
function startWakeword({ onWake }) {
|
|
if (running) return;
|
|
running = true;
|
|
|
|
const scriptPath = path.join(__dirname, '../python/wakeword.py');
|
|
|
|
wakewordProcess = spawn('python3', [scriptPath], {
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
});
|
|
|
|
wakewordProcess.stdout.on('data', (data) => {
|
|
if (data.toString().trim() === 'WAKE') {
|
|
onWake();
|
|
}
|
|
});
|
|
|
|
wakewordProcess.stderr.on('data', (data) => {
|
|
console.error('[Wakeword]', data.toString().trim());
|
|
});
|
|
|
|
wakewordProcess.on('close', (code) => {
|
|
running = false;
|
|
wakewordProcess = null;
|
|
if (code !== 0) {
|
|
setTimeout(() => startWakeword({ onWake }), 2000);
|
|
}
|
|
});
|
|
}
|
|
|
|
function stopWakeword() {
|
|
if (wakewordProcess) {
|
|
wakewordProcess.kill();
|
|
wakewordProcess = null;
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
module.exports = { startWakeword, stopWakeword };
|