ADSB enrichment
This commit is contained in:
579560
server/data/aircraft_db.csv
Normal file
579560
server/data/aircraft_db.csv
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@ import {cfg} from './config.mjs';
|
||||
import {resolve, dirname} from 'path';
|
||||
import {fileURLToPath} from 'url';
|
||||
import * as fs from 'node:fs';
|
||||
import {fromCsv} from '@ztimson/utils';
|
||||
|
||||
const DIR = dirname(fileURLToPath(import.meta.url));
|
||||
const DATA = resolve(DIR, '../data');
|
||||
@@ -14,57 +15,22 @@ const CSV_URL = 'https://s3.opensky-network.org/data-samples/metadata/aircraft-d
|
||||
|
||||
let aircraftMap = new Map();
|
||||
|
||||
function parseCsv(text) {
|
||||
const map = new Map();
|
||||
const lines = text.split('\n');
|
||||
for(let i = 1; i < lines.length; i++) {
|
||||
const line = lines[i].trim();
|
||||
if(!line) continue;
|
||||
const cols = line.split(',');
|
||||
const icao24 = cols[0]?.replace(/'/g, '').toUpperCase();
|
||||
if(!icao24) continue;
|
||||
map.set(icao24, {
|
||||
reg: cols[25]?.replace(/'/g, '') || null,
|
||||
type_code: cols[29]?.replace(/'/g, '') || null,
|
||||
desc: cols[14]?.replace(/'/g, '') || null,
|
||||
});
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
async function loadAircraftCsv() {
|
||||
let text = null;
|
||||
let fromCache = false;
|
||||
|
||||
try {
|
||||
const res = await fetch(CSV_URL);
|
||||
if(!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
text = await res.text();
|
||||
} catch(e) {
|
||||
console.warn(`✈️ Failed to download CSV (${e.message}), trying cache...`);
|
||||
if(fs.existsSync(CSV_CACHE)) {
|
||||
text = await fs.promises.readFile(CSV_CACHE, 'utf8');
|
||||
fromCache = true;
|
||||
} else {
|
||||
throw new Error('No aircraft CSV available and no cache found');
|
||||
}
|
||||
}
|
||||
|
||||
aircraftMap = parseCsv(text);
|
||||
console.log(`✈️ Aircraft loaded: ${aircraftMap.size} (${fromCache ? 'cache' : 'fresh'})`);
|
||||
|
||||
// Save to cache in background if fresh
|
||||
if(!fromCache) {
|
||||
fs.promises.writeFile(CSV_CACHE, text).catch(e =>
|
||||
console.warn('✈️ Failed to cache aircraft CSV:', e.message)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initAircraftDb() {
|
||||
if(!fs.existsSync(DATA)) fs.mkdirSync(DATA, {recursive: true});
|
||||
console.log('✈️ Downloading aircraft CSV...');
|
||||
await loadAircraftCsv();
|
||||
const missing = !fs.existsSync(CSV_CACHE);
|
||||
console.log(`✈️ Aircraft cache ${missing ? 'downloading' : 'loading'}`);
|
||||
if(missing) {
|
||||
const res = await fetch(CSV_URL);
|
||||
if(!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const text = await res.text();
|
||||
fs.promises.writeFile(CSV_CACHE, text);
|
||||
aircraftMap = fromCsv(text, true);
|
||||
console.log(`✈️ Aircraft loaded: ${aircraftMap.length} (fresh)`);
|
||||
} else {
|
||||
const text = fs.readFileSync(CSV_CACHE, 'utf8');
|
||||
aircraftMap = fromCsv(text, true);
|
||||
console.log(`✈️ Aircraft loaded: ${aircraftMap.length} (cache)`);
|
||||
}
|
||||
}
|
||||
|
||||
export function enrichAircraft(a) {
|
||||
@@ -85,7 +51,7 @@ export async function getShapes() {
|
||||
|
||||
export async function getADSB() {
|
||||
const {ADSB_URL} = cfg();
|
||||
if(!ADSB_URL) return {data: []};
|
||||
if(!ADSB_URL) return [];
|
||||
const r = await fetch(`${ADSB_URL}:8080/data/aircraft.json`);
|
||||
const j = await r.json();
|
||||
const aircraft = j.aircraft || [];
|
||||
|
||||
Reference in New Issue
Block a user