-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfly.html
More file actions
384 lines (347 loc) · 12 KB
/
Copy pathfly.html
File metadata and controls
384 lines (347 loc) · 12 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!DOCTYPE html>
<html lang="no">
<head>
<!-- Flyradar Siljan v1.6 -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flyradar Siljan</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #1a1a2e;
color: #eee;
}
.header {
background: #252542;
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.header h1 { font-size: 1.3rem; }
.header a { color: #60a5fa; text-decoration: none; }
.header a:hover { text-decoration: underline; }
.container {
display: flex;
height: calc(100vh - 60px);
}
#map {
flex: 1;
min-height: 400px;
}
.sidebar {
width: 320px;
background: #252542;
padding: 15px;
overflow-y: auto;
}
.sidebar h2 {
font-size: 1rem;
margin-bottom: 10px;
color: #888;
text-transform: uppercase;
letter-spacing: 1px;
}
.aircraft-list {
display: flex;
flex-direction: column;
gap: 10px;
}
.aircraft-card {
background: #1a1a2e;
border-radius: 8px;
padding: 12px;
cursor: pointer;
transition: background 0.2s;
border-left: 3px solid #60a5fa;
}
.aircraft-card:hover {
background: #2a2a4e;
}
.aircraft-card.selected {
background: #2a2a4e;
border-left-color: #4ade80;
}
.ac-flight {
font-weight: bold;
font-size: 1.1rem;
color: #60a5fa;
}
.ac-details {
font-size: 0.85rem;
color: #aaa;
margin-top: 5px;
}
.ac-distance {
font-size: 0.8rem;
color: #4ade80;
margin-top: 3px;
}
.stats {
margin-top: 20px;
padding-top: 15px;
border-top: 1px solid #333;
}
.stat-row {
display: flex;
justify-content: space-between;
padding: 5px 0;
font-size: 0.85rem;
}
.stat-label { color: #888; }
.stat-value { color: #eee; }
.updated {
margin-top: 15px;
font-size: 0.75rem;
color: #666;
text-align: center;
}
@media (max-width: 768px) {
.container { flex-direction: column; }
.sidebar { width: 100%; height: 40vh; }
#map { height: 60vh; }
}
</style>
</head>
<body>
<div class="header">
<h1>✈️ Flyradar Siljan</h1>
<a href="index.html">← Tilbake til værdata</a>
</div>
<div class="container">
<div id="map"></div>
<div class="sidebar">
<h2>Fly i nærheten</h2>
<div class="aircraft-list" id="aircraftList">
<div style="color: #888; font-size: 0.9rem;">Laster flydata...</div>
</div>
<div class="stats">
<h2>Statistikk</h2>
<div class="stat-row">
<span class="stat-label">Fly akkurat nå:</span>
<span class="stat-value" id="statNow">-</span>
</div>
<div class="stat-row">
<span class="stat-label">I dag:</span>
<span class="stat-value" id="statToday">-</span>
</div>
<div class="stat-row">
<span class="stat-label">Siste 7 dager:</span>
<span class="stat-value" id="stat7d">-</span>
</div>
</div>
<div class="updated">
Sist oppdatert: <span id="lastUpdate">-</span>
</div>
</div>
</div>
<script>
// Config
const SILJAN_LAT = 59.2924;
const SILJAN_LON = 9.7072;
const SUPABASE_URL = 'https://ygxhkdwhjyqpkmfzezxv.supabase.co';
const SUPABASE_KEY = 'sb_publishable_wiJxJlwGbklxNgNjL4riJg_Hh55ayG0';
// State
let map;
let markers = {};
let trails = {};
let selectedHex = null;
// Get hex from URL parameter
const urlParams = new URLSearchParams(window.location.search);
selectedHex = urlParams.get('hex');
// Init map
function initMap() {
map = L.map('map').setView([SILJAN_LAT, SILJAN_LON], 10);
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
attribution: '© OpenStreetMap, © CARTO',
maxZoom: 19
}).addTo(map);
// Add Siljan marker
L.circleMarker([SILJAN_LAT, SILJAN_LON], {
radius: 8,
fillColor: '#4ade80',
color: '#fff',
weight: 2,
fillOpacity: 0.8
}).addTo(map).bindPopup('📍 Siljan');
// Add 100km circle
L.circle([SILJAN_LAT, SILJAN_LON], {
radius: 100000,
color: '#60a5fa',
fillColor: '#60a5fa',
fillOpacity: 0.05,
weight: 1,
dashArray: '5, 10'
}).addTo(map);
}
// Heading to direction
function headingToDirection(degrees) {
if (degrees === null || degrees === undefined) return '';
const directions = ['N', 'NØ', 'Ø', 'SØ', 'S', 'SV', 'V', 'NV'];
const index = Math.round(degrees / 45) % 8;
return directions[index];
}
// Unit conversions
function knotsToKmh(kts) { return Math.round(kts * 1.852); }
function feetToMeters(ft) { return Math.round(ft * 0.3048); }
// Create aircraft icon (SVG rotated with heading)
function createAircraftIcon(track, isSelected) {
const rotation = track || 0;
const color = isSelected ? '#4ade80' : '#60a5fa';
const svg = `
<svg viewBox="0 0 24 24" width="24" height="24" style="transform: rotate(${rotation}deg);">
<path d="M12 2 L14 8 L20 10 L14 12 L14 18 L12 16 L10 18 L10 12 L4 10 L10 8 Z"
fill="${color}" stroke="#fff" stroke-width="0.5"/>
</svg>`;
return L.divIcon({
html: svg,
iconSize: [24, 24],
iconAnchor: [12, 12],
className: 'aircraft-icon'
});
}
// Update aircraft
async function updateAircraft() {
try {
const response = await fetch(
`${SUPABASE_URL}/rest/v1/aircraft_live?select=*&order=timestamp.asc`,
{ headers: { 'apikey': SUPABASE_KEY } }
);
const allPositions = await response.json();
// Clear old markers and trails
Object.values(markers).forEach(m => map.removeLayer(m));
Object.values(trails).forEach(t => map.removeLayer(t));
markers = {};
trails = {};
// 30 minute timeout threshold
const timeoutMs = 30 * 60 * 1000;
const now = Date.now();
// Group positions by hex
const aircraftByHex = {};
for (const pos of allPositions) {
if (!aircraftByHex[pos.hex]) {
aircraftByHex[pos.hex] = [];
}
aircraftByHex[pos.hex].push(pos);
}
// Filter out aircraft with last position older than 30 min
const activeAircraft = {};
for (const [hex, positions] of Object.entries(aircraftByHex)) {
const latest = positions[positions.length - 1];
const posTime = new Date(latest.timestamp).getTime();
if (now - posTime <= timeoutMs) {
activeAircraft[hex] = positions;
}
}
// Update list and markers
const listEl = document.getElementById('aircraftList');
const uniqueAircraft = Object.keys(activeAircraft);
if (uniqueAircraft.length === 0) {
listEl.innerHTML = '<div style="color: #888; font-size: 0.9rem;">Ingen fly i nærheten</div>';
document.getElementById('statNow').textContent = '0';
} else {
listEl.innerHTML = '';
document.getElementById('statNow').textContent = uniqueAircraft.length;
// Sort by latest distance
const sortedAircraft = uniqueAircraft
.map(hex => {
const positions = activeAircraft[hex];
return { hex, positions, latest: positions[positions.length - 1] };
})
.sort((a, b) => a.latest.distance_km - b.latest.distance_km);
sortedAircraft.forEach(({ hex, positions, latest }) => {
const flight = latest.flight?.trim() || hex;
const isSelected = hex === selectedHex;
const direction = headingToDirection(latest.track);
// Draw trail (polyline from all positions) - highlighted if selected
if (positions.length > 1) {
const trailCoords = positions.map(p => [p.lat, p.lon]);
const trail = L.polyline(trailCoords, {
color: isSelected ? '#4ade80' : '#60a5fa',
weight: isSelected ? 1.5 : 0.7,
opacity: isSelected ? 0.9 : 0.5,
dashArray: isSelected ? null : '5, 5'
}).addTo(map);
trails[hex] = trail;
}
// Add marker at latest position
const marker = L.marker([latest.lat, latest.lon], {
icon: createAircraftIcon(latest.track, isSelected)
}).addTo(map);
const speedKts = latest.speed_kts || 0;
const altFt = latest.altitude_ft || 0;
const speedKmh = knotsToKmh(speedKts);
const altM = feetToMeters(altFt);
marker.bindPopup(`
<b>${flight}</b><br>
${altFt} ft (${altM} m)<br>
${speedKts} kts (${speedKmh} km/t) ${direction}<br>
${latest.distance_km} km fra Siljan
`);
// Click on marker to select and highlight trace
marker.on('click', () => {
selectedHex = hex;
updateAircraft();
});
markers[hex] = marker;
// If selected, open popup
if (isSelected) {
marker.openPopup();
}
// Add to list
const card = document.createElement('div');
card.className = `aircraft-card${isSelected ? ' selected' : ''}`;
card.innerHTML = `
<div class="ac-flight">✈️ ${flight}</div>
<div class="ac-details">${altFt} ft (${altM} m) • ${speedKts} kts (${speedKmh} km/t) • ${direction}</div>
<div class="ac-distance">${latest.distance_km} km fra Siljan</div>
`;
card.onclick = () => {
selectedHex = hex;
marker.openPopup();
updateAircraft(); // Refresh to update selection
};
listEl.appendChild(card);
});
}
document.getElementById('lastUpdate').textContent = new Date().toLocaleTimeString('nb-NO');
} catch (err) {
console.error('Feil ved henting av flydata:', err);
}
}
// Get stats
async function updateStats() {
try {
// Today
const today = new Date().toISOString().split('T')[0];
const todayResp = await fetch(
`${SUPABASE_URL}/rest/v1/aircraft_stats?select=hex&date=eq.${today}`,
{ headers: { 'apikey': SUPABASE_KEY } }
);
const todayData = await todayResp.json();
document.getElementById('statToday').textContent = todayData?.length || 0;
// Last 7 days
const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
const weekResp = await fetch(
`${SUPABASE_URL}/rest/v1/aircraft_stats?select=hex&date=gte.${weekAgo}`,
{ headers: { 'apikey': SUPABASE_KEY } }
);
const weekData = await weekResp.json();
document.getElementById('stat7d').textContent = weekData?.length || 0;
} catch (err) {
console.error('Feil ved henting av statistikk:', err);
}
}
// Init
initMap();
updateAircraft();
updateStats();
// Refresh every 30 seconds
setInterval(updateAircraft, 30000);
setInterval(updateStats, 300000); // Stats every 5 min
</script>
</body>
</html>