ocpi-tariffs 0.46.1

OCPI tariff calculations
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use chrono::{DateTime, TimeDelta, Utc};
use rust_decimal::Decimal;

use super::super::Consumed;

use crate::{
    country, currency, expect_array_or_bail, expect_object_or_bail,
    json::{self, FieldsAsExt as _, FromJson as _},
    number::FromDecimal as _,
    parse_nullable_or_bail, parse_required_or_bail,
    price::{Period, PeriodRange, Warning},
    required_field, required_field_or_bail, string,
    warning::{self, GatherWarnings as _, IntoCaveat as _},
    Ampere, Enum, IntoEnum, Kw, Kwh, ParseError, Price, ToDuration, Verdict,
};

/// The CDR object describes the Charging Session and its costs. How these costs are build up etc.
///
/// * See: [OCPI spec 2.2.1: CDR](<https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_cdrs.asciidoc>)
#[derive(Debug)]
pub struct Cdr {
    /// Start timestamp of the charging session.
    pub start_date_time: DateTime<Utc>,

    /// Stop timestamp of the charging session.
    pub end_date_time: DateTime<Utc>,

    /// List of charging periods that make up this charging session. A session should consist of 1 or
    /// more periods, where each period has a different relevant Tariff.
    pub charging_periods: Vec<ChargingPeriod>,

    pub totals: Totals,
}

#[derive(Debug)]
pub struct Totals {
    /// Total cost of this transaction.
    pub cost: Price,

    /// Total cost of the flat dimension.
    pub fixed_cost: Option<Price>,

    /// Total energy charged, in kWh.
    pub energy: Kwh,

    /// Total cost related to the energy dimension.
    pub energy_cost: Option<Price>,

    /// Total time charging, in hours
    pub time: TimeDelta,

    /// Total cost related to the charging time dimension.
    pub time_cost: Option<Price>,

    /// Total time not charging, in hours
    pub parking_time: Option<TimeDelta>,

    /// Total cost related to the parking time dimension.
    pub parking_cost: Option<Price>,

    /// Total cost related to reservation time.
    pub reservation_cost: Option<Price>,
}

/// The volume that has been consumed for a specific dimension during a charging period.
#[derive(Debug, Clone)]
pub(crate) struct Dimension {
    pub dimension_type: DimensionType,

    /// Volume of the dimension consumed, measured according to the dimension type.
    pub volume: Decimal,
}

/// The volume that has been consumed for a specific dimension during a charging period.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DimensionType {
    /// Consumed energy in `kWh`.
    Energy,

    /// The peak current, in 'A', during this period.
    MaxCurrent,

    /// The lowest current, in `A`, during this period.
    MinCurrent,

    /// The maximum power, in 'kW', reached during this period.
    MaxPower,

    /// The minimum power, in 'kW', reached during this period.
    MinPower,

    /// The parking time, in hours, consumed in this period.
    ParkingTime,

    /// The reservation time, in hours, consumed in this period.
    ReservationTime,

    /// The charging time, in hours, consumed in this period.
    Time,
}

/// A single charging period, containing a nonempty list of charge dimensions.
///
/// * See: [OCPI spec 2.2.1: CDR ChargingPeriod](<https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_cdrs.asciidoc#146-chargingperiod-class>)
#[derive(Clone, Debug)]
pub struct ChargingPeriod {
    /// Start timestamp of the charging period. This period ends when a next period starts, the
    /// last period ends when the session ends
    pub start_date_time: DateTime<Utc>,

    /// List of relevant values for this charging period
    pub dimensions: Vec<Dimension>,
}

/// The [`Cdr`] object describes the charging session and how costs are built up.
///
/// The `tariffs` array is split off from the [`Cdr`] as it's used apart from the rest of the fields.
///
/// * See: [OCPI spec 2.2.1: CDR](<https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_cdrs.asciidoc>)
pub struct WithTariffs<'buf> {
    /// The [`Cdr`] object describes the charging session and how costs are built up.
    ///
    /// * See: [OCPI spec 2.2.1: CDR](<https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_cdrs.asciidoc>)
    pub cdr: Cdr,

    /// List of relevant tariff elements.
    ///
    /// The tariffs have already been checked for unknown fields when the CDR JSON was parsed into
    /// a [`cdr::Versioned`](crate::cdr::Versioned).
    pub tariffs: Vec<json::Element<'buf>>,
}

