This commit is contained in:
2026-06-22 00:02:16 -04:00
parent 2c8666db56
commit 2e5227f679
9 changed files with 635 additions and 120 deletions

49
server/airtraffic.mjs Normal file
View File

@@ -0,0 +1,49 @@
import { cfg } from './config.mjs'
import { writeFile, readFile, 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 }
}

View File

@@ -9,6 +9,7 @@ export function cfg() {
config({ path: resolve(ROOT, '.env.local'), override: true })
return {
PORT: process.env.PORT || 3000,
ADSB_URL: process.env.ADSB_URL || '',
INFLUX_URL: process.env.INFLUX_URL || 'http://localhost:8086',
INFLUX_TOKEN: process.env.INFLUX_TOKEN || '',
INFLUX_ORG: process.env.INFLUX_ORG || 'weather',

BIN
server/public/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@@ -9,6 +9,7 @@ import { getOpenMeteo } from './openmeteo.mjs'
import { apiReference } from '@scalar/express-api-reference'
import { spec } from './spec.mjs'
import { existsSync } from 'fs'
import {getAirTraffic, getAirTrafficHistory, getAirTrafficShapes} from './airtraffic.mjs';
const app = express()
const DIR = dirname(fileURLToPath(import.meta.url))
@@ -119,6 +120,11 @@ app.get('/api/daily', async (req, res) => {
res.json(filterArr(result, fields))
})
// -- ADSB ----------------------------------------------------------------------
app.get('/api/air-traffic', async (req, res) => res.json(await getAirTraffic()))
app.get('/api/air-traffic/:icao', async (req, res) => res.json(await getAirTrafficHistory(req.params.icao)))
app.get('/api/air-traffic-shapes', async (req, res) => res.json(await getShapes()))
// ── DOCS ----------------------------------------------------------------------
app.get('/openapi.json', (req, res) => res.json(spec))