Files
weather-station/server/airtraffic.mjs
2026-06-22 00:26:25 -04:00

33 lines
929 B
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')
export async function getShapes() {
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}/re-api/?all`)
const j = await r.json()
return { data: j.aircraft || [] }
}
export async function getAirTrafficHistory(icao) {
const { ADSB_URL } = cfg()
if (!ADSB_URL) return { history: [] }
const r = await fetch(`${ADSB_URL}/re-api/?trace&icao=${icao}`)
const j = await r.json()
const history = (j.trace || []).map(t => ({
latitude: t[1],
longitude: t[2],
altitude: t[3] || 0,
}))
return { history }
}