diff --git a/server/src/adsb.mjs b/server/src/adsb.mjs index b0bcca7..aff4436 100644 --- a/server/src/adsb.mjs +++ b/server/src/adsb.mjs @@ -155,10 +155,30 @@ export function classifyAircraft(row) { return 'unknown'; } -export function enrichAircraft(a) { +export async function enrichAircraft(a) { if (!a.hex) return a; const row = db.prepare('SELECT * FROM aircraft WHERE icao24 = ?').get(a.hex.toUpperCase()); - if (!row) return { ...a, type: isIcaoInMilitaryRange(a.hex) ? 'military' : 'unknown' }; + if (!row) { + const resp = await fetch(`https://hexdb.io/api/v1/aircraft/${a.hex.toUpperCase()}`); + if (resp.ok) { + const found = await resp.json(); + const insert = db.prepare(` + INSERT OR IGNORE INTO aircraft (icao24, registration, manufacturer, aircraft, model, operator) + VALUES (@icao24, @registration, @manufacturer, @aircraft, @model, @operator) + `); + const mapped = { + icao24: a.hex.toUpperCase(), + registration: found.Registration || null, + manufacturer: found.Manufacturer || null, + aircraft: found.ICAOTypeCode || null, + model: found.Type || null, + operator: found.RegisteredOwners || null, + }; + insert.run(mapped); + return { ...a, ...mapped, type: classifyAircraft(mapped) }; + } + return { ...a, type: isIcaoInMilitaryRange(a.hex) ? 'military' : 'unknown' }; + } return { ...a, ...row, type: classifyAircraft(row) }; } @@ -174,6 +194,7 @@ export async function getADSB() { const aircraft = j.aircraft || []; for (const a of aircraft) { + a.type = 'unknown'; if (!a.hex || !a.lat || !a.lon) continue; const key = a.hex.toLowerCase(); if (!history.has(key)) history.set(key, []); @@ -182,7 +203,7 @@ export async function getADSB() { if (trail.length > MAX_HISTORY) trail.shift(); } - return aircraft.map(enrichAircraft); + return Promise.all(aircraft.map(enrichAircraft)); } export async function getADSBHistory(icao) {