const HEADER_ROW = false; // Define the API URL // const urlStop = 'https://api.swu.de/mobility/v1/stop/attributes/BaseData?StopNumber=1240&ContentScope=extended'; const urlDepartures = 'https://api.swu.de/mobility/v1/stop/passage/Departures?StopNumber=1240&Limit=10'; // const apiUrl = 'Departures.json'; // Make a GET request function refreshData() { fetch(urlDepartures) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => { showAllDepsByTime(data); }) .catch(error => { console.error('Error:', error); }); } // Groups Departures into map by route function groupDepartures(data) { const routes = new Map(); for (let x of data.StopPassage.DepartureData) { if (routes.has(x.RouteNumber)) { routes.get(x.RouteNumber).push(x); } else { routes.set(x.RouteNumber, [x]); } } return routes; } function showAllDepsByTime(data) { const date = new Date(data.StopPassage.CurrentTimestamp); document.getElementById('stationName').innerHTML = data.StopPassage.StopName; document.getElementById('timeBox').innerHTML = date.toLocaleTimeString('de-DE'); const table = getTable( [cell => cell.innerHTML = "Linie", cell => cell.innerHTML = "Ziel", cell => cell.innerHTML = "Abfahrt"], [(cell, datap) => {cell.appendChild(getRouteImage(datap.RouteName)); cell.setAttribute("class", "cellRoute")}, (cell, datap) => {cell.innerHTML = datap.DepartureDirectionText; cell.setAttribute("class", "cellDest")}, (cell, datap) => {cell.innerHTML = "" + Math.floor((datap.DepartureCountdown + datap.DepartureDeviation) / 60); cell.setAttribute("class", "cellTime")}], data.StopPassage.DepartureData.filter(departure => departure.RouteName != '-') ); const contentSection = document.getElementById('contentSection'); if (contentSection.hasChildNodes()) { contentSection.replaceChild(table,contentSection.firstElementChild); } else { contentSection.appendChild(table); } } function getRouteImage(route) { const img = document.createElement('img'); img.src = 'https://www.swu.de/typo3conf/ext/swu_timetables/Resources/Public/Icons/Routes/linie-' + route + '.svg'; img.setAttribute("class","routeImage"); return img; } function getTable(listHead, list, listData) { const table = document.createElement('table'); if (HEADER_ROW) { const headrow = document.createElement('tr'); table.appendChild(headrow); for (let fn of listHead) { const cell = document.createElement('th'); fn(cell); headrow.appendChild(cell); } } for (let dataPoint of listData) { const row = document.createElement('tr'); for (let fn of list) { const cell = document.createElement('td'); fn(cell, dataPoint); row.appendChild(cell); } table.appendChild(row); } return table; } refreshData(); // setInterval(refreshData, 15000);