hkconnect 0.1.4

Async client for Hyundai Bluelink India and Kia Connect India
Documentation
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Trip history: monthly summaries, the trips on a day, and one trip's
//! route and driving events.
//!
//! Units as sent by the API: minutes, kilometres, km/h.

use jiff::Timestamp;
use jiff::civil::Date;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;

use crate::de::{
    flexible_f64, flexible_int, flexible_string, ist_timestamp, lenient, lenient_vec, parse_ist,
    parse_utc,
};

/// Totals for a month, and which days had trips, from
/// [`Client::trips_in_month`](crate::Client::trips_in_month).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TripMonth {
    /// Driving totals for the whole month.
    pub totals: TripTotals,
    /// The days that had at least one trip, in the order the server sent
    /// them.
    pub days: Vec<TripDayCount>,
}

/// How many trips were taken on one day of a [`TripMonth`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TripDayCount {
    /// The day, in Indian Standard Time.
    pub date: Date,
    /// Number of trips that day.
    pub trips: u32,
}

/// Driving totals over some period: a month, a day, or one trip. Each field
/// is `None` if the server did not report it.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TripTotals {
    /// Time spent driving, in minutes.
    pub drive_minutes: Option<u32>,
    /// Time spent idling with the engine on, in minutes.
    pub idle_minutes: Option<u32>,
    /// Distance driven, in km.
    pub distance_km: Option<f64>,
    /// Average speed, in km/h.
    pub average_speed_kmh: Option<f64>,
    /// Top speed, in km/h.
    pub max_speed_kmh: Option<f64>,
}

/// The trips taken on one day, from
/// [`Client::trips_on_day`](crate::Client::trips_on_day).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TripDay {
    /// The requested day, in Indian Standard Time.
    pub date: Date,
    /// Driving totals for the day. All `None` if there were no trips.
    pub totals: TripTotals,
    /// The trips, oldest first.
    pub trips: Vec<Trip>,
}

/// One trip, as listed for a day. Pass it to
/// [`Client::trip_detail`](crate::Client::trip_detail) for the route.
///
/// A `Trip` can be serialized, stored, deserialized and passed to
/// [`Client::trip_detail`](crate::Client::trip_detail) later. Its serialized
/// form includes an opaque `_detail_ref` object holding the server's own
/// identifiers for the trip, echoed back exactly as received; treat it as
/// private and keep it unchanged.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Trip {
    /// The server's id for this trip (its `serviceTID`), as text.
    pub id: String,
    /// When the trip started; `None` if the server sent an unparseable time.
    pub started_at: Option<Timestamp>,
    /// When the trip ended; `None` if the server sent an unparseable time.
    pub ended_at: Option<Timestamp>,
    /// Where the trip started, if reported.
    pub start: Option<Coordinates>,
    /// Where the trip ended, if reported.
    pub end: Option<Coordinates>,
    /// What [`Client::trip_detail`](crate::Client::trip_detail) sends back.
    #[serde(rename = "_detail_ref")]
    pub(crate) detail_ref: TripRef,
}

/// The raw identifiers the API uses for a trip, echoed back verbatim when
/// asking for its detail.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct TripRef {
    /// `serviceTID`, as received: the server has sent it as a number, but
    /// it is echoed with whatever JSON type it arrived as.
    pub service_tid: Value,
    /// `tripStartTime`, local time `YYYYMMDDHHMMSS`.
    pub start: String,
    /// `tripEndTime`, local time `YYYYMMDDHHMMSS`.
    pub end: String,
}

impl TripRef {
    /// The `YYYYMMDD` day the trip is listed under: the start time's date.
    pub(crate) fn day(&self) -> &str {
        self.start.get(..8).unwrap_or(&self.start)
    }
}

/// One trip's route and driving behaviour, from
/// [`Client::trip_detail`](crate::Client::trip_detail).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TripDetail {
    /// When the trip started; `None` if not reported.
    pub started_at: Option<Timestamp>,
    /// When the trip ended; `None` if not reported.
    pub ended_at: Option<Timestamp>,
    /// Driving totals for this trip.
    pub totals: TripTotals,
    /// Number of harsh-braking events.
    pub hard_braking: Option<u32>,
    /// Number of rapid-acceleration events.
    pub rapid_acceleration: Option<u32>,
    /// Number of speeding events.
    pub speeding: Option<u32>,
    /// GPS points along the route, in the order the server sent them.
    pub route: Vec<RoutePoint>,
}

/// A position (WGS 84).
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Coordinates {
    /// Latitude in degrees.
    pub latitude: f64,
    /// Longitude in degrees.
    pub longitude: f64,
}

