lectura de informacion, se realiza cada 30 segundos parametrizado

master
Francisco Sandoval 2024-03-27 12:56:48 -03:00
parent dc1caf47f7
commit 6b96f99718
3 changed files with 53 additions and 40 deletions

View File

@ -77,10 +77,10 @@
<style>
h1, h2 {
h2 {
text-align: center;
}
h1,h2, h3 {
h2, h3 {
color: #001b3b;
text-rendering: geometricPrecision;
text-wrap: balance;

View File

@ -1,3 +1,5 @@
import { json } from '@sveltejs/kit';
const {
VITE_TRANSPORTE_API: base,
@ -5,32 +7,31 @@ const {
VITE_TRANSPORTE_PASS: password
} = import.meta.env;
let intentos = 0;
export async function load({ url }) {
const id = url.searchParams.get('id');
export async function GET({ url }) {
const id = url.searchParams.get('id');
if (!id) return null;
return await tryFetch(id)
const data = await tryFetch(id)
return json(data)
}
function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(() => { resolve() }, seconds * 1000)
})
}
async function tryFetch(id) {
async function tryFetch(id, intentos = 0) {
try {
const token = await getToken() // token de autorizacion
const data = await fetchParadero(id, token) // data de dispositivo
intentos = 0;
return data;
} catch (error) {
if (intentos < 3) {
intentos++;
console.log('Reintento', intentos + 1)
await sleep(2); // espera 2 segundos
return await tryFetch(id)
return await tryFetch(id, intentos + 1)
}
return null;
}
@ -43,13 +44,11 @@ async function getToken() {
body: JSON.stringify({ rut: user, password })
})
if (!res.ok) throw await res.text()
const { token } = await res.json()
// console.log({ base })
return token;
} catch (error) {
console.log('token',{ error })
throw error;
}
}
@ -73,5 +72,6 @@ async function fetchParadero(id, token) {
} catch (error) {
console.log('paradero',{ error })
throw error;
}
}

View File

@ -1,41 +1,54 @@
<script>
import ImagenBus from "$lib/images/logo-autobus.png";
import { onMount } from "svelte";
const { VITE_REFRESH_DATA: segundosRefresca } = import.meta.env || {};
export let data;
// export let data;
let nombreParadero = null;
let paraderoData = null;
let timeRefresh = null
const { NombreParadero: nombreParadero = null, ...paraderoData } = data?.GetInfoDeviceResponse || {};
onMount(() => {
const url = new URL(document.location.href)
const id = url.searchParams.get('id');
init({ id, origin: url.origin })
const colores = [
"#5fbabe",
"#f05eb1",
"#5fab88",
"#f7646c",
"#5e66b5",
"#73776e",
];
return () => {
console.log('unmount')
timeRefresh && clearTimeout(timeRefresh)
}
})
function getColorAleatorio(index) {
return colores[index % 6];
async function init({ id, origin }) {
const result = await fetchData(`${origin}/api/paradero?id=${id}`);
nombreParadero = result.GetInfoDeviceResponse.NombreParadero;
paraderoData = result.GetInfoDeviceResponse;
timeRefresh = setTimeout(() => init({ id, origin }), +segundosRefresca * 1000);
}
function formatTimeMinutes({ EstimadaGPS: horaPlanificada, patente }) {
// const diferenciaMinutos = calcularDiferenciaMinutos(horaPlanificada);
const horaActual = new Date();
const horaUtc = horaActual.toISOString().substring(0, 10) + "T" + horaPlanificada + "Z";
const horaTrayecto = new Date(horaUtc);
const diferenciaMinutos = (horaTrayecto.getTime() - horaActual.getTime()) / (1000 * 60);
if (diferenciaMinutos <= 3) {
return "< a 3 Minutos";
} else if (diferenciaMinutos <= 5) {
return "3-5 Minutos";
} else if (diferenciaMinutos <= 10) {
return "Menos de 10 minutos";
} else {
return "Más de 10 minutos";
async function fetchData(url) {
try {
const res = await fetch(url);
if (!res.ok) throw await res.text()
return await res.json()
} catch (error) {
console.log({ error })
return null;
}
}
// const colores = [
// "#5fbabe",
// "#f05eb1",
// "#5fab88",
// "#f7646c",
// "#5e66b5",
// "#73776e",
// ];
function LetraoNumeroMicro(cadena, flag) {
if (!cadena || (flag !== 0 && flag !== 1)) {
return "Error: Entrada no válida";