summaryrefslogtreecommitdiff
path: root/src/onboard/iceportal.rs
blob: 55b35e7b27188fee5d78fbd703234e62b53c7138 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/// implementation of traits to query the iceportal.de
/// (available in high speed trains in DE)
use chrono::{DateTime, Utc};
use serde::Deserialize;
use serde_json::Value;

use crate::onboard;
use crate::onboard::{OnBoardAPI, OnBoardInfo};
use crate::{serde::*, traits::*, travelynx::TrainRef};

use crate::types::Trip;

pub struct Iceportal {}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TripInfo {
  trip: IceTrip,
  connection: Option<Value>,
  selected_route: Option<Value>,
  active: Option<Value>
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct IceTrip {
  train_type: String,
  vzn: String, // train number
  // some position info here
  actual_position: u64, // distance along track, presumably
  stops: Vec<Stop>
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Stop {
  info: StopInfo,
  station: Station,
  timetable: Timetable
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct StopInfo {
  distance_from_start: u64,
  position_status: Option<String> // one of "departed", "future", ... (?); null if cancelled
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Station {
  eva_nr: String,
  name: String
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Timetable {
  #[serde(deserialize_with = "option_naive_read_unixtime_db")]
  scheduled_arrival_time: Option<DateTime<Utc>>,
  #[serde(deserialize_with = "option_naive_read_unixtime_db")]
  actual_arrival_time: Option<DateTime<Utc>>
}

impl IsStation for Stop {
  fn name(&self) -> &str {
    &self.station.name
  }

  fn scheduled_arrival(&self) -> Option<&chrono::DateTime<Utc>> {
    self.timetable.scheduled_arrival_time.as_ref()
  }

  fn real_arrival(&self) -> Option<&chrono::DateTime<Utc>> {
    self.timetable.scheduled_arrival_time.as_ref()
  }

  fn ds100(&self) -> Option<&str> {
    None
  }
}

impl OnBoardInfo for TripInfo {
  fn guess_last_station(&self) -> Option<&dyn IsStation> {
    let current_pos = self.trip.actual_position;
    self
      .trip
      .stops
      .iter()
      .rev()
      .filter(|stop| stop.info.position_status.is_some())
      .map(|stop| (stop.info.distance_from_start, stop))
      .filter(|(dist, _)| dist <= &current_pos)
      .next()
      .map(|(_, stop)| stop as &dyn IsStation)
  }

  fn get_train_ref(&self) -> TrainRef {
    TrainRef {
      _type: self.trip.train_type.clone(),
      no: self.trip.vzn.clone()
    }
  }

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

impl std::fmt::Display for Stop {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.station.name)
  }
}

impl OnBoardAPI for Iceportal {
  fn apiurl(&self) -> &'static str {
    "https://iceportal.de/api1/rs/tripInfo/trip"
  }

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