/// One GPS point on a trip's route.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutePoint {
    /// When the car was here; `None` if not reported.
    pub at: Option<Timestamp>,
    /// Latitude in degrees.
    pub latitude: f64,
    /// Longitude in degrees.
    pub longitude: f64,
}

// ----- wire formats -----

#[derive(Debug, Default, Deserialize)]
struct WireTotals {
    #[serde(rename = "tripDrvTime", default, deserialize_with = "flexible_int")]
    drive_minutes: Option<u32>,
    #[serde(rename = "tripIdleTime", default, deserialize_with = "flexible_int")]
    idle_minutes: Option<u32>,
    #[serde(rename = "tripDist", default, deserialize_with = "flexible_f64")]
    distance_km: Option<f64>,
    #[serde(rename = "tripAvgSpeed", default, deserialize_with = "flexible_f64")]
    average_speed_kmh: Option<f64>,
    #[serde(rename = "tripMaxSpeed", default, deserialize_with = "flexible_f64")]
    max_speed_kmh: Option<f64>,
}

impl From<WireTotals> for TripTotals {
    fn from(t: WireTotals) -> Self {
        Self {
            drive_minutes: t.drive_minutes,
            idle_minutes: t.idle_minutes,
            distance_km: t.distance_km,
            average_speed_kmh: t.average_speed_kmh,
            max_speed_kmh: t.max_speed_kmh,
        }
    }
}

/// `POST /vehicles/{id}/tripinfo` for a month.
#[derive(Debug, Deserialize)]
pub(crate) struct WireTripMonth {
    #[serde(flatten)]
    totals: WireTotals,
    #[serde(rename = "tripDayList", default, deserialize_with = "lenient_vec")]
    days: Vec<WireTripDayCount>,
}

/// An entry without a valid date fails to parse and is skipped.
#[derive(Debug, Deserialize)]
struct WireTripDayCount {
    #[serde(rename = "tripDayInMonth", deserialize_with = "yyyymmdd")]
    date: Date,
    #[serde(rename = "tripCntDay", default, deserialize_with = "flexible_int")]
    trips: Option<u32>,
}

impl WireTripMonth {
    pub(crate) fn into_month(self) -> TripMonth {
        TripMonth {
            totals: self.totals.into(),
            days: self
                .days
                .into_iter()
                .map(|d| TripDayCount {
                    date: d.date,
                    trips: d.trips.unwrap_or(0),
                })
                .collect(),
        }
    }
}

/// `POST /vehicles/{id}/tripinfo` for a day.
#[derive(Debug, Deserialize)]
pub(crate) struct RawTripDayResponse {
    #[serde(rename = "dayTripList", default, deserialize_with = "lenient_vec")]
    day_trip_list: Vec<RawTripDay>,
}

#[derive(Debug, Deserialize)]
struct RawTripDay {
    #[serde(flatten)]
    totals: WireTotals,
    #[serde(rename = "tripList", default, deserialize_with = "lenient_vec")]
    trip_list: Vec<RawTrip>,
}

/// A trip without an id or a start time cannot be looked up later, so it
/// fails to parse and is skipped.
#[derive(Debug, Deserialize)]
struct RawTrip {
    #[serde(rename = "serviceTID", deserialize_with = "present")]
    service_tid: Value,
    #[serde(rename = "tripStartTime", deserialize_with = "required_string")]
    start: String,
    #[serde(rename = "tripEndTime", default, deserialize_with = "flexible_string")]
    end: Option<String>,
    #[serde(rename = "tripStartCoord", default, deserialize_with = "lenient")]
    start_coord: Option<RawCoord>,
    #[serde(rename = "tripEndCoord", default, deserialize_with = "lenient")]
    end_coord: Option<RawCoord>,
}

impl RawTrip {
    fn into_trip(self) -> Trip {
        let id = match &self.service_tid {
            Value::String(s) => s.clone(),
            other => other.to_string(),
        };
        let end = self.end.unwrap_or_default();
        Trip {
            id,
            started_at: parse_ist(&self.start),
            ended_at: parse_ist(&end),
            start: self.start_coord.and_then(RawCoord::into_coordinates),
            end: self.end_coord.and_then(RawCoord::into_coordinates),
            detail_ref: TripRef {
                service_tid: self.service_tid,
                start: self.start,
                end,
            },
        }
    }
}

#[derive(Debug, Deserialize)]
struct RawCoord {
    #[serde(default, deserialize_with = "flexible_f64")]
    lat: Option<f64>,
    #[serde(default, deserialize_with = "flexible_f64")]
    lon: Option<f64>,
}

