📦 Turbo87 / united-flarmnet

📄 ogn.rs · 58 lines
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
58use reqwest_middleware::ClientWithMiddleware;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct DeviceDatabase {
    pub devices: Vec<Device>,
}

/// ```json
/// {
///     "device_type": "F",
///     "device_id": "000000",
///     "aircraft_model": "HPH 304CZ-17",
///     "registration": "OK-7777",
///     "cn": "KN",
///     "tracked": "Y",
///     "identified": "Y",
///     "aircraft_type": "1"
/// }
/// ```
#[derive(Debug, Deserialize)]
pub struct Device {
    // pub device_type: String,
    pub device_id: String,
    pub aircraft_model: String,
    pub registration: String,
    pub cn: String,
    // pub tracked: String,
    // pub identified: String,
    // pub aircraft_type: String,
}

impl Device {
    pub fn into_flarmnet_record(self) -> flarmnet::Record {
        flarmnet::Record {
            flarm_id: self.device_id,
            pilot_name: "".to_string(),
            airfield: "".to_string(),
            plane_type: self.aircraft_model,
            registration: self.registration,
            call_sign: self.cn,
            frequency: "".to_string(),
        }
    }
}

#[instrument(skip(client))]
pub async fn get_ddb(client: &ClientWithMiddleware) -> anyhow::Result<Vec<Device>> {
    info!("Downloading OGN DDB…");
    let response = client
        .get("http://ddb.glidernet.org/download/?j=1&t=1")
        .send()
        .await?;
    let response = response.error_for_status()?;
    let ogn_ddb: DeviceDatabase = response.json().await?;
    Ok(ogn_ddb.devices)
}