Corregir lecturas nulas de sensores EZO

main
EMOTIONS-HUNTER 1 month ago
parent bcbee4e479
commit cd85010179

@ -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
};

@ -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');

Loading…
Cancel
Save