const params = new URLSearchParams(window.location.search);
const id = params.get('id');
//Sets stop name
const numero_span = document.getElementById('numero-span');
numero_span.textContent=id;
const urlBackend = `https://setaapi.serverissimo.freeddns.org/vehicleinfo/${id}`;
//const urlBackend = `http://localhost:5001/arrivals/${codice}`;
function caricadati(){
var item=[];
fetch(urlBackend)
.then(response => {
if (!response.ok) throw new Error("Errore di risposta nel caricamento dei dati, probabilmente il server API è offline.");
return response.json();
})
.then(data => {
item = data;
})
.then(data => {
const container = document.getElementById('tabella-container');
container.innerHTML = '';
// Creo tabella
const table = document.createElement('table');
// Intestazione
const thead = document.createElement('thead');
thead.innerHTML = `
Informazioni veicolo: |
`;
table.appendChild(thead);
// Corpo tabella
const tbody = document.createElement('tbody');
item.features.forEach(element => {
const bus = element.properties;
var tr = document.createElement('tr');
tr.innerHTML = `
Linea: |
${bus.linea} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Direzione: |
${bus.route_desc} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Tipo linea: |
${bus.service_tag} |
`;
tbody.appendChild(tr);
//Ritardo col +
if(bus.delay>0){
bus.delay="+"+bus.delay;
}
tr = document.createElement('tr');
tr.innerHTML = `
Ritardo/Anticipo: (+/-) |
${bus.delay} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Numero mezzo: |
${bus.vehicle_code} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Modello: |
${bus.model} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Targa: |
${bus.plate_num} |
`;
tbody.appendChild(tr);
//Si o no pedana
if(bus.pedana==1){
bus.pedana="Sì";
}else{
bus.pedana="No";
}
tr = document.createElement('tr');
tr.innerHTML = `
Pedana?: |
${bus.pedana} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Ora si trova a: |
${bus.wp_desc} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Codice percorso: |
${bus.route_code} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Codice corsa: |
${bus.journey_code} |
`;
tbody.appendChild(tr);
//Colore sfondo conta passeggeri
if(bus.num_passeggeri<=bus.posti_totali/4){
tr = document.createElement('tr');
tr.innerHTML = `
Numero passeggeri a bordo: |
${bus.num_passeggeri} |
`;
tbody.appendChild(tr);
}else if(bus.num_passeggeri<=bus.posti_totali/1.8){
tr = document.createElement('tr');
tr.innerHTML = `
Numero passeggeri a bordo: |
${bus.num_passeggeri} |
`;
tbody.appendChild(tr);
}else{
tr = document.createElement('tr');
tr.innerHTML = `
Numero passeggeri a bordo: |
${bus.num_passeggeri} |
`;
tbody.appendChild(tr);
}
tr = document.createElement('tr');
tr.innerHTML = `
Numero posti totali (in piedi + sedili): |
${bus.posti_totali} |
`;
tbody.appendChild(tr);
tr = document.createElement('tr');
tr.innerHTML = `
Posizione: |
GPS |
`;
tbody.appendChild(tr);
});
table.appendChild(tbody);
container.appendChild(table);
})
.catch(err => {
console.error('Errore nel caricamento dati:', err);
document.getElementById('tabella-container').textContent = "Errore nella sintassi dei dati ricevuti.";
});
}
caricadati();
setInterval(caricadati, 60000);