90 lines
3.4 KiB
JavaScript
90 lines
3.4 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import readline from 'node:readline';
|
|
import { dataLineToObject } from './libs/utils.js';
|
|
|
|
// Ruta del CSV de salida: tercer argumento o 'csvParaSubir.csv' por defecto
|
|
const outputPath = process.argv[3] || path.join('.', 'csvParaSubir.csv');
|
|
const output = fs.createWriteStream(outputPath);
|
|
// Timestamp de la línea anterior para evitar duplicados
|
|
let prevTimestamps = [];
|
|
// Encabezados para el CSV de salida
|
|
output.write('created_at,entry_id,field1,field2,field3,field4,field5,field6,field7,latitude,longitude,elevation,status\n');
|
|
|
|
// Ruta del CSV: primer argumento o 'logs/datos.csv' por defecto
|
|
const inputPath = process.argv[2] || path.join('.', 'datos.csv');
|
|
|
|
// Comprobamos que el archivo existe
|
|
if (!fs.existsSync(inputPath)) {
|
|
console.error(`Archivo no encontrado: ${inputPath}`);
|
|
process.exit(1);
|
|
}
|
|
// Creamos interfaz de lectura de líneas
|
|
const rl = readline.createInterface({
|
|
input: fs.createReadStream(inputPath),
|
|
crlfDelay: Infinity
|
|
});
|
|
|
|
rl.on('line', async (line) => {
|
|
const obj = dataLineToObject(line);
|
|
if (obj) {
|
|
try {
|
|
if (obj === null || obj === undefined) {
|
|
return;
|
|
}
|
|
const datosParaEnviar = {
|
|
created_at: obj.datetimeGPS.toISOString(),
|
|
delta_t: 1, // Activar si da problemas con el tiempo
|
|
field1: obj.paquetes,
|
|
field2: obj.temperatura,
|
|
field3: obj.presion,
|
|
field4: obj.altitudSegunPresion,
|
|
field5: obj.altitudMetros,
|
|
field6: obj.velocidadKmph,
|
|
field7: obj.numSatelites,
|
|
latitude: obj.latitud,
|
|
longitude: obj.longitud
|
|
};
|
|
// Ajustar created_at si coincide con alguno de los últimos 10 timestamps
|
|
let currentDate = new Date(datosParaEnviar.created_at);
|
|
// Mientras choque con alguno de los últimos 10, suma un segundo
|
|
while (prevTimestamps.some(ts => ts.getTime() === currentDate.getTime())) {
|
|
currentDate.setSeconds(currentDate.getSeconds() + 1);
|
|
}
|
|
datosParaEnviar.created_at = currentDate.toISOString();
|
|
// Actualizar lista y mantener solo 10 elementos
|
|
prevTimestamps.push(new Date(datosParaEnviar.created_at));
|
|
if (prevTimestamps.length > 10) {
|
|
prevTimestamps.shift();
|
|
}
|
|
// Escribir una línea en el CSV de salida
|
|
const entryId = obj.paquetes;
|
|
const csvLine = [
|
|
datosParaEnviar.created_at,
|
|
entryId,
|
|
datosParaEnviar.field1,
|
|
datosParaEnviar.field2,
|
|
datosParaEnviar.field3,
|
|
datosParaEnviar.field4,
|
|
datosParaEnviar.field5,
|
|
datosParaEnviar.field6,
|
|
datosParaEnviar.field7,
|
|
datosParaEnviar.latitude,
|
|
datosParaEnviar.longitude,
|
|
'', // elevation
|
|
'' // status
|
|
].join(',') + '\n';
|
|
output.write(csvLine);
|
|
//console.log(`Enviado paquete ${obj.paquetes}`);
|
|
} catch (err) {
|
|
console.error('Error al subir dato:', err);
|
|
}
|
|
}
|
|
});
|
|
|
|
rl.on('close', () => {
|
|
output.end();
|
|
console.log(`CSV listo para subir a thingspeak: ${outputPath}`);
|
|
process.exit(0);
|
|
});
|