ADSB updates
This commit is contained in:
@@ -21,6 +21,7 @@ const CARGO_OPERATORS = ['fedex', 'ups', 'dhl', 'cargo', 'freight', 'logisti
|
||||
const PASSENGER_OPERATORS = ['airlines', 'airways', 'air ', 'jet', 'easyjet', 'ryanair', 'delta', 'united', 'american', 'southwest', 'lufthansa', 'emirates', 'british'];
|
||||
const MILITARY_CLASSES = ['H1M', 'H2M', 'L1M', 'L2M', 'L4M', 'A0'];
|
||||
|
||||
const noRecord = [];
|
||||
const history = new Map();
|
||||
const milRanges = [];
|
||||
let db;
|
||||
@@ -71,7 +72,7 @@ export async function initAircraftDb() {
|
||||
db = new Database(DB_PATH);
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS aircraft (
|
||||
icao24 TEXT PRIMARY KEY,
|
||||
icao TEXT PRIMARY KEY,
|
||||
built TEXT,
|
||||
categoryDescription TEXT,
|
||||
country TEXT,
|
||||
@@ -104,7 +105,7 @@ export async function initAircraftDb() {
|
||||
const csv = fromCsv(text, true);
|
||||
const insert = db.prepare(`
|
||||
INSERT OR REPLACE INTO aircraft VALUES (
|
||||
@icao24, @built, @categoryDescription, @country, @engines,
|
||||
@icao, @built, @categoryDescription, @country, @engines,
|
||||
@class, @manufacturer, @model, @modes, @operator,
|
||||
@operatorCallsign, @owner, @registration, @serialNumber, @aircraft
|
||||
)
|
||||
@@ -115,7 +116,7 @@ export async function initAircraftDb() {
|
||||
});
|
||||
|
||||
insertMany(csv.map(row => ({
|
||||
icao24: (row.icao24 || '').toUpperCase(),
|
||||
icao: (row.icao || '').toUpperCase(),
|
||||
built: row.built || null,
|
||||
categoryDescription: row.categoryDescription || null,
|
||||
country: row.country || null,
|
||||
@@ -162,7 +163,7 @@ export function classifyAircraft(row) {
|
||||
const ownerFields = [row.owner];
|
||||
const allFields = [...operatorFields, ...ownerFields, row.categoryDescription];
|
||||
|
||||
if (isIcaoInMilitaryRange(row.icao24) || MILITARY_CLASSES.includes(row.class) || matchesAny(allFields, MILITARY_OPERATORS)) return 'military';
|
||||
if (isIcaoInMilitaryRange(row.icao) || MILITARY_CLASSES.includes(row.class) || matchesAny(allFields, MILITARY_OPERATORS)) return 'military';
|
||||
if (row.categoryDescription?.toLowerCase().includes('cargo') || matchesAny(operatorFields, CARGO_OPERATORS)) return 'cargo';
|
||||
if (matchesAny(operatorFields, PASSENGER_OPERATORS)) return 'passenger';
|
||||
if (row.owner && !row.operator) return 'private';
|
||||
@@ -228,10 +229,11 @@ export async function enrichAircraft(a) {
|
||||
const icao = a.hex.toUpperCase();
|
||||
|
||||
// 1. Check own DB first
|
||||
const row = db.prepare('SELECT * FROM aircraft WHERE icao24 = ?').get(icao);
|
||||
const row = db.prepare('SELECT * FROM aircraft WHERE icao = ?').get(icao);
|
||||
if(row?.manufacturer && row?.model) return { ...a, ...row, type: classifyAircraft(row) };
|
||||
|
||||
// 2. Race the two external sources
|
||||
if(noRecord.includes(icao)) return { icao, ...a, type: 'unknown' };
|
||||
let found = await Promise.any([
|
||||
fetchHexDb(icao).then(resp => {
|
||||
console.log(`source1: ${icao}`, resp);
|
||||
@@ -245,6 +247,10 @@ export async function enrichAircraft(a) {
|
||||
|
||||
// Merge with any partial DB row we already have
|
||||
const merged = { ...row, ...found };
|
||||
if(!merged.aircraft) {
|
||||
noRecord.push(icao);
|
||||
return { icao, ...a, type: 'unknown' };
|
||||
}
|
||||
|
||||
// 3. Backfill manufacturer/model/engines from similar aircraft type in DB
|
||||
if (merged.aircraft && (!merged.manufacturer || !merged.model)) {
|
||||
@@ -257,9 +263,9 @@ export async function enrichAircraft(a) {
|
||||
// 4. Save back to DB
|
||||
if (found) {
|
||||
db.prepare(`
|
||||
INSERT INTO aircraft (icao24, registration, manufacturer, aircraft, model, operator, country, serialNumber, engines, categoryDescription, class)
|
||||
VALUES (@icao24, @registration, @manufacturer, @aircraft, @model, @operator, @country, @serialNumber, @engines, @categoryDescription, @class)
|
||||
ON CONFLICT(icao24) DO UPDATE SET
|
||||
INSERT INTO aircraft (icao, registration, manufacturer, aircraft, model, operator, country, serialNumber, engines, categoryDescription, class)
|
||||
VALUES (@icao, @registration, @manufacturer, @aircraft, @model, @operator, @country, @serialNumber, @engines, @categoryDescription, @class)
|
||||
ON CONFLICT(icao) DO UPDATE SET
|
||||
registration = COALESCE(excluded.registration, registration),
|
||||
manufacturer = COALESCE(excluded.manufacturer, manufacturer),
|
||||
aircraft = COALESCE(excluded.aircraft, aircraft),
|
||||
@@ -271,7 +277,7 @@ export async function enrichAircraft(a) {
|
||||
categoryDescription = COALESCE(excluded.categoryDescription, categoryDescription),
|
||||
class = COALESCE(excluded.class, class)
|
||||
`).run({
|
||||
icao24: icao,
|
||||
icao: icao,
|
||||
registration: merged.registration || null,
|
||||
manufacturer: merged.manufacturer || null,
|
||||
aircraft: merged.aircraft || null,
|
||||
@@ -285,8 +291,7 @@ export async function enrichAircraft(a) {
|
||||
});
|
||||
}
|
||||
|
||||
const result = { icao24: icao, ...merged };
|
||||
return { ...a, ...result, type: classifyAircraft(result) };
|
||||
return { icao, ...a, ...merged, type: classifyAircraft(result) };
|
||||
}
|
||||
|
||||
export async function getADSB() {
|
||||
|
||||
Reference in New Issue
Block a user