57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
const { spawn } = require('child_process');
|
|
const path = require('path');
|
|
|
|
let wakewordProcess = null;
|
|
let running = false;
|
|
|
|
function startWakeword({ onWake }, retries = 0) {
|
|
if (running) return;
|
|
if (retries >= 10) {
|
|
console.error('[Wakeword] Maximale Neustartversuche erreicht. Wakeword deaktiviert.');
|
|
return;
|
|
}
|
|
running = true;
|
|
|
|
const scriptPath = path.join(__dirname, '../python/wakeword.py');
|
|
|
|
const proc = spawn('python3', [scriptPath], {
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
});
|
|
wakewordProcess = proc;
|
|
|
|
let lineBuffer = '';
|
|
proc.stdout.on('data', (data) => {
|
|
lineBuffer += data.toString();
|
|
const lines = lineBuffer.split('\n');
|
|
lineBuffer = lines.pop();
|
|
for (const line of lines) {
|
|
if (line.trim() === 'WAKE') onWake();
|
|
}
|
|
});
|
|
|
|
proc.stderr.on('data', (data) => {
|
|
console.error('[Wakeword]', data.toString().trim());
|
|
});
|
|
|
|
proc.on('close', (code, signal) => {
|
|
if (proc !== wakewordProcess) return;
|
|
running = false;
|
|
wakewordProcess = null;
|
|
if (code !== 0 && signal === null) {
|
|
const delay = Math.min(2000 * Math.pow(2, retries), 30000);
|
|
console.error(`[Wakeword] Neustart in ${delay / 1000}s (Versuch ${retries + 1}/10)`);
|
|
setTimeout(() => startWakeword({ onWake }, retries + 1), delay);
|
|
}
|
|
});
|
|
}
|
|
|
|
function stopWakeword() {
|
|
if (wakewordProcess) {
|
|
wakewordProcess.kill();
|
|
wakewordProcess = null;
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
module.exports = { startWakeword, stopWakeword };
|