Files
adsb-index/src/lib/get-data.mjs

64 lines
2.1 KiB
JavaScript

import fetch from './fetch.mjs';
const formatNumber = str => str && Number(`${str}`.replace(/[^0-9.-]/, ''));
const formatString = str => {
if (str == null) return 'N/A';
if (typeof str !== 'string') return str;
return str && str.length && str !== 'undefined' ? str.trim() : 'N/A';
};
export async function getOpenskyData() {
const positionSourceMap = ['ADS-B', 'ASTERIX', 'MLAT'];
const res = await fetch.get(`https://opensky-network.org/api/states/all`);
return res.data.states.map(rec => ({
transponder: `${rec[0]}`.toLowerCase(),
callsign: `${rec[1]}`.trim(),
origin_country: rec[2],
time_position: new Date(rec[3] * 1000),
last_contact: new Date(rec[4] * 1000),
location: rec[5] && rec[6] ? `${rec[6]},${rec[5]}` : null,
lat: rec[6],
lon: rec[5],
baro_altitude: rec[7],
geo_altitude: rec[13],
on_ground: rec[8],
velocity: rec[9],
vertical_rate: rec[11],
squawk: rec[14],
spi: rec[15],
position_source: positionSourceMap[rec[16]],
}));
}
export async function getAdbsExchangeData({ lat, lon, radius }) {
const positionSourceMap = ['Unknown', 'Mode-S', 'ADS-B', 'ADS-B', 'ADS-B', 'ADS-B'];
const res = await fetch.get(
`http://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?lat=${lat}&lng=${lon}&fDstL=0&fDstU=${radius}`
);
return res.data.acList.map(rec => ({
transponder: formatString(`${rec.Icao}`.toLowerCase()),
callsign: formatString(rec.Call),
origin_country: rec.Cou,
time_position: new Date(rec.PosTime),
last_contact: new Date(rec.PosTime),
location: rec.Lat && rec.Long ? `${rec.Lat},${rec.Long}` : null,
lat: rec.Lat,
lon: rec.Long,
baro_altitude: formatNumber(rec.Alt),
geo_altitude: formatNumber(rec.GAlt),
on_ground: rec.Gnd,
velocity: formatNumber(rec.Spd),
vertical_rate: formatNumber(rec.Vsi),
squawk: rec.Sqk,
spi: false,
position_source: positionSourceMap[rec.Trt],
operator: formatString(rec.Op),
aircraft: formatString(rec.Mdl),
aircraft_manufacturer: formatString(rec.Man),
from: formatString(rec.From),
to: formatString(rec.To),
}));
}