u
This commit is contained in:
@@ -148,15 +148,36 @@ function handleData(ws, message, clientState, db) {
|
|||||||
return sendError(ws, 'Invalid readings: expected non-empty array');
|
return sendError(ws, 'Invalid readings: expected non-empty array');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const validReadings = [];
|
||||||
|
let skippedCount = 0;
|
||||||
|
|
||||||
// Validate readings format
|
// Validate readings format
|
||||||
for (const reading of readings) {
|
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) {
|
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 {
|
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 });
|
send(ws, { type: 'ack', count });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[WS] Error inserting readings:', err.message);
|
console.error('[WS] Error inserting readings:', err.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user