38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import { cfg } from './config.mjs'
|
|
import { readFile } from 'fs/promises'
|
|
import { resolve, dirname } from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
const DIR = dirname(fileURLToPath(import.meta.url))
|
|
const SHAPES_PATH = resolve(DIR, 'public', 'aircraft.json')
|
|
|
|
const history = new Map() // icao -> [{lat, lon, alt, ts}]
|
|
const MAX_HISTORY = 500
|
|
|
|
export async function getShapes() {
|
|
return JSON.parse(await readFile(SHAPES_PATH, 'utf8'))
|
|
}
|
|
|
|
export async function getAirTraffic() {
|
|
const { ADSB_URL } = cfg()
|
|
if (!ADSB_URL) return { data: [] }
|
|
const r = await fetch(`${ADSB_URL}/data/aircraft.json`)
|
|
const j = await r.json()
|
|
const aircraft = j.aircraft || []
|
|
|
|
for (const a of aircraft) {
|
|
if (!a.hex || !a.lat || !a.lon) continue
|
|
const key = a.hex.toLowerCase()
|
|
if (!history.has(key)) history.set(key, [])
|
|
const trail = history.get(key)
|
|
trail.push({ latitude: a.lat, longitude: a.lon, altitude: a.alt_baro || 0, ts: Date.now() })
|
|
if (trail.length > MAX_HISTORY) trail.shift()
|
|
}
|
|
|
|
return { data: aircraft }
|
|
}
|
|
|
|
export async function getAirTrafficHistory(icao) {
|
|
return { history: history.get(icao?.toLowerCase()) || [] }
|
|
}
|