Recoge y genera nuevos datos
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
/**
|
||||
* Convierte una línea de datos en un objeto
|
||||
* @param {string} line Línea de datos para convertir a objeto
|
||||
@@ -6,6 +8,8 @@
|
||||
function dataLineToObject(line) {
|
||||
//Separamos nuestra línea en diferentes trozos usando como separador el ';'
|
||||
const lineArray = line.split(";");
|
||||
console.log(line);
|
||||
saveDataInCSVFile(line);
|
||||
|
||||
//Devolvemos un objeto con sus propiedades convertidas
|
||||
|
||||
@@ -23,9 +27,10 @@ function dataLineToObject(line) {
|
||||
longitud: parseFloat(lineArray[6].trim()),
|
||||
altitudMetros: parseFloat(lineArray[7].trim()),
|
||||
velocidadKmph: parseFloat(lineArray[8].trim()),
|
||||
direccionONose: parseFloat(lineArray[9].trim()),
|
||||
direccionNorte: parseFloat(lineArray[9].trim()),
|
||||
numSatelites: parseInt(lineArray[10].trim()),
|
||||
datetime: new Date(lineArray[11].trim().replace("Date/Time: ", ""))
|
||||
datetimeGPS: new Date(lineArray[11].trim().replace("Date/Time: ", "")),
|
||||
datetime: new Date().toISOString() // Obtenemos la fecha/hora actual
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error al convertir los datos:', error);
|
||||
@@ -44,8 +49,79 @@ async function sleep(ms) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const fechaHoraActual = new Date();
|
||||
async function saveDataInFile(data) {
|
||||
fs.appendFile(`logs/${fechaHoraActual.toISOString()}.json`, JSON.stringify(data) + ',\n', err => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
} else {
|
||||
//console.log('Datos guardados correctamente');
|
||||
}
|
||||
});
|
||||
|
||||
saveDataInGeoJSONFile(data);
|
||||
}
|
||||
|
||||
var geoJSONData = {
|
||||
type: "FeatureCollection",
|
||||
features: [
|
||||
{
|
||||
"type": "Feature",
|
||||
"properties": {},
|
||||
"geometry": {
|
||||
"coordinates": [],
|
||||
"type": "LineString"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async function saveDataInGeoJSONFile(data) {
|
||||
|
||||
if (data.latitud === null || data.longitud === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
geoJSONData.features.push({
|
||||
type: "Feature",
|
||||
properties: {
|
||||
numPaquete: data.paquetes,
|
||||
temperatura: data.temperatura
|
||||
},
|
||||
geometry: {
|
||||
type: "Point",
|
||||
coordinates: [data.longitud, data.latitud]
|
||||
}
|
||||
});
|
||||
|
||||
geoJSONData.features[0].geometry.coordinates.push([data.longitud, data.latitud]);
|
||||
|
||||
|
||||
fs.writeFile('logs/' + fechaHoraActual.toISOString() + '.geojson', JSON.stringify(geoJSONData), err => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
} else {
|
||||
// file written successfully
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
saveDataInCSVFile("Nº Paquete; Temperatura; Presión; Altitud Según Presión; Latitud; Longitud; Altitud (m); Velocidad (Km/h); Dirección/Norte; Número de Satelites; Fecha/Hora GPS; Fecha/Hora del Sistema");
|
||||
async function saveDataInCSVFile(data) {
|
||||
fs.appendFile(`logs/${fechaHoraActual.toISOString()}.csv`, data + '\n', err => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
} else {
|
||||
//console.log('Datos guardados correctamente');
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Exportamos las funciones para poder usarlas en otras partes del programa
|
||||
export {
|
||||
dataLineToObject,
|
||||
sleep
|
||||
sleep,
|
||||
saveDataInFile
|
||||
}
|
||||
Reference in New Issue
Block a user