Versión Inicial
This commit is contained in:
96
libs/api.js
Normal file
96
libs/api.js
Normal file
@@ -0,0 +1,96 @@
|
||||
// Importamos las utilidades que necesitemos y la configuración del programa
|
||||
import { sleep } from './utils.js';
|
||||
import config from '../config.js';
|
||||
|
||||
// Creamos una lista de elementos que contendrá las peticiones acumuladas para enviarlas de golpe
|
||||
const peticionesRetrasadas = [];
|
||||
|
||||
/**
|
||||
* Añade el dato a la lista de peticiones en el formato que necesita la api
|
||||
* @param {object} datos Objeto con los datos a subir
|
||||
*/
|
||||
async function subirDato(datos) {
|
||||
if (datos === null || datos === undefined) {
|
||||
return;
|
||||
}
|
||||
const datosParaEnviar = {
|
||||
created_at: datos.datetime,
|
||||
delta_t: 1,
|
||||
field1: datos.paquetes,
|
||||
field2: datos.temperatura,
|
||||
field3: datos.presion,
|
||||
field4: datos.altitudSegunPresion,
|
||||
latitude: datos.latitud,
|
||||
longitude: datos.longitud
|
||||
};
|
||||
|
||||
//console.log(datosParaEnviar);
|
||||
|
||||
peticionesRetrasadas.push(datosParaEnviar);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sube los datos de golpe al ThingSpeak
|
||||
* @param {Array} datosParaSubir Array de objetos con los datos a subir
|
||||
* @returns {Promise<boolean>} True si se subieron los datos correctamente, false si no
|
||||
*/
|
||||
async function subirDatosDeGolpe(datosParaSubir) {
|
||||
//console.log("Subiendo datos de golpe...");
|
||||
|
||||
// Si no hay datos, no subimos nada
|
||||
if(datosParaSubir.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Preparamos los datos para subir añadiendo la apiKey
|
||||
const datosParaEnviar = {
|
||||
write_api_key: config.thingSpeak.writeKey,
|
||||
updates: datosParaSubir
|
||||
}
|
||||
|
||||
//console.log(datosParaEnviar);
|
||||
|
||||
// Hacemos la petición a la API de ThingSpeak
|
||||
let resultado = false;
|
||||
await fetch(`https://api.thingspeak.com/channels/${config.thingSpeak.channelId}/bulk_update.json`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(datosParaEnviar) // Convertimos el objeto a JSON para enviarlo
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
//console.log(data);
|
||||
resultado = true;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error al subir los datos:', error);
|
||||
})
|
||||
return resultado;
|
||||
}
|
||||
|
||||
async function bucleSubidaDatos() {
|
||||
while(true) {
|
||||
await sleep(20000);
|
||||
console.log("Lista por subir: " + peticionesRetrasadas.length);
|
||||
const datosQueSeVanASubir = JSON.parse(JSON.stringify(peticionesRetrasadas))
|
||||
if (await subirDatosDeGolpe(datosQueSeVanASubir)) { // Con eso creamos una copia de los datos del array
|
||||
if (peticionesRetrasadas.length == datosQueSeVanASubir.length) {
|
||||
peticionesRetrasadas.length = 0;
|
||||
} else {
|
||||
peticionesRetrasadas.splice(0, datosQueSeVanASubir.length);
|
||||
//console.log("Datos que se hubieran perdido: " + peticionesRetrasadas)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
subirDato
|
||||
}
|
||||
|
||||
bucleSubidaDatos();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user