made some more changes to make it more functional api
This commit is contained in:
parent
645d18fc4e
commit
ac1618ced4
6 changed files with 59 additions and 13 deletions
10
README.md
Normal file
10
README.md
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
50
src/main.rs
50
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<state::State>)->tide::Result{
|
||||
async fn transit_path(req: tide::Request<state::State>)->tide::Result{
|
||||
let query = req.query::<TransitPathQuery>()?;
|
||||
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<RetRoute>
|
||||
}
|
||||
#[derive(serde::Serialize)]
|
||||
struct RetRoute{
|
||||
route_name:String,
|
||||
//enter_stop_id:u32,
|
||||
//exit_stop_id:u32,
|
||||
//enter_stop_coords: types::Coords
|
||||
}
|
||||
impl From<route::Route> 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<state::State>)->tide::Result{
|
||||
async fn transit_estimate(req: tide::Request<state::State>)->tide::Result{
|
||||
let query = req.query::<TransitEstimateQuery>()?;
|
||||
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<state::State>)->tide::Result{
|
||||
async fn bus_route_draw(req: tide::Request<state::State>)->tide::Result{
|
||||
let query = req.query::<RouteDrawQuery>()?;
|
||||
let route = match query.service.as_str() {
|
||||
"OCCT" => {
|
||||
|
|
|
@ -22,9 +22,9 @@ fn sort_routes_by_measure<F:Fn(&&crate::stop::Stop)->u64+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<crate::route::Route>{
|
||||
pub async fn calc_route(routes:&mut [crate::route::Route], from:crate::types::Coords, to: crate::types::Coords)->anyhow::Result<Vec<crate::route::Route>>{
|
||||
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{
|
||||
|
|
|
@ -74,7 +74,7 @@ pub async fn broome_county_routes_get()->anyhow::Result<Vec<Route>>{
|
|||
let resp = reqwest::get("https://bctransit.doublemap.com/map/v2/routes").await?;
|
||||
let body = resp.text().await?;
|
||||
let routes:Vec<BCRoute> = 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<crate::stop::Stop> = stops.iter()
|
||||
.filter(|stop|{
|
||||
|
|
|
@ -55,7 +55,7 @@ impl From<BCStop> for Stop{
|
|||
pub async fn occt_stops_get()->anyhow::Result<Vec<Stop>>{
|
||||
Ok(Vec::new())
|
||||
}
|
||||
pub async fn BC_stops_get()->anyhow::Result<Vec<Stop>>{
|
||||
pub async fn bc_stops_get()->anyhow::Result<Vec<Stop>>{
|
||||
Ok(Vec::new())
|
||||
}
|
||||
pub fn poly_encode_stops<I:IntoIterator<Item=Stop>>(stops:I)->String{
|
||||
|
|
Loading…
Reference in a new issue