opensky_network/
tracks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Module for handling flight tracks by aircraft.
use std::{
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use log::{debug, warn};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};

use crate::errors::Error;

#[derive(Debug, Serialize, Deserialize)]
/// Represents the trajectory for a certain aircraft at a given time.
pub struct FlightTrack {
    /// Unique ICAO 24-bit address of the transponder in lower case hex string
    /// representation.
    pub icao24: String,
    #[serde(alias = "startTime")]
    /// Time of the first waypoint in seconds since epoch (Unix time).
    pub start_time: f64,
    #[serde(alias = "endTime")]
    /// Time of the last waypoint in seconds since epoch (Unix time).
    pub end_time: f64,
    /// Callsign (8 characters) that holds for the whole track.
    pub callsign: Option<String>,
    /// Waypoints of the trajectory
    pub path: Vec<Waypoint>,
}

#[derive(Debug, Serialize)]
/// Represents the single waypoint that is a basic part of flight trajectory.
pub struct Waypoint {
    /// Time which the given waypoint is associated with in seconds since epoch
    /// (Unix time).
    pub time: u64,
    /// WGS-84 latitude in decimal degrees.
    pub latitude: Option<f64>,
    /// WGS-84 longitude in decimal degrees.
    pub longitude: Option<f64>,
    /// Barometric altitude in meters.
    pub baro_altitude: Option<f64>,
    /// True track in decimal degrees clockwise from north (north=0°). Can be
    /// None.
    pub true_track: Option<f64>,
    /// Boolean value which indicates if the position was retrieved from a
    /// surface position report.
    pub on_ground: bool,
}

impl<'de> Deserialize<'de> for Waypoint {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let values = Deserialize::deserialize(deserializer)?;
        match values {
            Value::Array(arr) => Ok(Waypoint::from(arr)),
            Value::Object(obj) => Ok(Waypoint::from(obj)),
            _ => Err(serde::de::Error::custom("expected array")),
        }
    }
}

impl From<Vec<Value>> for Waypoint {
    fn from(value: Vec<Value>) -> Self {
        Waypoint {
            time: value[0].as_u64().unwrap(),
            latitude: value[1].as_f64(),
            longitude: value[2].as_f64(),
            baro_altitude: value[3].as_f64(),
            true_track: value[4].as_f64(),
            on_ground: value[5].as_bool().unwrap(),
        }
    }
}

impl From<Map<String, Value>> for Waypoint {
    fn from(value: Map<String, Value>) -> Self {
        Waypoint {
            time: value["time"].as_u64().unwrap(),
            latitude: value["latitude"].as_f64(),
            longitude: value["longitude"].as_f64(),
            baro_altitude: value["baro_altitude"].as_f64(),
            true_track: value["true_track"].as_f64(),
            on_ground: value["on_ground"].as_bool().unwrap(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct TrackRequest {
    login: Option<Arc<(String, String)>>,
    icao24: String,
    time: u64,
}

impl TrackRequest {
    pub async fn send(&self) -> Result<FlightTrack, Error> {
        let login_part = if let Some(login) = &self.login {
            format!("{}:{}@", login.0, login.1)
        } else {
            String::new()
        };

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        if self.time != 0 && now - self.time > 30 * 24 * 60 * 60 {
            warn!(
                "Interval ({} secs) is larger than limits ({} secs)",
                now - self.time,
                30 * 24 * 60 * 60
            );
        }

        let url = format!(
            "https://{}opensky-network.org/api/tracks/all?icao24={}&time={}",
            login_part, self.icao24, self.time
        );

        debug!("url = {}", url);

        let res = reqwest::get(url).await?;

        match res.status() {
            reqwest::StatusCode::OK => {
                let bytes = res.bytes().await?.to_vec();

                let result: FlightTrack = match serde_json::from_slice(&bytes) {
                    Ok(result) => result,
                    Err(e) => {
                        return Err(Error::InvalidJson(e));
                    }
                };

                Ok(result)
            }
            status => Err(Error::Http(status)),
        }
    }
}

pub struct TrackRequestBuilder {
    inner: TrackRequest,
}

impl TrackRequestBuilder {
    pub fn new(login: Option<Arc<(String, String)>>, icao24: String) -> Self {
        Self {
            inner: TrackRequest {
                login,
                icao24,
                time: 0,
            },
        }
    }

    pub fn at_time(&mut self, time: u64) -> &mut Self {
        self.inner.time = time;

        self
    }

    pub async fn send(self) -> Result<FlightTrack, Error> {
        self.inner.send().await
    }
}