diff options
author | stuebinm | 2022-02-08 16:55:51 +0100 |
---|---|---|
committer | stuebinm | 2022-02-08 16:56:21 +0100 |
commit | cf88935b5245daea51d2b513709b61a0e43483d6 (patch) | |
tree | a3900ac0ca602003a144aad8ca3e6336e1e9f165 /src/zugportal.rs | |
parent | 36acf9b9e4d35dea60ef2bfe9998d09fd6a1ebef (diff) |
S-Bahns are weird
(added a simple zugportal.de api client, but can't do auto-checkin yet)
Diffstat (limited to 'src/zugportal.rs')
-rw-r--r-- | src/zugportal.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/zugportal.rs b/src/zugportal.rs new file mode 100644 index 0000000..f00cd04 --- /dev/null +++ b/src/zugportal.rs @@ -0,0 +1,97 @@ +/// implementation of traits to query zugportal.de +/// (available at least in the Munich S-Bahn, maybe other trains) + +use chrono::{DateTime, Utc}; +use serde::Deserialize; +use serde_json::Value; + +use crate::{serde::*, travelynx::TrainRef, types::IsStation}; + +#[derive(Deserialize, Debug)] +#[serde(rename_all="camelCase")] +pub struct Journey { + name: String, // the line's name, e.g. S 8 + no: i64, + stops: Vec<Stop> +} + + +#[derive(Deserialize, Debug)] +#[serde(rename_all="camelCase")] +pub struct Stop { + station: Station, + status: String, // one of "Normal", ...? + track: Track, + messages: Vec<String>, + arrival_time: Option<DepartureTime>, + departure_time: Option<DepartureTime> +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all="camelCase")] +struct Station { + eva_no: String, + name: String +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all="camelCase")] +struct Track { + target: String, + prediction: String +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all="camelCase")] +struct DepartureTime { + target: DateTime<Utc>, + predicted: DateTime<Utc>, + time_type: String, // one of REAL, PREVIEW, ..? + diff: i64, // diff in minutes? + // NOTE: also sends predictedTimeInMs and targetTimeInMs; these might be unix times +} + +impl IsStation for Stop { + fn name(&self) -> &str { + &self.station.name + } + + fn scheduled_arrival(&self) -> Option<&chrono::DateTime<Utc>> { + self.arrival_time.as_ref().map(|t| &t.target) + } + + fn real_arrival(&self) -> Option<&chrono::DateTime<Utc>> { + self.arrival_time.as_ref().map(|t| &t.predicted) + } + + fn ds100(&self) -> &str { + "??" + } +} + +impl Journey { + pub fn guess_last_station(&self) -> Option<String> { + todo!() + // let current_pos = self.trip.actual_position; + // self + // .trip + // .stops + // .iter() + // .rev() + // .map(|stop| (stop.info.distance_from_start, stop)) + // .filter(|(dist, _)| dist <= ¤t_pos) + // .next() + // .map(|(_, stop)| stop.station.name.clone()) + } + + pub fn get_train_ref(&self) -> TrainRef { + TrainRef { + _type: self.name.clone(), + no: self.no.to_string().clone() + } + } + + pub fn trip(&self) -> crate::types::Trip<'_, Stop> { + crate::types::Trip(&self.stops) + } +} |