From 4300ebc532fded56af5049343599d1267de1c0a7 Mon Sep 17 00:00:00 2001 From: ztimson Date: Mon, 22 Jun 2026 00:40:57 -0400 Subject: [PATCH] fixing adsb connection --- server/airtraffic.mjs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/server/airtraffic.mjs b/server/airtraffic.mjs index c44e027..a88dfc5 100644 --- a/server/airtraffic.mjs +++ b/server/airtraffic.mjs @@ -3,9 +3,12 @@ import { readFile } from 'fs/promises' import { resolve, dirname } from 'path' 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 history = new Map() // icao -> [{lat, lon, alt, ts}] +const MAX_HISTORY = 500 + export async function getShapes() { return JSON.parse(await readFile(SHAPES_PATH, 'utf8')) } @@ -13,20 +16,22 @@ export async function getShapes() { export async function getAirTraffic() { const { ADSB_URL } = cfg() 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() - 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) { - 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 } + return { history: history.get(icao?.toLowerCase()) || [] } }