neato lidar antispam

This commit is contained in:
legop3
2026-05-05 22:45:08 -04:00
parent 13885fff4c
commit 33f1e66b62
2 changed files with 26 additions and 6 deletions
+15 -1
View File
@@ -28,6 +28,7 @@ function normalizeDeviceName(value) {
const device = normalizeDeviceName(neatoConfig.device); const device = normalizeDeviceName(neatoConfig.device);
const RESUME_DELAY_MS = 3000; const RESUME_DELAY_MS = 3000;
const OFFLINE_LIDAR_RETRY_MS = 10000;
const brainslugHost = String(neatoConfig.brainslugHost || '').trim(); const brainslugHost = String(neatoConfig.brainslugHost || '').trim();
const brainslugPort = Number(neatoConfig.brainslugPort) || 6053; const brainslugPort = Number(neatoConfig.brainslugPort) || 6053;
const brainslugKey = String(neatoConfig.brainslugKey || '').trim(); const brainslugKey = String(neatoConfig.brainslugKey || '').trim();
@@ -283,7 +284,20 @@ lidarRuntime =
port: brainslugPort, port: brainslugPort,
key: brainslugKey, key: brainslugKey,
logFile: brainslugLogFile, logFile: brainslugLogFile,
shouldPoll: () => Boolean(homeAssistantEnabled && isHomeAssistantConnected() && hasVerifiedSockets()), getPollReadiness: () => {
const verifiedSockets = hasVerifiedSockets();
if (!verifiedSockets) {
return { allowed: false, delayMs: 1000 };
}
if (!homeAssistantEnabled || !isHomeAssistantConnected()) {
return { allowed: false, delayMs: OFFLINE_LIDAR_RETRY_MS };
}
const neatoOnline = buildState().connected;
if (!neatoOnline) {
return { allowed: false, delayMs: OFFLINE_LIDAR_RETRY_MS };
}
return { allowed: true, delayMs: 0 };
},
requestScan: requestLidarScan, requestScan: requestLidarScan,
}) })
: null; : null;
@@ -5,6 +5,8 @@ const fs = require('fs');
const SCAN_TIMEOUT_MS = 8000; const SCAN_TIMEOUT_MS = 8000;
const RECONNECT_DELAY_MS = 5000; const RECONNECT_DELAY_MS = 5000;
const IDLE_RETRY_MS = 1000; const IDLE_RETRY_MS = 1000;
const OFFLINE_RETRY_MS = 10000;
const REQUEST_FAILURE_RETRY_MS = 10000;
function sanitizeLogText(value) { function sanitizeLogText(value) {
return String(value || '') return String(value || '')
@@ -56,7 +58,7 @@ function parseRotationSpeed(payload) {
return Number(match[1]); return Number(match[1]);
} }
function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shouldPoll, requestScan }) { function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shouldPoll, getPollReadiness, requestScan }) {
const events = new EventEmitter(); const events = new EventEmitter();
const state = { const state = {
connected: false, connected: false,
@@ -341,11 +343,15 @@ function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shou
async function tickPoll() { async function tickPoll() {
if (!state.connected) { if (!state.connected) {
schedulePollRetry(); schedulePollRetry(OFFLINE_RETRY_MS);
return; return;
} }
if (!shouldPoll?.()) { const readiness =
schedulePollRetry(); typeof getPollReadiness === 'function'
? getPollReadiness()
: { allowed: shouldPoll?.() !== false, delayMs: IDLE_RETRY_MS };
if (!readiness?.allowed) {
schedulePollRetry(Number(readiness?.delayMs) || OFFLINE_RETRY_MS);
return; return;
} }
if (state.requestInFlight) return; if (state.requestInFlight) return;
@@ -364,7 +370,7 @@ function createLidarRuntime({ logger, host, port = 6053, key, logFile = '', shou
} catch (err) { } catch (err) {
logger.warn('Failed to request Neato lidar scan', err.message); logger.warn('Failed to request Neato lidar scan', err.message);
resetScanState(); resetScanState();
triggerPollSoon(); schedulePollRetry(REQUEST_FAILURE_RETRY_MS);
} }
} }