opensky_network/
lib.rs

1//! # OpenSky Network API
2//! This is a Rust library for interacting with the OpenSky Network API.
3//! The OpenSky Network is a community-based receiver network which continuously
4//! collects air traffic surveillance data. Unlike other networks, OpenSky keeps
5//! the collected data forever and makes it available to researchers and
6//! developers. The OpenSky Network API provides a way to access the collected
7//! data.
8//!
9//! Please follow [The OpenSky Network API documentation](https://openskynetwork.github.io/opensky-api/) for more information.
10//!
11//! ## Example
12//!
13//! Get the state vectors of aircraft.
14//! ```rust
15//! use opensky_network::OpenSkyApi;
16//! #[tokio::main]
17//! async fn main() {
18//!     let api = OpenSkyApi::new();
19//!     let request = api
20//!         .get_states()
21//!         .at_time(1458564121)
22//!         .with_icao24("3c6444".to_string());
23//!     let result = request.send().await.expect("Failed to get states");
24//!     println!("{:#?}", result);
25//! }
26//! ```
27//!
28//! Get the flight data of aircraft.
29//! ```rust
30//! use opensky_network::OpenSkyApi;
31//! use std::env;
32//! #[tokio::main]
33//! async fn main() {
34//!     dotenv::dotenv().ok();
35//!     // setup OPENSKY_USER and OPENSKY_PASS in .env file
36//!     let username = env::var("OPENSKY_USER").expect("OPENSKY_USER environment variable not set");
37//!     let password = env::var("OPENSKY_PASS").expect("OPENSKY_PASS environment variable not set");
38//!     let api = OpenSkyApi::with_login(username, password);
39//!
40//!     let now = std::time::SystemTime::now()
41//!         .duration_since(std::time::UNIX_EPOCH)
42//!         .unwrap()
43//!         .as_secs();
44//!     let mut request = api.get_flights(now - 7 * 24 * 60 * 60, now);
45//!     request.by_aircraft("8990ed".to_string());
46//!     let result = request.send().await.expect("Failed to get flights");
47//!     println!("{:#?}", result);
48//! }
49//! ```
50use std::sync::Arc;
51
52pub mod bounding_box;
53pub mod errors;
54pub mod flights;
55pub mod states;
56pub mod tracks;
57
58pub use bounding_box::BoundingBox;
59pub use flights::Flight;
60use flights::FlightsRequestBuilder;
61use states::StateRequestBuilder;
62pub use states::{StateVector, States};
63use tracks::TrackRequestBuilder;
64pub use tracks::{FlightTrack, Waypoint};
65
66#[derive(Default)]
67///  The OpenSky Network API <https://openskynetwork.github.io/opensky-api>
68pub struct OpenSkyApi {
69    login: Option<Arc<(String, String)>>,
70}
71
72impl OpenSkyApi {
73    /// Creates a new anonymous OpenSkyApi instance
74    pub fn new() -> Self {
75        Self { login: None }
76    }
77
78    /// Creates a new OpenSkyApi instance with the provided username and
79    /// password
80    pub fn with_login(username: String, password: String) -> Self {
81        Self {
82            login: Some(Arc::new((username, password))),
83        }
84    }
85
86    /// Creates a new StateRequestBuilder which can be used to create
87    /// StateRequests
88    pub fn get_states(&self) -> StateRequestBuilder {
89        StateRequestBuilder::new(self.login.clone())
90    }
91
92    /// Creates a new FlightsRequestBuilder using the given time interval. The
93    /// beginning and ending times are numbers that represent times in
94    /// seconds since the Unix Epoch.
95    ///
96    /// The interval must not span greater than 2 hours, otherwise the request
97    /// will fail.
98    pub fn get_flights(&self, begin: u64, end: u64) -> FlightsRequestBuilder {
99        FlightsRequestBuilder::new(self.login.clone(), begin, end)
100    }
101
102    /// Create a new TrackRequestBuilder for the given icao24 address of a
103    /// certain aircraft.
104    ///
105    /// In contrast to state vectors, trajectories do not contain all
106    /// information we have about the flight, but rather show the aircraft’s
107    /// general movement pattern. For this reason, waypoints are selected
108    /// among available state vectors given the following set of rules:
109    /// * The first point is set immediately after the the aircraft’s expected
110    ///   departure, or after the network received the first position when the
111    ///   aircraft entered its reception range.
112    /// * The last point is set right before the aircraft’s expected arrival, or
113    ///   the aircraft left the networks reception range.
114    /// * There is a waypoint at least every 15 minutes when the aircraft is
115    ///   in-flight.
116    /// * A waypoint is added if the aircraft changes its track more than 2.5°.
117    /// * A waypoint is added if the aircraft changes altitude by more than 100m
118    ///   (~330ft).
119    /// * A waypoint is added if the on-ground state changes.
120    ///
121    /// Tracks are strongly related to flights. Internally, we compute flights
122    /// and tracks within the same processing step. As such, it may be
123    /// beneficial to retrieve a list of flights with the API methods from
124    /// above, and use these results with the give time stamps to retrieve
125    /// detailed track information.
126    pub fn get_tracks(&self, icao24: String) -> TrackRequestBuilder {
127        TrackRequestBuilder::new(self.login.clone(), icao24)
128    }
129}