2023-08-19 01:24:12 +02:00
|
|
|
use chrono::{Utc, DateTime, NaiveDateTime, Timelike};
|
|
|
|
use chrono_tz::Europe::Berlin;
|
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,
|
|
|
|
pub journeys: Vec<JourneyElements>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct JourneyElements {
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub elements: Vec<JourneyElements>,
|
2023-08-18 23:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Line {
|
2023-08-19 00:56:54 +02:00
|
|
|
pub id: 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 destination: String,
|
2023-08-19 00:05:19 +02:00
|
|
|
pub barrier_level: String,
|
|
|
|
pub loads_forecast_type: String,
|
2023-08-19 00:56:54 +02:00
|
|
|
pub realtime_departure: Option<i64>,
|
2023-08-19 00:05:19 +02:00
|
|
|
pub scheduled_departure: i64,
|
2023-08-19 00:56:54 +02:00
|
|
|
pub difference: Option<i64>,
|
2023-08-18 23:06:17 +02:00
|
|
|
pub canceled: bool,
|
|
|
|
}
|
|
|
|
|
2023-08-19 00:56:54 +02:00
|
|
|
// Return value
|
2023-08-18 23:06:17 +02:00
|
|
|
pub struct NextDeparture {
|
|
|
|
pub rheinau: i64,
|
|
|
|
pub schoenau: i64,
|
|
|
|
}
|
|
|
|
|
2023-08-19 01:24:12 +02:00
|
|
|
pub fn fetch_data() -> Option<&'static str> {
|
2023-08-19 00:05:19 +02:00
|
|
|
let result = reqwest::blocking::get(STATION_URL);
|
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() {
|
|
|
|
println!("Could not read station response {:?}", result.err());
|
|
|
|
return Option::None;
|
|
|
|
}
|
|
|
|
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-08-19 00:05:19 +02:00
|
|
|
return Option::None;
|
|
|
|
}
|
2023-08-18 23:06:17 +02:00
|
|
|
|
|
|
|
|
2023-08-19 00:56:54 +02:00
|
|
|
let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&text.unwrap());
|
2023-08-19 00:05:19 +02:00
|
|
|
|
|
|
|
if body.is_err() {
|
|
|
|
println!("Could not parse json {:?}", body.err());
|
|
|
|
return Option::None;
|
|
|
|
}
|
|
|
|
|
2023-08-19 01:24:12 +02:00
|
|
|
let cur_time = DateTime::<Utc>::default();
|
|
|
|
|
|
|
|
// parse JSON result.. serach of both directions
|
2023-08-19 00:05:19 +02:00
|
|
|
let json = body.unwrap();
|
2023-08-21 20:34:54 +02:00
|
|
|
for journey in (json.graphQL.response.journeys) {
|
2023-08-19 01:24:12 +02:00
|
|
|
let destination = (journey.destination).to_string();
|
|
|
|
let departure = (journey.realtime_departure);
|
|
|
|
let difference = (journey.difference);
|
|
|
|
|
|
|
|
if (departure.is_some()) {
|
|
|
|
// get current time
|
|
|
|
let time_s = departure.unwrap();
|
|
|
|
let local_time = NaiveDateTime::from_timestamp_millis(time_s*1000).unwrap();
|
|
|
|
let zoned_time : DateTime<Utc> = DateTime::from_utc(local_time, Utc);
|
|
|
|
let europe_time = zoned_time.with_timezone(&Berlin);
|
|
|
|
|
|
|
|
let hour = europe_time.hour();
|
|
|
|
let minute = europe_time.minute();
|
2023-08-21 20:34:54 +02:00
|
|
|
if (zoned_time > cur_time) {
|
|
|
|
println!("------------- Future starts here ----------------");
|
|
|
|
}
|
2023-08-19 01:24:12 +02:00
|
|
|
println!("{0} {1}:{2}", destination, hour, minute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some("")
|
2023-08-18 00:03:38 +02:00
|
|
|
}
|