Added live metar data updates

This commit is contained in:
2023-09-10 18:42:26 -04:00
parent 4c6bee686c
commit bcda1bbf3f
8 changed files with 76 additions and 63 deletions

View File

@@ -3,6 +3,7 @@ use crate::error_handler::CustomError;
use crate::schema::airports;
use diesel::prelude::*;
use postgis_diesel::types::*;
use postgis_diesel::functions::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
@@ -39,24 +40,12 @@ pub struct Airports {
pub point: Point
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Bounds {
pub north_east: LatLng,
pub south_west: LatLng,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LatLng {
pub lat: f32,
pub lon: f32
}
impl Airports {
pub fn find_all(bounds: Bounds, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
pub fn find_all(bounds: Polygon<Point>, limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
let mut conn = db::connection()?;
let airports = airports::table
.limit(limit as i64)
.filter(airports::id.gt(page * limit))
.filter(airports::id.gt(page * limit).and(st_contains(bounds, airports::point)))
.load::<Airports>(&mut conn)?;
Ok(airports)
}

View File

@@ -1,19 +1,26 @@
use crate::{airports::{Airport, Airports, Bounds, LatLng}, db};
use crate::{airports::{Airport, Airports}, db};
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest};
use log::error;
use postgis_diesel::types::{Polygon, Point};
use serde::{Serialize, Deserialize};
use serde_json::json;
#[derive(Debug, Serialize, Deserialize)]
struct FindAllParams {
ne_lat: f32,
ne_lon: f32,
sw_lat: f32,
sw_lon: f32,
ne_lat: f64,
ne_lon: f64,
sw_lat: f64,
sw_lon: f64,
limit: i32,
page: i32
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct Coordinate {
lon: f64,
lat: f64
}
#[get("/setup")]
async fn setup() -> HttpResponse {
db::import_data();
@@ -23,11 +30,13 @@ async fn setup() -> HttpResponse {
#[get("/airports")]
async fn find_all(req: HttpRequest) -> HttpResponse {
let params = web::Query::<FindAllParams>::from_query(req.query_string()).unwrap();
let bounds = Bounds {
north_east: LatLng { lat: params.ne_lat, lon: params.ne_lon },
south_west: LatLng { lat: params.sw_lat, lon: params.sw_lon }
};
match web::block(move || Airports::find_all(bounds, params.limit, params.page)).await.unwrap() {
let mut polygon: Polygon<Point> = Polygon::new(Some(4326));
polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.ne_lon, y: params.sw_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.ne_lon, y: params.ne_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.sw_lon, y: params.ne_lat, srid: Some(4326) });
polygon.add_point(Point { x: params.sw_lon, y: params.sw_lat, srid: Some(4326) });
match web::block(move || Airports::find_all(polygon, params.limit, params.page)).await.unwrap() {
Ok(a) => HttpResponse::Ok().json(a),
Err(err) => {
error!("{}", err);

View File

@@ -14,18 +14,17 @@ pub struct QualityControlFlags {
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
#[diesel(table_name = metars)]
pub struct Metar {
pub icao: String,
pub raw_text: String,
pub station_id: String,
pub observation_time: String,
pub latitude: f64,
pub longitude: f64,
pub temp_c: f64,
pub dewpoint_c: f64,
pub wind_dir_degrees: i32,
pub wind_speed_kt: i32,
pub visibility_statute_mi: String,
pub altim_in_hg: f64,
pub temp_c: Option<f64>,
pub dewpoint_c: Option<f64>,
pub wind_dir_degrees: Option<String>,
pub wind_speed_kt: Option<i32>,
pub visibility_statute_mi: Option<String>,
pub altim_in_hg: Option<f64>,
pub sea_level_pressure_mb: Option<f64>,
// pub quality_control_flags: Option<QualityControlFlags>,
pub wx_string: Option<String>,
@@ -35,7 +34,7 @@ pub struct Metar {
pub metar_type: String,
#[serde(rename = "maxT_c")]
pub max_t_c: Option<f64>,
#[serde(rename = "minT_c")]
#[serde(rename = " ")]
pub min_t_c: Option<f64>,
pub precip_in: Option<f64>,
pub elevation_m: i32
@@ -43,19 +42,17 @@ pub struct Metar {
#[derive(Serialize, Deserialize, Queryable)]
pub struct Metars {
// pub id: i32,
// pub icao: String,
pub raw_text: String,
pub station_id: String,
pub observation_time: String,
pub latitude: f64,
pub longitude: f64,
pub temp_c: f64,
pub dewpoint_c: f64,
pub wind_dir_degrees: i32,
pub wind_speed_kt: i32,
pub visibility_statute_mi: String,
pub altim_in_hg: f64,
pub temp_c: Option<f64>,
pub dewpoint_c: Option<f64>,
pub wind_dir_degrees: Option<String>,
pub wind_speed_kt: Option<i32>,
pub visibility_statute_mi: Option<String>,
pub altim_in_hg: Option<f64>,
pub sea_level_pressure_mb: Option<f64>,
pub quality_control_flags: Option<QualityControlFlags>,
pub wx_string: Option<String>,
@@ -119,8 +116,10 @@ impl Metars {
let metar_bytes = Metars::read_to_end_into_buffer(&mut reader, &e, &mut junk_buf).unwrap();
let str = std::str::from_utf8(&metar_bytes).unwrap();
let mut deserializer = Deserializer::from_str(str);
let metar = Metars::deserialize(&mut deserializer).unwrap();
metars.push(metar);
match Metars::deserialize(&mut deserializer) {
Ok(m) => metars.push(m),
Err(err) => warn!("{}", err)
};
},
_ => ()
}

View File

@@ -27,12 +27,12 @@ diesel::table! {
observation_time -> Text,
latitude -> Double,
longitude -> Double,
temp_c -> Double,
dewpoint_c -> Double,
wind_dir_degrees -> Integer,
wind_speed_kt -> Integer,
visibility_statute_mi -> Text,
altim_in_hg -> Double,
temp_c -> Nullable<Double>,
dewpoint_c -> Nullable<Double>,
wind_dir_degrees -> Nullable<Text>,
wind_speed_kt -> Nullable<Integer>,
visibility_statute_mi -> Nullable<Text>,
altim_in_hg -> Nullable<Double>,
sea_level_pressure_mb -> Nullable<Double>,
wx_string -> Nullable<Text>,
flight_category -> Text,