Skip to content
Snippets Groups Projects
Commit 345c694e authored by Nicolò Caruso's avatar Nicolò Caruso
Browse files

[fr-api] Added response deserialization

Added response deserialization from the flight radar API response.
Also added the creation of the ROCKET_STATS_TM and ROCKET_FLIGHT_TM packets to be later send on to track the aircraft.
parent 457a9d08
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,34 @@ use reqwest::{header, redirect::Policy}; ...@@ -4,6 +4,34 @@ use reqwest::{header, redirect::Policy};
use log::{error, info}; use log::{error, info};
use serde::Deserialize;
use skyward_mavlink::lyra::{MavMessage, ROCKET_FLIGHT_TM_DATA, ROCKET_STATS_TM_DATA};
use crate::packet::TimedMessage;
// The message structure from the FR-API
#[derive(Debug, Deserialize)]
struct Message{
message: String,
details: String,
data: String
}
// The sub-message with the information about the aircraft position
#[derive(Debug, Deserialize)]
struct LightTelemetryMessage{
fr24_id: String,
hex: String,
callsign: String,
lat: f32,
lon: f32,
track: i8, // Heading 0-360 [deg]
alt:f32,
gspeed: i32, // Ground speed in knots
vspeed: i32, // Vertical speed in feet
squawk: String,
timestamp: String,
source: String
}
pub async fn api_request(flight_nr: String) -> () { pub async fn api_request(flight_nr: String) -> () {
// TODO: Set your token // TODO: Set your token
let token: String = String::from("YOUR API TOKEN"); let token: String = String::from("YOUR API TOKEN");
...@@ -52,6 +80,46 @@ pub async fn api_request(flight_nr: String) -> () { ...@@ -52,6 +80,46 @@ pub async fn api_request(flight_nr: String) -> () {
Ok(string) => { Ok(string) => {
print!("{}", string); print!("{}", string);
info!("Got response {}", string); info!("Got response {}", string);
let msg_payload : Result<Message, serde_json::Error> = serde_json::from_str(&string);
match msg_payload {
Ok(payload) =>{
println!("Message received is: {:?}\n", payload);
println!("Singular fields: msg: {:?} dts: {:?} data:{:?}\n", payload.message, payload.details, payload.data);
let flight_data: Result<LightTelemetryMessage, serde_json::Error> = serde_json::from_str(&payload.data);
match flight_data {
Ok(data) =>{
let mut packets = Vec::new();
// Send the current GPS as origin for the NAS
let origin_data = ROCKET_STATS_TM_DATA {
ref_lat: data.lat as f32,
ref_lon: data.lon as f32,
ref_alt: 0 as f32,
..Default::default()
};
let mav_msg = MavMessage::ROCKET_STATS_TM(origin_data);
packets.push(TimedMessage::new(0, mav_msg));
// Send as NAS the Down from altitude, NE=0 and speed considering the heading
let nas_data = ROCKET_FLIGHT_TM_DATA{
nas_n: 0 as f32,
nas_e: 0 as f32,
nas_d: -data.alt as f32,
nas_vn: (data.track as f32).cos()*(data.gspeed as f32)/2 as f32, // From heading and knots to m/s
nas_ve: (data.track as f32).sin()*(data.gspeed as f32)/2 as f32, // From heading and knots to m/s
nas_vd: -60.0*(data.vspeed as f32)/3.2808399 as f32, // Converting feet/min to m/s
..Default::default()
};
let mav_msg = MavMessage::ROCKET_FLIGHT_TM(nas_data);
packets.push(TimedMessage::new(0, mav_msg));
}
Err(err)=>{error!("An error occured deserializing the data{}", err)}
}
}
Err(err) =>{error!("An error occurred deserializing the message {}", err)}
}
} }
Err(err) => { Err(err) => {
error!("An error occurred in the message {}", err) error!("An error occurred in the message {}", err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment