LED-BOARD/client/bin/src/straba.rs

159 lines
5.2 KiB
Rust
Raw Normal View History

2023-09-15 23:11:35 +02:00
use chrono::DateTime;
use std::time::{SystemTime, UNIX_EPOCH};
2023-09-15 22:01:43 +02:00
2023-08-19 00:56:54 +02:00
use serde::Deserialize;
2023-08-19 00:05:19 +02:00
const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494";
2023-08-19 00:56:54 +02:00
/* ******************** JSON Description ****************************** */
2023-08-18 23:06:17 +02:00
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Station {
2023-08-19 00:56:54 +02:00
pub id: String,
2023-08-18 23:06:17 +02:00
pub name: String,
2023-09-22 21:47:59 +02:00
pub graph_ql: GraphQL,
2023-08-21 20:34:54 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphQL {
pub response: GraphQLResponse,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphQLResponse {
pub name: String,
2023-09-13 22:11:08 +02:00
pub journeys: JourneysElement,
2023-08-21 20:34:54 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
2023-09-13 22:11:08 +02:00
pub struct JourneysElement {
pub elements: Vec<Journey>,
2023-08-18 23:06:17 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Line {
2023-09-22 21:47:59 +02:00
pub line_group: LineGroup,
2023-09-13 22:11:08 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LineGroup {
pub label: String,
2023-08-18 23:06:17 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Journey {
pub line: Line,
pub canceled: bool,
2023-09-13 22:11:08 +02:00
pub stops: Vec<StopsElement>,
2023-08-18 23:06:17 +02:00
}
2023-09-13 22:11:08 +02:00
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StopsElement {
2023-09-22 21:47:59 +02:00
pub destination_label: String,
pub planned_departure: IsoStringDateTime,
pub realtime_departure: IsoStringDateTime,
2023-09-13 22:11:08 +02:00
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsoStringDateTime {
2023-09-22 21:47:59 +02:00
pub iso_string: Option<String>,
}
2023-08-19 00:56:54 +02:00
// Return value
2023-08-18 23:06:17 +02:00
pub struct NextDeparture {
pub request_time: i64,
2023-09-16 00:24:53 +02:00
pub outbound_station: String,
pub outbound_diff: i64,
pub inbound_station: String,
pub inbound_diff: i64,
2023-09-15 23:11:35 +02:00
pub failure: bool,
2023-08-18 23:06:17 +02:00
}
pub fn fetch_data(debug_print : Option<bool>) -> NextDeparture {
2023-09-15 23:11:35 +02:00
2023-09-15 22:01:43 +02:00
let st_now = SystemTime::now();
let seconds = st_now.duration_since(UNIX_EPOCH).unwrap().as_secs();
let url = &format!("{}?datetime={}", STATION_URL, seconds);
let result = reqwest::blocking::get(url);
2023-09-22 21:47:59 +02:00
let mut return_value = NextDeparture {
failure : false,
outbound_station : String::from(""),
outbound_diff : 10000,
inbound_station : String::from(""),
inbound_diff : 10000,
request_time : seconds as i64,
};
2023-09-15 22:01:43 +02:00
2023-08-19 00:05:19 +02:00
if result.is_err() {
2023-09-15 23:11:35 +02:00
println!("Could not read station response {:?}", result.err());
2023-09-22 21:47:59 +02:00
return_value.failure = true;
return return_value;
2023-08-19 00:05:19 +02:00
}
let text = result.unwrap().text();
if text.is_err() {
2023-08-19 00:56:54 +02:00
println!("Could not convert response {:?}", text.err());
2023-09-22 21:47:59 +02:00
return_value.failure = true;
return return_value;
2023-08-19 00:05:19 +02:00
}
2023-08-18 23:06:17 +02:00
2023-09-22 21:47:59 +02:00
let raw_text = &text.unwrap();
let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&raw_text);
2023-08-19 00:05:19 +02:00
if body.is_err() {
println!("Could not parse json {:?}", body.err());
println!("------------------------- %< ----------------------------");
2023-09-22 21:47:59 +02:00
println!("{}", &raw_text);
println!("------------------------- %< ----------------------------");
2023-09-22 21:47:59 +02:00
return_value.failure = true;
return return_value;
2023-08-19 00:05:19 +02:00
}
2023-09-13 22:11:08 +02:00
// parse JSON result.. search of both directions
2023-08-19 00:05:19 +02:00
let json = body.unwrap();
2023-09-22 21:47:59 +02:00
for el in json.graph_ql.response.journeys.elements {
if debug_print.is_some() && debug_print.unwrap() == true {
println!("Line {:}", el.line.line_group.label);
}
for stop in el.stops {
// use only valid data
2023-09-22 21:47:59 +02:00
if stop.realtime_departure.iso_string.is_some() &&
stop.destination_label != "" {
let txt_departure = stop.realtime_departure.iso_string.unwrap();
2023-09-16 00:24:53 +02:00
let next_departure = DateTime::parse_from_rfc3339(&txt_departure).unwrap();
let diff = next_departure.timestamp() - (seconds as i64);
2023-09-22 21:47:59 +02:00
if debug_print.is_some() && debug_print.unwrap() == true {
println!("To {:} {:} (in {:} seconds)", stop.destination_label, txt_departure, diff );
}
2023-09-16 13:44:30 +02:00
2023-09-22 21:47:59 +02:00
if stop.destination_label.contains("Rheinau") {
if diff < return_value.outbound_diff {
return_value.outbound_station = stop.destination_label;
return_value.outbound_diff = diff;
2023-09-16 13:44:30 +02:00
}
2023-09-22 21:47:59 +02:00
} else if stop.destination_label.contains("Hochschule") ||
stop.destination_label.contains("Hauptbahnhof") ||
stop.destination_label.contains("Schönau") {
if diff < return_value.inbound_diff {
return_value.inbound_station = stop.destination_label;
return_value.inbound_diff = diff;
2023-09-15 23:11:35 +02:00
}
}
} else {
2023-09-22 21:47:59 +02:00
println!("Planned {:} {:?}", stop.destination_label, stop.planned_departure.iso_string)
2023-08-21 20:34:54 +02:00
}
}
2023-08-19 01:24:12 +02:00
}
2023-09-22 21:47:59 +02:00
return_value
}