fix: wakeword restart-after-kill, stale close event, stdout line buffer

main
boksbc 2026-05-11 22:08:37 +02:00
parent 0a2154ddbd
commit 6f3f770783
1 changed files with 13 additions and 7 deletions

View File

@ -10,24 +10,30 @@ function startWakeword({ onWake }) {
const scriptPath = path.join(__dirname, '../python/wakeword.py'); const scriptPath = path.join(__dirname, '../python/wakeword.py');
wakewordProcess = spawn('python3', [scriptPath], { const proc = spawn('python3', [scriptPath], {
stdio: ['pipe', 'pipe', 'pipe'], stdio: ['pipe', 'pipe', 'pipe'],
}); });
wakewordProcess = proc;
wakewordProcess.stdout.on('data', (data) => { let lineBuffer = '';
if (data.toString().trim() === 'WAKE') { proc.stdout.on('data', (data) => {
onWake(); lineBuffer += data.toString();
const lines = lineBuffer.split('\n');
lineBuffer = lines.pop();
for (const line of lines) {
if (line.trim() === 'WAKE') onWake();
} }
}); });
wakewordProcess.stderr.on('data', (data) => { proc.stderr.on('data', (data) => {
console.error('[Wakeword]', data.toString().trim()); console.error('[Wakeword]', data.toString().trim());
}); });
wakewordProcess.on('close', (code) => { proc.on('close', (code, signal) => {
if (proc !== wakewordProcess) return;
running = false; running = false;
wakewordProcess = null; wakewordProcess = null;
if (code !== 0) { if (code !== 0 && signal === null) {
setTimeout(() => startWakeword({ onWake }), 2000); setTimeout(() => startWakeword({ onWake }), 2000);
} }
}); });