mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 01:21:20 -04:00
neatofix
This commit is contained in:
@@ -2,11 +2,17 @@ const { spawn } = require('child_process');
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const SCAN_TIMEOUT_MS = 4000;
|
const SCAN_TIMEOUT_MS = 8000;
|
||||||
const RECONNECT_DELAY_MS = 5000;
|
const RECONNECT_DELAY_MS = 5000;
|
||||||
|
|
||||||
|
function sanitizeLogText(value) {
|
||||||
|
return String(value || '')
|
||||||
|
.replace(/\u0000/g, '')
|
||||||
|
.replace(/\x1B\[[0-9;]*[A-Za-z]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
function parsePayloadLine(line) {
|
function parsePayloadLine(line) {
|
||||||
const raw = String(line || '').trim();
|
const raw = sanitizeLogText(line).trim();
|
||||||
if (!raw) return null;
|
if (!raw) return null;
|
||||||
const quotedMatch = raw.match(/<<< "(.*)"$/);
|
const quotedMatch = raw.match(/<<< "(.*)"$/);
|
||||||
if (quotedMatch) return quotedMatch[1];
|
if (quotedMatch) return quotedMatch[1];
|
||||||
@@ -51,6 +57,7 @@ function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shou
|
|||||||
pollSoonTimer: null,
|
pollSoonTimer: null,
|
||||||
requestTimeoutTimer: null,
|
requestTimeoutTimer: null,
|
||||||
stdoutBuffer: '',
|
stdoutBuffer: '',
|
||||||
|
stderrBuffer: '',
|
||||||
currentScan: null,
|
currentScan: null,
|
||||||
requestInFlight: false,
|
requestInFlight: false,
|
||||||
requestStartedAt: 0,
|
requestStartedAt: 0,
|
||||||
@@ -138,13 +145,20 @@ function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shou
|
|||||||
state.currentScan.points.set(point.angleDeg, point);
|
state.currentScan.points.set(point.angleDeg, point);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleStdoutChunk(chunk) {
|
function handleTextChunk(chunk, bufferKey, sourceLabel = '') {
|
||||||
|
const text = String(chunk || '');
|
||||||
if (state.logStream) {
|
if (state.logStream) {
|
||||||
state.logStream.write(String(chunk || ''));
|
const prefix = sourceLabel ? `[${sourceLabel}] ` : '';
|
||||||
|
state.logStream.write(
|
||||||
|
text
|
||||||
|
.split(/\r?\n/)
|
||||||
|
.map((line, index, parts) => (index === parts.length - 1 && line === '' ? '' : `${prefix}${line}`))
|
||||||
|
.join('\n'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
state.stdoutBuffer += String(chunk || '');
|
state[bufferKey] += sanitizeLogText(text);
|
||||||
const lines = state.stdoutBuffer.split(/\r?\n/);
|
const lines = state[bufferKey].split(/\r?\n/);
|
||||||
state.stdoutBuffer = lines.pop() || '';
|
state[bufferKey] = lines.pop() || '';
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const payload = parsePayloadLine(line);
|
const payload = parsePayloadLine(line);
|
||||||
handlePayload(payload);
|
handlePayload(payload);
|
||||||
@@ -219,10 +233,11 @@ function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shou
|
|||||||
emitStatus();
|
emitStatus();
|
||||||
triggerPollSoon();
|
triggerPollSoon();
|
||||||
}
|
}
|
||||||
handleStdoutChunk(chunk);
|
handleTextChunk(chunk, 'stdoutBuffer', 'stdout');
|
||||||
});
|
});
|
||||||
|
|
||||||
proc.stderr.on('data', (chunk) => {
|
proc.stderr.on('data', (chunk) => {
|
||||||
|
handleTextChunk(chunk, 'stderrBuffer', 'stderr');
|
||||||
const message = String(chunk || '').trim();
|
const message = String(chunk || '').trim();
|
||||||
if (message) logger.warn('Neato lidar log stream stderr', message);
|
if (message) logger.warn('Neato lidar log stream stderr', message);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user