impl WithTariffs<'_> {
    /// Discard the CDR's internal tariffs
    pub fn discard_tariffs(self) -> Cdr {
        self.cdr
    }
}

impl<'buf> json::FromJson<'buf> for Cdr {
    type Warning = Warning;

    /// Parse the JSON element into a structured CDR.
    ///
    /// Validate the CDR and sanitize the data so that the next phase can make presumptions about
    /// the data. Presumptions such as; the charging periods are sorted.
    ///
    /// Return warnings if the CDR isn't internally consistent.
    fn from_json(elem: &json::Element<'buf>) -> Verdict<Self, Self::Warning> {
        let warnings = warning::Set::<Warning>::new();

        let fields = expect_object_or_bail!(elem, warnings);
        let fields = fields.as_raw_map();

        cdr_from_fields(elem, &fields)
    }
}

impl<'buf> json::FromJson<'buf> for WithTariffs<'buf> {
    type Warning = Warning;

    /// Parse the JSON element into a structured CDR.
    ///
    /// Validate the CDR and sanitize the data so that the next phase can make presumptions about
    /// the data. Presumptions such as; the charging periods are sorted.
    ///
    /// Return warnings if the CDR isn't internally consistent.
    fn from_json(elem: &json::Element<'buf>) -> Verdict<Self, Self::Warning> {
        let mut warnings = warning::Set::<Warning>::new();

        let fields = expect_object_or_bail!(elem, warnings);
        let fields = fields.as_raw_map();

        let cdr = cdr_from_fields(elem, &fields)?.gather_warnings_into(&mut warnings);
        let tariffs_elem = fields.get("tariffs");

        let tariffs = if let Some(elem) = tariffs_elem {
            let tariffs = expect_array_or_bail!(elem, warnings);
            tariffs.to_vec()
        } else {
            vec![]
        };

        Ok(WithTariffs { cdr, tariffs }.into_caveat(warnings))
    }
}

