You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const {
|
|
collectReadings,
|
|
publishAllOffline,
|
|
readRuntimeConfig
|
|
} = require('./acquisition-service');
|
|
|
|
let stopping = false;
|
|
|
|
function wait(milliseconds) {
|
|
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
}
|
|
|
|
async function run() {
|
|
console.log('[ACQUISITION] Recolector iniciado.');
|
|
|
|
while (!stopping) {
|
|
const startedAt = Date.now();
|
|
|
|
try {
|
|
const result = await collectReadings();
|
|
const onlineCount = result.results.filter((item) => item.online).length;
|
|
console.log(
|
|
`[ACQUISITION] ${result.timestamp} ${result.mode}: ${onlineCount}/4 sensores.`
|
|
);
|
|
} catch (error) {
|
|
console.error(`[ACQUISITION] ${error.message}`);
|
|
await publishAllOffline(error);
|
|
}
|
|
|
|
const config = await readRuntimeConfig();
|
|
const elapsed = Date.now() - startedAt;
|
|
await wait(Math.max(100, config.loggingRateSeconds * 1000 - elapsed));
|
|
}
|
|
}
|
|
|
|
function stop() {
|
|
stopping = true;
|
|
}
|
|
|
|
process.on('SIGINT', stop);
|
|
process.on('SIGTERM', stop);
|
|
|
|
run().catch((error) => {
|
|
console.error('[ACQUISITION] Error fatal:', error);
|
|
process.exitCode = 1;
|
|
});
|