From 23a6f900ec565b8780e10a81a7b11ec8e62ca937 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Wed, 24 Dec 2025 22:32:08 +0100 Subject: [PATCH] u --- server/src/websocket/server.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/src/websocket/server.js b/server/src/websocket/server.js index fcbbc14..5c230d5 100644 --- a/server/src/websocket/server.js +++ b/server/src/websocket/server.js @@ -148,15 +148,36 @@ function handleData(ws, message, clientState, db) { return sendError(ws, 'Invalid readings: expected non-empty array'); } + const validReadings = []; + let skippedCount = 0; + // Validate readings format for (const reading of readings) { + // We strictly require device, channel, and value. + // If value is missing (undefined), we skip it, even if 'data' is present. if (!reading.device || !reading.channel || reading.value === undefined) { - return sendError(ws, 'Invalid reading format: each reading must have device, channel, and value'); + console.warn(`[WS] Skipped invalid reading from ${clientState.name || 'unknown'}:`, JSON.stringify(reading)); + skippedCount++; + continue; } + validReadings.push(reading); + } + + if (validReadings.length === 0) { + if (skippedCount > 0) { + // Acknowledge receipt even if all were skipped, so the agent doesn't retry endlessly if it thinks it's a temp error. + // But here the agent probably doesn't handle acks effectively anyway. + console.log(`[WS] Received ${skippedCount} readings, but all were valid (non-numeric data dropped).`); + return send(ws, { type: 'ack', count: 0 }); + } + return sendError(ws, 'No valid readings found in batch'); } try { - const count = insertReadings(db, clientState.devicePrefix, readings); + const count = insertReadings(db, clientState.devicePrefix, validReadings); + if (skippedCount > 0) { + console.log(`[WS] Inserted ${count} valid readings (skipped ${skippedCount} invalid/non-numeric readings).`); + } send(ws, { type: 'ack', count }); } catch (err) { console.error('[WS] Error inserting readings:', err.message);