fn cdr_from_fields<'buf>(
    elem: &json::Element<'buf>,
    fields: &json::RawRefMap<'_, 'buf>,
) -> Verdict<Cdr, Warning> {
    let mut warnings = warning::Set::<Warning>::new();

    {
        // We don't use the `country_code`, `currency` or `party_id` while pricing
        // but there's no harm in warning the user about issues with them.
        if let Some(elem) = required_field!(elem, fields, "country_code", warnings) {
            let code_set = country::CodeSet::from_json(elem).gather_warnings_into(&mut warnings)?;

            if let country::CodeSet::Alpha3(_) = code_set {
                warnings.insert(Warning::CountryShouldBeAlpha2, elem);
            }
        }
        if let Some(elem) = required_field!(elem, fields, "currency", warnings) {
            let _ignore_value = currency::Code::from_json(elem).gather_warnings_into(&mut warnings);
        }

        if let Some(elem) = required_field!(elem, fields, "party_id", warnings) {
            let _ignore_value =
                string::CiExactLen::<'_, 3>::from_json(elem).gather_warnings_into(&mut warnings);
        }
    }

    let start_date_time =
        parse_required_or_bail!(elem, fields, "start_date_time", DateTime<Utc>, warnings);
    let end_date_time =
        parse_required_or_bail!(elem, fields, "end_date_time", DateTime<Utc>, warnings);
    let total_cost = parse_required_or_bail!(elem, fields, "total_cost", Price, warnings);
    let total_energy = parse_required_or_bail!(elem, fields, "total_energy", Kwh, warnings);
    let total_time = parse_required_or_bail!(elem, fields, "total_time", Decimal, warnings);

    let charging_periods_elem = required_field_or_bail!(elem, fields, "charging_periods", warnings);
    let charging_periods = expect_array_or_bail!(charging_periods_elem, warnings)
        .iter()
        .map(ChargingPeriod::from_json)
        .collect::<Result<Vec<_>, _>>()?;

    let mut charging_periods = charging_periods.gather_warnings_into(&mut warnings);

    if charging_periods.is_empty() {
        return warnings.bail(Warning::NoPeriods, charging_periods_elem);
    }

    let total_parking_time =
        parse_nullable_or_bail!(fields, "total_parking_time", Decimal, warnings);
    let total_fixed_cost = parse_nullable_or_bail!(fields, "total_fixed_cost", Price, warnings);
    let total_energy_cost = parse_nullable_or_bail!(fields, "total_energy_cost", Price, warnings);
    let total_time_cost = parse_nullable_or_bail!(fields, "total_time_cost", Price, warnings);
    let total_parking_cost = parse_nullable_or_bail!(fields, "total_parking_cost", Price, warnings);
    let total_reservation_cost =
        parse_nullable_or_bail!(fields, "total_reservation_cost", Price, warnings);

    let cdr_range = start_date_time..end_date_time;
    charging_periods.sort_unstable_by_key(|p| p.start_date_time);

    match charging_periods.as_slice() {
        [] => (),
        [period] => {
            if !cdr_range.contains(&period.start_date_time) {
                warnings.insert(
                    Warning::PeriodsOutsideStartEndDateTime {
                        cdr_range,
                        period_range: PeriodRange::Single(period.start_date_time),
                    },
                    elem,
                );
            }
        }
        [period_earliest, .., period_latest] => {
            let period_range = period_earliest.start_date_time..period_latest.start_date_time;

            if !(cdr_range.contains(&period_range.start) && cdr_range.contains(&period_range.end)) {
                warnings.insert(
                    Warning::PeriodsOutsideStartEndDateTime {
                        cdr_range,
                        period_range: PeriodRange::Many(period_range),
                    },
                    elem,
                );
            }
        }
    }

    let cdr = Cdr {
        start_date_time,
        end_date_time,
        charging_periods,
        totals: Totals {
            cost: total_cost,
            fixed_cost: total_fixed_cost,
            energy: total_energy,
            energy_cost: total_energy_cost,
            time: total_time.to_duration(),
            time_cost: total_time_cost,
            parking_time: total_parking_time.map(|d| d.to_duration()),
            parking_cost: total_parking_cost,
            reservation_cost: total_reservation_cost,
        },
    };

    Ok(cdr.into_caveat(warnings))
}

impl json::FromJson<'_> for ChargingPeriod {
    type Warning = Warning;

    fn from_json(elem: &'_ json::Element<'_>) -> Verdict<Self, Self::Warning> {
        let mut warnings = warning::Set::<Warning>::new();
        let fields = expect_object_or_bail!(elem, warnings);
        let fields = fields.as_raw_map();

        let start_date_time_elem =
            required_field_or_bail!(elem, fields, "start_date_time", warnings);
        let start_date_time =
            DateTime::from_json(start_date_time_elem)?.gather_warnings_into(&mut warnings);

        let dimensions_elem = required_field_or_bail!(elem, fields, "dimensions", warnings);

        // If the `DimensionType` of a `Dimension` is unknown the entire `Dimension` will be returned as `None`.
        // There can still be Warnings generated from the `Ok` and `Err` paths from the `from_json` call.
        // Warnings in the `Err` path will cause an early return.
        let dimensions = expect_array_or_bail!(dimensions_elem, warnings)
            .iter()
            .map(Option::<Dimension>::from_json)
            .collect::<Result<Vec<_>, _>>()?;

        // This collection has all Results resolved but still might contain Dimensions with
        // unknown `DimensionType`s. We gather up these Warnings and flatten the `None` Dimensions.
        // This leaves us with a collection of `Dimensions` where the `DimensionType` is known.
        let dimensions = dimensions
            .gather_warnings_into(&mut warnings)
            .into_iter()
            .flatten()
            .collect();

        let elem = Self {
            start_date_time,
            dimensions,
        };

        Ok(elem.into_caveat(warnings))
    }
}

