From cd85010179349f911b5cb5ee0307a5f17db82cf5 Mon Sep 17 00:00:00 2001 From: EMOTIONS-HUNTER Date: Tue, 23 Jun 2026 12:53:26 -0600 Subject: [PATCH] Corregir lecturas nulas de sensores EZO --- api/acquisition-service.js | 6 +++++- test/api.test.js | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/api/acquisition-service.js b/api/acquisition-service.js index b90df43..35a5212 100644 --- a/api/acquisition-service.js +++ b/api/acquisition-service.js @@ -140,8 +140,11 @@ async function appendReading(filePath, timestamp, value) { async function publishReading(sensorId, rawValue, timestamp, mode) { const sensor = SENSOR_FILES[sensorId]; - const value = Number(rawValue); const jsonPath = path.join(DATA_DIRECTORY, sensor.json); + const hasReading = rawValue !== null && + rawValue !== undefined && + rawValue !== ''; + const value = hasReading ? Number(rawValue) : NaN; if (!Number.isFinite(value)) { await atomicWriteFile(jsonPath, `${JSON.stringify({ @@ -217,6 +220,7 @@ module.exports = { collectReadings, detectAcquisitionMode, publishAllOffline, + publishReading, readRuntimeConfig, writeRuntimeConfig }; diff --git a/test/api.test.js b/test/api.test.js index fc02ab2..90ede88 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -16,7 +16,7 @@ process.env.RUNTIME_CONFIG_FILE = temporaryRuntime; process.env.EZO_MODE = 'demo'; const { app, HISTORY_FILES } = require('../api/server'); -const { collectReadings } = require('../api/acquisition-service'); +const { collectReadings, publishReading } = require('../api/acquisition-service'); let server; let baseUrl; @@ -140,6 +140,20 @@ test('demo acquisition writes live JSON and CSV files', async () => { } }); +test('hardware null readings are marked offline and not appended as zero', async () => { + const timestamp = new Date().toISOString(); + const csvPath = path.join(temporaryLogs, 'ph.csv'); + fs.rmSync(csvPath, { force: true }); + + const online = await publishReading('ph', null, timestamp, 'hardware'); + const data = JSON.parse(fs.readFileSync(path.join(temporaryData, 'EZOPH.json'))); + + assert.equal(online, false); + assert.equal(data.online, false); + assert.equal(data.error, 'Lectura no disponible'); + assert.equal(fs.existsSync(csvPath), false); +}); + test('clears historical CSV files and preserves headers', async () => { for (const name of Object.keys(HISTORY_FILES)) { fs.writeFileSync(path.join(temporaryLogs, `${name}.csv`), 'old,data\n1,2\n');