fixing adsb connection

This commit is contained in:
2026-06-22 00:40:57 -04:00
parent 4edd856eaf
commit 4300ebc532

View File

@@ -3,9 +3,12 @@ import { readFile } from 'fs/promises'
import { resolve, dirname } from 'path' import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
const DIR = dirname(fileURLToPath(import.meta.url)) const DIR = dirname(fileURLToPath(import.meta.url))
const SHAPES_PATH = resolve(DIR, 'public', 'aircraft.json') 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() { export async function getShapes() {
return JSON.parse(await readFile(SHAPES_PATH, 'utf8')) return JSON.parse(await readFile(SHAPES_PATH, 'utf8'))
} }
@@ -13,20 +16,22 @@ export async function getShapes() {
export async function getAirTraffic() { export async function getAirTraffic() {
const { ADSB_URL } = cfg() const { ADSB_URL } = cfg()
if (!ADSB_URL) return { data: [] } if (!ADSB_URL) return { data: [] }
const r = await fetch(`${ADSB_URL}/re-api/?all`) const r = await fetch(`${ADSB_URL}/data/aircraft.json`)
const j = await r.json() const j = await r.json()
return { data: j.aircraft || [] } 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) { export async function getAirTrafficHistory(icao) {
const { ADSB_URL } = cfg() return { history: history.get(icao?.toLowerCase()) || [] }
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 }
} }