impl json::FromJson<'_> for Option<Dimension> {
    type Warning = Warning;

    fn from_json(elem: &'_ json::Element<'_>) -> Verdict<Self, Self::Warning> {
        let mut warnings = warning::Set::<Warning>::new();
        let fields = expect_object_or_bail!(elem, warnings);
        let fields = fields.as_raw_map();

        let type_elem = required_field_or_bail!(elem, fields, "type", warnings);
        let dimension_type =
            Enum::<DimensionType>::from_json(type_elem)?.gather_warnings_into(&mut warnings);

        let dimension_type = match dimension_type {
            Enum::Known(v) => v,
            Enum::Unknown(s) => {
                warnings.insert(
                    Warning::field_invalid_value(s,
                        "A CDR DimensionType should be one of `ENERGY`, `MAX_CURRENT`, `MIN_CURRENT`, `MAX_POWER`, `MIN_POWER`, `PARKING_TIME`, `RESERVATION_TIME` or `TIME`"
                    ),
                    type_elem
                );
                return Ok(None.into_caveat(warnings));
            }
        };

        let volume_elem = required_field_or_bail!(elem, fields, "volume", warnings);
        let volume = Decimal::from_json(volume_elem)?.gather_warnings_into(&mut warnings);

        let dimension = Dimension {
            dimension_type,
            volume,
        };

        Ok(Some(dimension).into_caveat(warnings))
    }
}

impl IntoEnum for DimensionType {
    fn enum_from_str(s: &str) -> Enum<DimensionType> {
        let dt = if s.eq_ignore_ascii_case("energy") {
            Self::Energy
        } else if s.eq_ignore_ascii_case("max_current") {
            Self::MaxCurrent
        } else if s.eq_ignore_ascii_case("min_current") {
            Self::MinCurrent
        } else if s.eq_ignore_ascii_case("max_power") {
            Self::MaxPower
        } else if s.eq_ignore_ascii_case("min_power") {
            Self::MinPower
        } else if s.eq_ignore_ascii_case("parking_time") {
            Self::ParkingTime
        } else if s.eq_ignore_ascii_case("reservation_time") {
            Self::ReservationTime
        } else if s.eq_ignore_ascii_case("time") {
            Self::Time
        } else {
            return Enum::Unknown(s.to_string());
        };

        Enum::Known(dt)
    }
}

impl json::FromJson<'_> for Enum<DimensionType> {
    type Warning = Warning;

    fn from_json(elem: &'_ json::Element<'_>) -> Verdict<Self, Self::Warning> {
        let mut warnings = warning::Set::new();

        let Some(s) = elem.to_raw_str() else {
            return warnings.bail(
                Warning::FieldInvalidType {
                    expected_type: json::ValueKind::String,
                },
                elem,
            );
        };

        let s = s.decode_escapes(elem).gather_warnings_into(&mut warnings);
        let dt = DimensionType::enum_from_str(&s);
        Ok(dt.into_caveat(warnings))
    }
}

impl TryFrom<ChargingPeriod> for Period {
    type Error = ParseError;

    fn try_from(period: ChargingPeriod) -> Result<Self, Self::Error> {
        let ChargingPeriod {
            start_date_time,
            dimensions,
        } = period;
        let mut consumed = Consumed {
            current_max: None,
            current_min: None,
            duration_charging: None,
            duration_parking: None,
            energy: None,
            power_max: None,
            power_min: None,
        };

        for dimension in dimensions {
            let Dimension {
                dimension_type,
                volume,
            } = dimension;

            match dimension_type {
                DimensionType::MinCurrent => {
                    consumed.current_min = Some(Ampere::from_decimal(volume));
                }
                DimensionType::MaxCurrent => {
                    consumed.current_max = Some(Ampere::from_decimal(volume));
                }
                DimensionType::MaxPower => {
                    consumed.power_max = Some(Kw::from_decimal(volume));
                }
                DimensionType::MinPower => {
                    consumed.power_min = Some(Kw::from_decimal(volume));
                }
                DimensionType::Energy => {
                    consumed.energy = Some(Kwh::from_decimal(volume));
                }
                DimensionType::Time => {
                    consumed.duration_charging = Some(volume.to_duration());
                }
                DimensionType::ParkingTime => {
                    consumed.duration_parking = Some(volume.to_duration());
                }
                DimensionType::ReservationTime => {
                    // The pricer does not use reservation time.
                }
            }
        }

        Ok(Period {
            start_date_time,
            consumed,
        })
    }
}