use warp::{http::Response, Filter}; use adsb::ICAOAddress; use beast::aircrafts::Entry; use serde::Serialize; #[derive(Serialize)] pub struct Aircraft { hex: String, flight: Option, category: Option, lat: Option, lon: Option, /// ft altitude: Option, track: Option, /// kts speed: Option, } impl Aircraft { pub fn new(icao_address: &ICAOAddress, entry: &Entry) -> Self { let pos = entry.location(); Aircraft { hex: format!("{}", icao_address), flight: entry.callsign.clone(), category: entry.emitter_category.clone(), lat: pos.as_ref().map(|pos| pos.latitude), lon: pos.as_ref().map(|pos| pos.longitude), altitude: entry.altitude.clone(), track: entry.heading.map(|heading| heading as i16), speed: entry.ground_speed.map(|speed| speed as u16), } } } #[tokio::main] async fn main() { let aircrafts = beast::aircrafts::Aircrafts::new(); aircrafts.connect("radiobert.serv.zentralwerk.org", 30005); let hello = warp::path!("data.json") .map(move || { let data = aircrafts.read() .iter() .map(|(icao_address, entry)| Aircraft::new(icao_address, &entry.read().unwrap())) .collect::>(); let json = serde_json::ser::to_vec_pretty(&data) .unwrap_or_else(|_| vec![]); Response::builder() .header("Content-Type", "application/json") .body(json) }); warp::serve(hello) .run(([0, 0, 0, 0], 8080)) .await; }