This repository was archived by the owner on Jul 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
180 lines (158 loc) · 5.78 KB
/
index.html
File metadata and controls
180 lines (158 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MávTrack</title>
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
/>
<style>
body,
html {
height: 100%;
margin: 0;
}
#map {
height: 100vh;
}
.popup-table {
font-size: 14px;
border-collapse: collapse;
width: 100%;
}
.popup-table td,
.popup-table th {
border: 1px solid #ccc;
padding: 4px;
text-align: left;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
const map = L.map("map").setView([47.0, 19.0], 7);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
}).addTo(map);
const vehicleMarkers = new Map();
function getColorByDelay(delay) {
if (delay <= 4 * 60) return "green";
if (delay <= 19 * 60) return "yellow";
if (delay <= 59 * 60) return "orange";
return "red";
}
async function fetchGraphQL(query) {
const response = await fetch("http://localhost:3000/api/mav", {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0",
},
body: JSON.stringify({ query }),
});
const data = await response.json();
return data.data;
}
async function fetchAndDisplayTrains() {
const vehicleQuery = `{ vehiclePositions(swLat: 45.5, swLon: 16.1, neLat: 48.7, neLon: 22.8, modes: [RAIL, RAIL_REPLACEMENT_BUS]) { trip { gtfsId tripShortName tripHeadsign } vehicleId lat lon label speed heading } }`;
const data = await fetchGraphQL(vehicleQuery);
const seenVehicleIds = new Set();
for (const v of data.vehiclePositions) {
seenVehicleIds.add(v.vehicleId);
fetchTripDelay(v);
}
for (const id of [...vehicleMarkers.keys()]) {
if (!seenVehicleIds.has(id)) {
map.removeLayer(vehicleMarkers.get(id));
vehicleMarkers.delete(id);
}
}
}
async function fetchTripDelay(vehicle) {
const gtfsId = vehicle.trip.gtfsId;
const date = new Date().toISOString().split("T")[0];
const query = `{ trip(id: \"${gtfsId}\", serviceDay: \"${date}\") { tripHeadsign trainCategoryName trainName route { longName shortName } stoptimes { stop { name } realtimeArrival realtimeDeparture arrivalDelay scheduledArrival scheduledDeparture } } }`;
const data = await fetchGraphQL(query);
const trip = data.trip;
if (!trip) return;
const stoptimes = trip.stoptimes;
const now = Math.floor((Date.now() - new Date(date).getTime()) / 1000);
for (let st of stoptimes) {
if (st.realtimeDeparture > now) {
const delay = st.arrivalDelay;
const stopName = st.stop.name;
const scheduled = secondsToHHMM(st.scheduledArrival);
const actual = secondsToHHMM(st.realtimeArrival);
displayMarker(
vehicle,
delay,
stopName,
scheduled,
actual,
stoptimes
);
break;
}
}
}
function secondsToHHMM(seconds) {
const d = new Date(0);
d.setSeconds(seconds);
return d.toISOString().substr(11, 5);
}
function displayMarker(
vehicle,
delay,
stopName,
scheduled,
actual,
stoptimes
) {
const id = vehicle.vehicleId;
const lat = vehicle.lat;
const lon = vehicle.lon;
const color = getColorByDelay(delay);
const name = vehicle.trip.tripShortName;
const headsign = vehicle.trip.tripHeadsign;
const speed = Math.round(vehicle.speed * 3.6);
if (vehicleMarkers.has(id)) {
map.removeLayer(vehicleMarkers.get(id));
}
const circle = L.circleMarker([lat, lon], {
radius: 10,
color: "black",
weight: 2,
fillColor: color,
fillOpacity: 0.9,
});
const hoverText = `<b>${name} → ${headsign}</b> • \n${speed} km/h • \n${Math.round(
delay / 60
)} perc késés`;
circle.bindTooltip(hoverText);
let table =
'<table class="popup-table"><tr><th>Megálló</th><th>Tervezett</th><th>Tényleges</th><th>Késés (perc)</th></tr>';
for (const st of stoptimes) {
const sName = st.stop.name;
const sched = secondsToHHMM(st.scheduledArrival);
const act = secondsToHHMM(st.realtimeArrival);
const d = Math.round(st.arrivalDelay / 60);
table += `<tr><td>${sName}</td><td>${sched}</td><td>${act}</td><td>${d}</td></tr>`;
}
table += "</table>";
const popup = `<b>${name} → ${headsign}</b><br><b>Sebesség:</b> ${speed} km/h<br><b>Köv. megálló:</b> ${stopName}<br><b>Késés:</b> ${Math.round(
delay / 60
)} perc<br><b>Érkezés:</b> ${scheduled} (<span style="color: green">${actual}</span>)<br>${table}`;
circle.bindPopup(popup);
circle.addTo(map);
vehicleMarkers.set(id, circle);
}
fetchAndDisplayTrains();
setInterval(fetchAndDisplayTrains, 60000);
</script>
</body>
</html>