From dec8ab4334f2ecc18f3e8a186e8a6765b19dd894 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Sun, 18 Feb 2024 04:15:42 -0500 Subject: [PATCH] swapped tokio for async std and realized I never implemented fetching stops --- Cargo.lock | 82 ++++++++---------------------------------------- Cargo.toml | 3 +- src/google.rs | 5 ++- src/main.rs | 17 ++++++---- src/path_calc.rs | 10 +++--- src/route.rs | 2 +- src/stop.rs | 16 ++++++++-- src/types.rs | 6 ++++ 8 files changed, 54 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb63c96..15c1237 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,16 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "async-channel" version = "1.9.0" @@ -165,6 +175,7 @@ dependencies = [ "blocking", "futures-lite 2.2.0", "once_cell", + "tokio", ] [[package]] @@ -319,6 +330,7 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" dependencies = [ + "async-attributes", "async-channel 1.9.0", "async-global-executor", "async-io 1.13.0", @@ -480,12 +492,12 @@ name = "bus_api" version = "0.1.0" dependencies = [ "anyhow", + "async-std", "chrono", "reqwest", "serde", "serde_json", "tide", - "tokio", ] [[package]] @@ -1247,16 +1259,6 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" -[[package]] -name = "lock_api" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.20" @@ -1407,29 +1409,6 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "redox_syscall", - "smallvec", - "windows-targets 0.48.5", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1633,15 +1612,6 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "reqwest" version = "0.11.24" @@ -1754,12 +1724,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "security-framework" version = "2.9.2" @@ -1907,12 +1871,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "smallvec" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" - [[package]] name = "socket2" version = "0.4.10" @@ -2243,25 +2201,11 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot", "pin-project-lite 0.2.13", - "signal-hook-registry", "socket2 0.5.5", - "tokio-macros", "windows-sys 0.48.0", ] -[[package]] -name = "tokio-macros" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.49", -] - [[package]] name = "tokio-native-tls" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 2d24b66..49825a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,11 @@ edition = "2021" [dependencies] anyhow = "1.0.79" +async-std = { version = "1.12.0", features = ["tokio1", "attributes"] } chrono = "0.4.34" reqwest = "0.11.24" #sea-orm = "0.12.14" serde = { version = "1.0.196", features = ["derive"] } serde_json = "1.0.113" tide = "0.16.0" -tokio = { version = "1.36.0", features = ["full"] } +#tokio = { version = "1.36.0", features = ["full"] } diff --git a/src/google.rs b/src/google.rs index 3ffd751..3fac807 100644 --- a/src/google.rs +++ b/src/google.rs @@ -1,12 +1,11 @@ -use tokio::io::AsyncReadExt; - +use async_std::io::ReadExt; pub struct TravelPathInfo{ pub duration: chrono::Duration, pub polyline: String } pub async fn calc_travel_path(path:&[crate::types::Coords])->anyhow::Result{ let mut google_api_key = String::new(); - tokio::fs::File::open("GOOGLE_API_KEY").await?.read_to_string(&mut google_api_key).await?; + async_std::fs::File::open("GOOGLE_API_KEY").await?.read_to_string(&mut google_api_key).await?; let client = reqwest::Client::new(); let response = client.post("https://routes.googleapis.com/directions/v2:computeRoutes") .body(serde_json::to_string(&build_route_body_pain(path))?) diff --git a/src/main.rs b/src/main.rs index 85aaa7e..42b8958 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -extern crate tokio; +//extern crate tokio; +extern crate async_std; extern crate reqwest; extern crate tide; extern crate anyhow; @@ -12,7 +13,7 @@ mod google; mod types; mod path_calc; -#[tokio::main] +#[async_std::main] async fn main() -> anyhow::Result<()> { let mut app = tide::with_state(state::State::new().await?); app.at("/path").get(transit_path); @@ -27,7 +28,7 @@ async fn main() -> anyhow::Result<()> { }); println!("READY!"); - app.listen("0.0.0.0:80").await?; + app.listen("0.0.0.0:8080").await?; Ok(()) } @@ -52,14 +53,18 @@ async fn transit_path(req: tide::Request)->tide::Result{ 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?; let ret = TransitPathResult{ path: route.into_iter().map(RetRoute::from).collect() }; - - Ok(serde_json::to_string(&ret)?.into()) + let resp_body:String = serde_json::to_string(&ret)?.into(); + let resp = tide::Response::builder(200) + .body(resp_body) + .header("Content-Type","application/json") + .build(); + Ok(resp) } #[derive(serde::Deserialize)] struct TransitPathQuery{ diff --git a/src/path_calc.rs b/src/path_calc.rs index 56c0c1e..c7cd85d 100644 --- a/src/path_calc.rs +++ b/src/path_calc.rs @@ -47,13 +47,15 @@ pub async fn calc_route(routes:&mut [crate::route::Route], from:crate::types::Co let min_direct_route = routes.iter().min_by_key(|route|route_measure_dist_to(route)+route_measure_dist_from(route)).unwrap(); let min_to_bu_route = routes.iter().min_by_key(|route|route_measure_dist_from(route)+route_measure_dist_bu(route)).unwrap(); let min_from_bu_route = routes.iter().min_by_key(|route|route_measure_dist_bu(route)+route_measure_dist_to(route)).unwrap(); - let min_to_bc_hub = routes.iter().min_by_key(|route|route_measure_dist_from(route)+route_measure_dist_bc_hub(route)).unwrap(); - let min_from_bc_hub = routes.iter().min_by_key(|route|route_measure_dist_bc_hub(route)+route_measure_dist_to(route)).unwrap(); + //let min_to_bc_hub = routes.iter().min_by_key(|route|route_measure_dist_from(route)+route_measure_dist_bc_hub(route)).unwrap(); + //let min_from_bc_hub = routes.iter().min_by_key(|route|route_measure_dist_bc_hub(route)+route_measure_dist_to(route)).unwrap(); let direct_route_cost = route_measure_dist_to(min_direct_route) + route_measure_dist_from(min_direct_route); 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); - + //let bc_route_cost = route_measure_dist_from(min_to_bc_hub) + route_measure_dist_from(min_from_bc_hub); + let min_to_bc_hub = crate::route::Route::default(); + let min_from_bc_hub = crate::route::Route::default(); + let bc_route_cost = u64::MAX; 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()])])) } diff --git a/src/route.rs b/src/route.rs index 385ddf6..fa2da87 100644 --- a/src/route.rs +++ b/src/route.rs @@ -1,5 +1,5 @@ -#[derive(Hash, Clone)] +#[derive(Hash, Clone, Default)] pub struct Route { pub id:u32, pub name: String, diff --git a/src/stop.rs b/src/stop.rs index 3a29b74..14cbd6e 100644 --- a/src/stop.rs +++ b/src/stop.rs @@ -1,4 +1,4 @@ -#[derive(Clone, PartialEq, PartialOrd)] +#[derive(Clone, PartialEq, PartialOrd, Debug)] pub struct Stop{ pub id:u32, pub name:String, @@ -52,11 +52,21 @@ impl From for Stop{ } } +#[derive(serde::Deserialize)] +struct OcctWrap{ + get_stops:Vec +} pub async fn occt_stops_get()->anyhow::Result>{ - Ok(Vec::new()) + let resp = reqwest::get("http://binghamtonupublic.etaspot.net/service.php?service=get_stops&token=TESTING").await?; + let body = resp.text().await?; + let OcctWrap {get_stops: stops}= serde_json::from_str(body.as_str())?; + Ok(stops.into_iter().map(Stop::from).collect()) } pub async fn bc_stops_get()->anyhow::Result>{ - Ok(Vec::new()) + let resp = reqwest::get("https://bctransit.doublemap.com/map/v2/stops").await?; + let body = resp.text().await?; + let stops:Vec = serde_json::from_str(body.as_str())?; + Ok(stops.into_iter().map(Stop::from).collect()) } pub fn poly_encode_stops>(stops:I)->String{ stops.into_iter() diff --git a/src/types.rs b/src/types.rs index 5d8bccd..7f6a1b8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,3 +7,9 @@ pub struct Coords{ #[derive(Clone, Copy, Debug, Hash)] pub enum Service{OCCT, BC} + +impl Default for Service{ + fn default() -> Self { + Service::BC + } +}