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