2023-09-15 23:11:35 +02:00
|
|
|
use chrono::DateTime;
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2023-08-19 01:24:12 +02:00
|
|
|
use chrono_tz::Europe::Berlin;
|
2023-09-15 22:01:43 +02:00
|
|
|
|
2023-08-18 00:03:38 +02:00
|
|
|
/* @file straba.rs
|
|
|
|
* @brief fetch next depature of light rail vehicle
|
|
|
|
*/
|
2023-08-19 00:05:19 +02:00
|
|
|
use serde_json::Value;
|
2023-08-19 00:56:54 +02:00
|
|
|
use serde::Deserialize;
|
2023-08-18 00:03:38 +02:00
|
|
|
|
2023-08-19 00:05:19 +02:00
|
|
|
const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494";
|
2023-08-18 00:03:38 +02:00
|
|
|
|
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-08-21 20:34:54 +02:00
|
|
|
pub graphQL: GraphQL,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-13 22:11:08 +02:00
|
|
|
pub lineGroup: LineGroup,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-15 18:56:42 +02:00
|
|
|
pub destinationLabel: String,
|
|
|
|
pub plannedDeparture: IsoStringDateTime,
|
|
|
|
pub realtimeDeparture: IsoStringDateTime,
|
2023-09-13 22:11:08 +02:00
|
|
|
}
|
2023-09-15 18:56:42 +02:00
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IsoStringDateTime {
|
|
|
|
pub isoString: Option<String>,
|
|
|
|
}
|
|
|
|
|
2023-08-19 00:56:54 +02:00
|
|
|
// Return value
|
2023-08-18 23:06:17 +02:00
|
|
|
pub struct NextDeparture {
|
2023-09-15 23:11:35 +02:00
|
|
|
pub rheinau: String,
|
|
|
|
pub schoenau: String,
|
|
|
|
pub failure: bool,
|
2023-08-18 23:06:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-15 23:11:35 +02:00
|
|
|
pub fn fetch_data() -> NextDeparture {
|
|
|
|
let mut returnValue = NextDeparture {
|
|
|
|
failure : false,
|
|
|
|
rheinau : String::from(""),
|
|
|
|
schoenau : String::from(""),
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
/*let cur_time = readTime( DateTime::<Utc>::default() );
|
|
|
|
println!("Next results after {:?}", cur_time);
|
|
|
|
|
|
|
|
return Some("Debug");
|
|
|
|
*/
|
2023-08-19 00:56:54 +02:00
|
|
|
println!("Start Straba Crawler");
|
2023-08-18 23:06:17 +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());
|
|
|
|
returnValue.failure = true;
|
|
|
|
return returnValue;
|
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-15 23:11:35 +02:00
|
|
|
returnValue.failure = true;
|
|
|
|
return returnValue;
|
2023-08-19 00:05:19 +02:00
|
|
|
}
|
2023-09-13 22:11:08 +02:00
|
|
|
let rawText = &text.unwrap();
|
2023-08-18 23:06:17 +02:00
|
|
|
|
2023-09-13 22:11:08 +02:00
|
|
|
let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&rawText);
|
2023-08-19 00:05:19 +02:00
|
|
|
|
|
|
|
if body.is_err() {
|
|
|
|
println!("Could not parse json {:?}", body.err());
|
2023-09-15 19:00:10 +02:00
|
|
|
println!("------------------------- %< ----------------------------");
|
|
|
|
println!("{}", &rawText);
|
|
|
|
println!("------------------------- %< ----------------------------");
|
2023-09-15 23:11:35 +02:00
|
|
|
returnValue.failure = true;
|
|
|
|
return returnValue;
|
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-13 22:11:08 +02:00
|
|
|
for el in (json.graphQL.response.journeys.elements) {
|
2023-09-15 18:56:42 +02:00
|
|
|
println!("Line {:}", el.line.lineGroup.label);
|
|
|
|
for stop in el.stops {
|
|
|
|
if stop.realtimeDeparture.isoString.is_some() {
|
2023-09-15 19:19:35 +02:00
|
|
|
let txt_departure = stop.realtimeDeparture.isoString.unwrap();
|
|
|
|
let next_departure = DateTime::parse_from_rfc3339(&txt_departure);
|
2023-09-15 22:01:43 +02:00
|
|
|
println!("To {:} {:}", stop.destinationLabel, txt_departure);
|
2023-09-15 23:11:35 +02:00
|
|
|
if returnValue.rheinau == "" {
|
|
|
|
if stop.destinationLabel.contains("Rheinau") {
|
|
|
|
returnValue.rheinau = txt_departure;
|
|
|
|
} else if returnValue.schoenau == "" {
|
|
|
|
returnValue.schoenau = txt_departure;
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 18:56:42 +02:00
|
|
|
} else {
|
|
|
|
println!("Planned {:} {:?}", stop.destinationLabel, stop.plannedDeparture.isoString)
|
2023-08-21 20:34:54 +02:00
|
|
|
}
|
2023-09-15 18:56:42 +02:00
|
|
|
}
|
2023-08-19 01:24:12 +02:00
|
|
|
}
|
|
|
|
|
2023-09-15 23:11:35 +02:00
|
|
|
returnValue
|
2023-08-18 00:03:38 +02:00
|
|
|
}
|