feat(flight-live): follow booked flights live from keyless ADS-B feeds

A new flight-live domain polls the open feeds adsb.lol and adsb.fi (no key,
no cost; adsb.fi only on self-hosted installs, its terms are non-commercial)
for every booked flight leg in its watch window, derives phase, take-off,
landing, delay and an arrival estimate, and pushes changes as flight:live.
GET /api/trips/:tripId/flights/live, the MCP tool get_trip_flight_status,
notifications for take-off, landing and delays, and the admin switch
flight_live_enabled (FLIGHT_LIVE_ENABLED wins) come with it.
This commit is contained in:
Maurice
2026-11-13 15:29:03 +01:00
parent 3365511b10
commit 154563f34d
143 changed files with 5048 additions and 14 deletions
@@ -0,0 +1,51 @@
import { flightLiveLegSchema, tripFlightLiveResponseSchema } from './flight-live.schema';
import { describe, it, expect } from 'vitest';
const leg = {
reservation_id: 6,
leg_index: 0,
callsign: 'DLH400',
flight_number: 'LH 400',
service_date: '2026-06-11',
origin: { code: 'FRA', name: 'Frankfurt', lat: 50.03, lng: 8.57, timezone: 'Europe/Berlin' },
destination: { code: 'JFK', name: 'New York JFK', lat: 40.64, lng: -73.78, timezone: 'America/New_York' },
phase: 'scheduled',
scheduled_departure: '2026-06-11T11:50:00.000Z',
scheduled_arrival: '2026-06-11T20:35:00.000Z',
departed_at: null,
landed_at: null,
estimated_arrival: null,
departure_delay_min: null,
arrival_delay_min: null,
progress: null,
distance_total_km: 6200,
distance_remaining_km: null,
position: null,
aircraft: null,
last_seen_at: null,
fetched_at: null,
stale: false,
source: null,
};
describe('flightLiveLegSchema', () => {
it('accepts a leg that has not shown up yet', () => {
expect(flightLiveLegSchema.parse(leg).phase).toBe('scheduled');
});
it('rejects a progress outside 0 to 1', () => {
expect(flightLiveLegSchema.safeParse({ ...leg, progress: 1.2 }).success).toBe(false);
});
it('rejects an unknown phase', () => {
expect(flightLiveLegSchema.safeParse({ ...leg, phase: 'boarding' }).success).toBe(false);
});
});
describe('tripFlightLiveResponseSchema', () => {
it('carries the switch, the credit line and the legs', () => {
const parsed = tripFlightLiveResponseSchema.parse({ enabled: true, attribution: 'adsb.lol', flights: [leg] });
expect(parsed.flights).toHaveLength(1);
});
});
@@ -0,0 +1,105 @@
import { z } from 'zod';
/**
* Live state of a booked flight leg, read from open ADS-B feeds without any key
* (adsb.lol, adsb.fi). These feeds only know where an aircraft is, so everything
* an airline would publish (gate, terminal, belt, cancellation) is out of reach;
* the delays and the arrival estimate are worked out from the positions and are
* marked as estimates in the UI.
*
* Times cross the wire as ISO 8601 UTC strings. A leg is addressed by its
* reservation and its index in the leg chain (client/src/utils/flightLegs.ts),
* so a multi-leg booking gets one state per segment.
*/
/**
* - scheduled: departure is still ahead and the aircraft has not shown up.
* - taxiing: the aircraft is seen on the ground at the departure airport.
* - airborne: in the air.
* - landed: seen on the ground at, or lost low over, the destination.
* - unknown: inside the watch window but the callsign never showed up (an
* airline flying under another callsign, a codeshare, no coverage).
*/
export const flightLivePhaseSchema = z.enum(['scheduled', 'taxiing', 'airborne', 'landed', 'unknown']);
export type FlightLivePhase = z.infer<typeof flightLivePhaseSchema>;
export const flightLiveSourceSchema = z.enum(['adsb.lol', 'adsb.fi']);
export type FlightLiveSource = z.infer<typeof flightLiveSourceSchema>;
export const flightLivePositionSchema = z.object({
lat: z.number(),
lng: z.number(),
altitude_ft: z.number().nullable(),
ground_speed_kt: z.number().nullable(),
/** Degrees clockwise from north, for turning the aircraft icon. */
track_deg: z.number().nullable(),
vertical_rate_fpm: z.number().nullable(),
on_ground: z.boolean(),
});
export type FlightLivePosition = z.infer<typeof flightLivePositionSchema>;
export const flightLiveAirportSchema = z.object({
/** IATA code, e.g. "FRA". */
code: z.string(),
name: z.string().nullable(),
lat: z.number(),
lng: z.number(),
/** IANA zone the schedule is local to, e.g. "Europe/Berlin". */
timezone: z.string().nullable(),
});
export type FlightLiveAirport = z.infer<typeof flightLiveAirportSchema>;
export const flightLiveLegSchema = z.object({
reservation_id: z.number().int(),
leg_index: z.number().int().min(0),
/** ICAO callsign as transmitted, e.g. "DLH400". */
callsign: z.string(),
/** The booked flight number as the user knows it, e.g. "LH 400". */
flight_number: z.string(),
/** Local departure date of the leg, YYYY-MM-DD. */
service_date: z.string(),
/** Both ends of the leg, so the client can show local times and draw the arc. */
origin: flightLiveAirportSchema,
destination: flightLiveAirportSchema,
phase: flightLivePhaseSchema,
scheduled_departure: z.string().nullable(),
scheduled_arrival: z.string().nullable(),
/** Observed wheels-up, or null while unknown. */
departed_at: z.string().nullable(),
/** Observed touchdown, or null while unknown. */
landed_at: z.string().nullable(),
/** Arrival estimate from remaining distance and ground speed; null on the ground. */
estimated_arrival: z.string().nullable(),
/** Minutes late (negative when early) against the schedule; null until observed or estimated. */
departure_delay_min: z.number().int().nullable(),
arrival_delay_min: z.number().int().nullable(),
/** Share of the great-circle distance flown, 0 to 1. */
progress: z.number().min(0).max(1).nullable(),
distance_total_km: z.number().nullable(),
distance_remaining_km: z.number().nullable(),
position: flightLivePositionSchema.nullable(),
aircraft: z
.object({
registration: z.string().nullable(),
/** ICAO type designator, e.g. "A20N". */
type: z.string().nullable(),
})
.nullable(),
/** When the feed last heard the aircraft. */
last_seen_at: z.string().nullable(),
/** When TREK last asked the feed. */
fetched_at: z.string().nullable(),
/** True when the last position is too old to trust, e.g. over an ocean. */
stale: z.boolean(),
source: flightLiveSourceSchema.nullable(),
});
export type FlightLiveLeg = z.infer<typeof flightLiveLegSchema>;
export const tripFlightLiveResponseSchema = z.object({
/** False when the admin switched live flight data off; the client then hides it. */
enabled: z.boolean(),
/** Credit line the feed's terms ask for, shown next to live data. */
attribution: z.string().nullable(),
flights: z.array(flightLiveLegSchema),
});
export type TripFlightLiveResponse = z.infer<typeof tripFlightLiveResponseSchema>;