Files
weather-station/server/airtraffic.mjs
2026-06-22 00:08:48 -04:00

51 lines
1.5 KiB
JavaScript

import { cfg } from './config.mjs'
import { writeFile, readFile } from 'fs/promises'
import { existsSync } from 'fs'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
const DIR = dirname(fileURLToPath(import.meta.url))
const SHAPES_PATH = resolve(DIR, 'shapes.json')
export async function getShapes() {
if (existsSync(SHAPES_PATH)) {
return JSON.parse(await readFile(SHAPES_PATH, 'utf8'))
}
const r = await fetch('https://raw.githubusercontent.com/wiedehopf/tar1090/master/html/markers.min.js')
const text = await r.text()
// tar1090 exposes a var with all the shape definitions
let shapes = {}
try {
const match = text.match(/var\s+_aircraft_markers\s*=\s*(\{[\s\S]*?\});/)
if (match) shapes = JSON.parse(match[1])
} catch (e) {
console.error('Failed to parse tar1090 shapes:', e)
}
writeFile(SHAPES_PATH, JSON.stringify(shapes), () => {})
return shapes
}
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 }
}