From ac1618ced46a896f6ad5011a38b2b27c03c44feb Mon Sep 17 00:00:00 2001 From: Pagwin Date: Sat, 17 Feb 2024 23:05:03 -0500 Subject: [PATCH] made some more changes to make it more functional api --- README.md | 10 ++++++++++ src/google.rs | 2 +- src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++------- src/path_calc.rs | 6 +++--- src/route.rs | 2 +- src/stop.rs | 2 +- 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6298ec5 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# BinghamtonBetterBus +Currently just an api to get the best bus route from point a to point b, currently building frontend + +## TODO: + +- Build frontend +- Change api to give a not shitty poly line for Broome county buses +- Test for Broome county buses (can't at night due to being too late) +- use a DB via SeaORM to avoid spaming APIs +- use info in db to try and predict bus schedules in future diff --git a/src/google.rs b/src/google.rs index 03dc631..c1b06c8 100644 --- a/src/google.rs +++ b/src/google.rs @@ -57,7 +57,7 @@ fn build_route_body_pain(path:&[crate::types::Coords])->serde_json::Value{ let time = chrono::offset::Utc::now() .to_rfc3339_opts(chrono::SecondsFormat::Nanos,true); let inter = &path[1..path.len()-1]; - let mut val = serde_json::json!({ + let val = serde_json::json!({ "origin":{ "location": path[0], "sideOfRoad": true diff --git a/src/main.rs b/src/main.rs index 121025b..eca7e20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,6 @@ mod google; mod types; mod path_calc; -use tide::prelude::*; - #[tokio::main] async fn main() -> anyhow::Result<()> { let mut app = tide::with_state(state::State::new().await?); @@ -28,7 +26,7 @@ async fn main() -> anyhow::Result<()> { } -/// return the best path of buses with the request specifying ISO time, lat and longitude in query params +/// return the best path of buses with the request specifying ISO time, lat and longitude in query params, only gives routes at the moment and no enter or exit stops /// Example return /// { /// "path": [ @@ -41,17 +39,55 @@ async fn main() -> anyhow::Result<()> { /// ... /// ] /// } -async fn transit_path(mut req: tide::Request)->tide::Result{ +async fn transit_path(req: tide::Request)->tide::Result{ + let query = req.query::()?; + let _time = chrono::DateTime::parse_from_rfc3339(&query.time); + let from = types::Coords {lat:query.from_lat, lon:query.from_lon}; + let to = types::Coords {lat:query.to_lat, lon:query.to_lon}; + let mut routes = route::occt_routes_get().await?; + routes.append(&mut route::broome_county_routes_get().await?); + let route = path_calc::calc_route(&mut routes,from,to).await?; - Ok("".into()) + let ret = TransitPathResult{ + path: route.into_iter().map(RetRoute::from).collect() + }; + + Ok(serde_json::to_string(&ret)?.into()) +} +#[derive(serde::Deserialize)] +struct TransitPathQuery{ + time:String, + from_lat:f64, + from_lon:f64, + to_lat:f64, + to_lon:f64, + +} +#[derive(serde::Serialize)] +struct TransitPathResult{ + path:Vec +} +#[derive(serde::Serialize)] +struct RetRoute{ + route_name:String, + //enter_stop_id:u32, + //exit_stop_id:u32, + //enter_stop_coords: types::Coords +} +impl From for RetRoute{ + fn from(route: route::Route) -> Self { + Self{ + route_name: route.name, + } + } } /// return the num of estimated seconds for stop ids given in query params, also need to specify /// service and route id /// example return: /// 23 -async fn transit_estimate(mut req: tide::Request)->tide::Result{ +async fn transit_estimate(req: tide::Request)->tide::Result{ let query = req.query::()?; let route = match query.service.as_str(){ "OCCT"=>{ @@ -85,7 +121,7 @@ struct TransitEstimateQuery{ /// return the coord pairs as json and line color forgiven route id in query params /// Example return: /// {"color":"#0000ff", "stops":[{"lat":-75.5,"lon":23},...]} -async fn bus_route_draw(mut req: tide::Request)->tide::Result{ +async fn bus_route_draw(req: tide::Request)->tide::Result{ let query = req.query::()?; let route = match query.service.as_str() { "OCCT" => { diff --git a/src/path_calc.rs b/src/path_calc.rs index 176ad9b..56c0c1e 100644 --- a/src/path_calc.rs +++ b/src/path_calc.rs @@ -22,9 +22,9 @@ fn sort_routes_by_measureu64+Clone>(measure: F, rout const BU: crate::types::Coords = crate::types::Coords{lat:42.08707019482314, lon:-75.96706048564714}; const BC_BUS_HUB: crate::types::Coords = crate::types::Coords{lat:42.10156942719183, lon:-75.91073683134701}; // return a list indicating the bus routes to take in order -fn calc_routes(routes:&mut [crate::route::Route], from:crate::types::Coords, to: crate::types::Coords)->Vec{ +pub async fn calc_route(routes:&mut [crate::route::Route], from:crate::types::Coords, to: crate::types::Coords)->anyhow::Result>{ if routes.len() == 0 { - return Vec::new() + return Ok(Vec::new()) } let measure_distance_to = walk_distance_measure(to); @@ -54,7 +54,7 @@ fn calc_routes(routes:&mut [crate::route::Route], from:crate::types::Coords, to: let bu_route_cost = route_measure_dist_from(min_to_bu_route) + route_measure_dist_to(min_from_bu_route); let bc_route_cost = route_measure_dist_from(min_to_bc_hub) + route_measure_dist_from(min_from_bc_hub); - select_min(vec![(direct_route_cost,vec![min_direct_route.clone()]), (bu_route_cost,vec![min_to_bu_route.clone(),min_from_bu_route.clone()]), (bc_route_cost, vec![min_to_bc_hub.clone(), min_from_bc_hub.clone()])]) + Ok(select_min(vec![(direct_route_cost,vec![min_direct_route.clone()]), (bu_route_cost,vec![min_to_bu_route.clone(),min_from_bu_route.clone()]), (bc_route_cost, vec![min_to_bc_hub.clone(), min_from_bc_hub.clone()])])) } fn walk_distance_measure(pos: crate::types::Coords)->impl Fn(&&crate::stop::Stop)->u64+Clone{ diff --git a/src/route.rs b/src/route.rs index 9c18462..385ddf6 100644 --- a/src/route.rs +++ b/src/route.rs @@ -74,7 +74,7 @@ pub async fn broome_county_routes_get()->anyhow::Result>{ let resp = reqwest::get("https://bctransit.doublemap.com/map/v2/routes").await?; let body = resp.text().await?; let routes:Vec = serde_json::from_str(body.as_str())?; - let stops = crate::stop::BC_stops_get().await?; + let stops = crate::stop::bc_stops_get().await?; let routes = routes.into_iter().map(|route|{ let stops:Vec = stops.iter() .filter(|stop|{ diff --git a/src/stop.rs b/src/stop.rs index 309c0a5..3a29b74 100644 --- a/src/stop.rs +++ b/src/stop.rs @@ -55,7 +55,7 @@ impl From for Stop{ pub async fn occt_stops_get()->anyhow::Result>{ Ok(Vec::new()) } -pub async fn BC_stops_get()->anyhow::Result>{ +pub async fn bc_stops_get()->anyhow::Result>{ Ok(Vec::new()) } pub fn poly_encode_stops>(stops:I)->String{