summaryrefslogtreecommitdiff
path: root/src/onboard/zugportal.rs
blob: c64910758621c39e6f932e868129278495a860a2 (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
98
99
100
101
102
103
104
105
106
107
108
109
/// 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 crate::onboard;
use crate::onboard::{OnBoardAPI, OnBoardInfo};
use crate::{traits::*, travelynx::TrainRef};

use crate::types::Trip;

pub struct Zugportal {}

#[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 std::fmt::Display for Stop {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.station.name)
  }
}

impl OnBoardInfo for Journey {
  fn guess_last_station(&self) -> Option<&dyn IsStation> {
    todo!()
  }

  fn get_train_ref(&self) -> TrainRef {
    TrainRef {
      _type: self.name.clone(),
      no: self.no.to_string().clone()
    }
  }

  fn stops<'a>(&'a self) -> Trip<'a> {
    (&self.stops).into()
  }
}

impl OnBoardAPI for Zugportal {
  fn apiurl(&self) -> &'static str {
    "https://zugportal.de/prd/zupo-travel-information/api/public/ri/journey"
  }

  fn request(
    &self,
    debug: bool
  ) -> Result<Box<dyn OnBoardInfo>, serde_json::Error> {
    onboard::request::<_, Journey>(self, debug)
  }
}