summaryrefslogtreecommitdiff
path: root/src/zugportal.rs
blob: f00cd04c2861df824f30db30ea635e141616e872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 <= &current_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)
  }
}