feat: enhance serial communication handling and logging in backend services

This commit is contained in:
2025-09-02 23:25:25 +02:00
parent 423eaefe85
commit db44b15717
6 changed files with 179 additions and 121 deletions

View File

@@ -74,13 +74,20 @@
// data events contain the actual lines emitted by the Pico; parse JSON payloads but fall back to raw
es.addEventListener('data', (ev: any) => {
console.log('[serial][SSE] data event', ev.data);
const raw = ev.data as string;
let line: string | undefined;
try {
const d = JSON.parse(ev.data);
// always show the raw line emitted by the firmware
addLog('RX: ' + d.line);
} catch (e) {
addLog('RX (raw): ' + ev.data);
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === 'object' && 'line' in parsed) {
line = (parsed as any).line;
} else if (typeof parsed === 'string') {
line = parsed; // backend sent a plain JSON string
}
} catch {
// not JSON -> treat as plain text
}
if (!line) line = raw;
addLog('RX: ' + line);
});
}