impl RawCoord {
    fn into_coordinates(self) -> Option<Coordinates> {
        Some(Coordinates {
            latitude: self.lat?,
            longitude: self.lon?,
        })
    }
}

impl RawTripDayResponse {
    pub(crate) fn into_day(self, date: Date) -> TripDay {
        let mut totals = TripTotals::default();
        let mut trips = Vec::new();
        // Normally one entry per day; merge defensively if there are more.
        for (i, day) in self.day_trip_list.into_iter().enumerate() {
            if i == 0 {
                totals = day.totals.into();
            }
            trips.extend(day.trip_list.into_iter().map(RawTrip::into_trip));
        }
        // The server lists newest first; chronological order is easier to
        // read and makes trip numbers stable.
        trips.sort_by(|a, b| a.detail_ref.start.cmp(&b.detail_ref.start));
        TripDay {
            date,
            totals,
            trips,
        }
    }
}

/// `POST /vehicles/{id}/tripinfo/detail`.
#[derive(Debug, Deserialize)]
pub(crate) struct RawTripDetailResponse {
    /// Missing, null or malformed reads as an empty detail.
    #[serde(rename = "tripInfo", default, deserialize_with = "lenient")]
    trip_info: Option<RawTripDetail>,
}

#[derive(Debug, Default, Deserialize)]
struct RawTripDetail {
    #[serde(flatten)]
    totals: WireTotals,
    #[serde(rename = "tripStartTime", default, deserialize_with = "ist_timestamp")]
    started_at: Option<Timestamp>,
    #[serde(rename = "tripEndTime", default, deserialize_with = "ist_timestamp")]
    ended_at: Option<Timestamp>,
    #[serde(
        rename = "tripHardBreakingTime",
        default,
        deserialize_with = "flexible_int"
    )]
    hard_braking: Option<u32>,
    #[serde(
        rename = "tripRapidAccelationTime",
        default,
        deserialize_with = "flexible_int"
    )]
    rapid_acceleration: Option<u32>,
    #[serde(
        rename = "tripExcessiveSpeedTime",
        default,
        deserialize_with = "flexible_int"
    )]
    speeding: Option<u32>,
    #[serde(rename = "tripList", default, deserialize_with = "lenient_vec")]
    route: Vec<RawRoutePoint>,
}

#[derive(Debug, Deserialize)]
struct RawRoutePoint {
    #[serde(default, deserialize_with = "flexible_f64")]
    lat: Option<f64>,
    #[serde(default, deserialize_with = "flexible_f64")]
    lon: Option<f64>,
    #[serde(rename = "tripDate", default, deserialize_with = "lenient")]
    date: Option<RawUtc>,
}

#[derive(Debug, Deserialize)]
struct RawUtc {
    #[serde(default, deserialize_with = "flexible_string")]
    utc: Option<String>,
}

impl RawRoutePoint {
    /// `None` for a point without both latitude and longitude.
    fn into_point(self) -> Option<RoutePoint> {
        Some(RoutePoint {
            at: self.date.and_then(|d| d.utc).as_deref().and_then(parse_utc),
            latitude: self.lat?,
            longitude: self.lon?,
        })
    }
}

impl RawTripDetailResponse {
    pub(crate) fn into_detail(self) -> TripDetail {
        let t = self.trip_info.unwrap_or_default();
        TripDetail {
            started_at: t.started_at,
            ended_at: t.ended_at,
            totals: t.totals.into(),
            hard_braking: t.hard_braking,
            rapid_acceleration: t.rapid_acceleration,
            speeding: t.speeding,
            route: t
                .route
                .into_iter()
                .filter_map(RawRoutePoint::into_point)
                .collect(),
        }
    }
}

fn yyyymmdd<'de, D: Deserializer<'de>>(d: D) -> Result<Date, D::Error> {
    let raw = String::deserialize(d)?;
    Date::strptime("%Y%m%d", &raw).map_err(serde::de::Error::custom)
}

/// Any value except null.
fn present<'de, D: Deserializer<'de>>(d: D) -> Result<Value, D::Error> {
    match Value::deserialize(d)? {
        Value::Null => Err(serde::de::Error::custom("unexpected null")),
        v => Ok(v),
    }
}

/// A string or number, converted to a string; null is an error.
fn required_string<'de, D: Deserializer<'de>>(d: D) -> Result<String, D::Error> {
    flexible_string(d)?.ok_or_else(|| serde::de::Error::custom("expected a string"))
}