ocpi-tariffs 0.43.0

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
use std::collections::HashSet;

use tracing::trace;

use chrono::{DateTime, Duration, NaiveDate, NaiveTime, Timelike, Utc, Weekday};

use crate::{
    currency,
    json::FromJson as _,
    price::{self, Consumed, PeriodNormalized},
    warning::VerdictExt as _,
    Ampere, Kw, Kwh, Verdict, Version, Versioned as _,
};

use super::TotalsSnapshot;

pub(super) fn parse<'caller: 'buf, 'buf>(
    tariff: &'caller crate::tariff::Versioned<'buf>,
) -> Verdict<crate::tariff::v221::Tariff<'buf>, crate::tariff::Warning> {
    match tariff.version() {
        Version::V211 => {
            let tariff = crate::tariff::v211::Tariff::from_json(tariff.as_element());
            tariff.map_caveat(crate::tariff::v221::Tariff::from)
        }
        Version::V221 => crate::tariff::v221::Tariff::from_json(tariff.as_element()),
    }
}

/// A normalized tariff that doesn't correspond to any particular OCPI version.
///
/// The normalized version is based off of the v221 version of a tariff.
///
/// * See: [OCPI spec 2.2.1: Tariff](<https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_tariffs.asciidoc#131-tariff-object>)
#[derive(Debug)]
pub(crate) struct Tariff {
    /// Uniquely identifies the tariff within the CPO's platform (and sub-operator platforms).
    id: String,

    /// ISO-4217 code of the currency of this tariff.
    currency: currency::Code,

    /// List of tariff elements.
    elements: Vec<Element>,

    /// The time when this tariff becomes active.
    ///
    /// In UTC, `timezone` field of the `Location` can be used to convert to local time.
    /// Typically used for a new tariff that is already given with the location,
    /// before it becomes active.
    start_date_time: Option<DateTime<Utc>>,

    /// The time after which this tariff is no longer valid.
    ///
    /// In UTC, `time_zone` field of the `Location` can be used to convert to local time.
    /// Typically used when this tariff is going to be replaced with a new tariff in the near future.
    end_date_time: Option<DateTime<Utc>>,
}

impl Tariff {
    /// Create a new normalized tariff from a v221 Tariff.
    pub(super) fn from_v221(tariff: &crate::tariff::v221::Tariff<'_>) -> Self {
        let crate::tariff::v221::Tariff {
            id,
            elements,
            start_date_time,
            end_date_time,
            country_code: _,
            party_id: _,
            currency,
            min_price: _,
            max_price: _,
        } = tariff;

        let elements = elements
            .iter()
            .enumerate()
            .map(|(element_index, element)| Element::new(element, element_index))
            .collect();

        Self {
            id: id.to_string(),
            currency: *currency,
            start_date_time: *start_date_time,
            end_date_time: *end_date_time,
            elements,
        }
    }

    pub(super) fn id(&self) -> &str {
        &self.id
    }

    pub(super) fn currency(&self) -> currency::Code {
        self.currency
    }

    /// Returns the set of Price Components that are active during the given `ChargePeriod`.
    ///
    /// When the list of Tariff Elements contains more than one Element that has a Price Component
    /// for a certain dimension. Then the first Tariff Element with a Price Component for that
    /// dimension in the list with matching Tariff Restrictions will be used.
    ///
    /// Only one Price Component per dimension can be active at any point in time, but multiple
    /// Price Components for different dimensions can be active at once. That is you can have an
    /// ENERGY component and a TIME component active at the same time. But only those ones that
    /// are in the first Tariff Element that has a Price Component for that dimension and that has
    /// restrictions that match at that time.
    ///
    /// When no Tariff Element with a specific Dimension is found for which the Restrictions match,
    /// and there is no Tariff Element in the list with the given Dimension without Restrictions,
    /// there will be no costs for that Tariff Dimension.
    ///
    /// It is advised to always add a "default" Price Component per dimension. This can be achieved
    /// by adding a Tariff Element without restrictions after all other occurrences of the same
    /// dimension in the list of Tariff Elements.
    ///
    /// Such a Tariff Element will act as fallback when there is no other Tariff Element that has
    /// matching restrictions and that contains a Price Component for that dimension.
    ///
    /// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_tariffs.asciidoc#131-tariff-object>
    pub(super) fn active_components(&self, period: &PeriodNormalized) -> price::ComponentSet {
        let mut component_set = price::ComponentSet {
            energy: None,
            flat: None,
            duration_charging: None,
            duration_parking: None,
        };

        for tariff_element in &self.elements {
            trace!("{period:#?}");

            if !tariff_element.is_active(period) {
                continue;
            }

            if component_set.duration_charging.is_none() {
                component_set
                    .duration_charging
                    .clone_from(&tariff_element.components.duration_charging);
            }

            if component_set.duration_parking.is_none() {
                component_set
                    .duration_parking
                    .clone_from(&tariff_element.components.duration_parking);
            }

            if component_set.energy.is_none() {
                component_set
                    .energy
                    .clone_from(&tariff_element.components.energy);
            }

            if component_set.flat.is_none() {
                component_set
                    .flat
                    .clone_from(&tariff_element.components.flat);
            }

            if component_set.has_all_components() {
                break;
            }
        }

        component_set
    }

    fn is_active(&self, start_time: DateTime<Utc>) -> bool {
        let is_after_start = self
            .start_date_time
            .map(|s| start_time >= s)
            .unwrap_or(true);
        let is_before_end = self.end_date_time.map(|s| start_time < s).unwrap_or(true);

        is_after_start && is_before_end
    }
}

/// A Tariff Element is a group of Price Components that share a set of restrictions under which they apply.
///
/// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_tariffs.asciidoc#144-tariffelement-class>
#[derive(Debug)]
pub(super) struct Element {
    /// List of Price Components that each describe how a certain dimension is priced.
    components: price::ComponentSet,

    /// Restrictions that describe under which circumstances the Price Components of this Tariff Element apply.
    restrictions: Vec<Restriction>,
}

impl Element {
    fn new(element: &crate::tariff::v221::Element, element_index: usize) -> Self {
        let restrictions = if let Some(restrictions) = &element.restrictions {
            collect_restrictions(restrictions)
        } else {
            Vec::new()
        };

        let mut components = price::ComponentSet {
            energy: None,
            flat: None,
            duration_charging: None,
            duration_parking: None,
        };

        for component in &element.price_components {
            let price_component = price::Component::new(component, element_index);

            match component.dimension_type {
                crate::tariff::v2x::DimensionType::Flat => {
                    components.flat.get_or_insert(price_component)
                }
                crate::tariff::v2x::DimensionType::Time => {
                    components.duration_charging.get_or_insert(price_component)
                }
                crate::tariff::v2x::DimensionType::ParkingTime => {
                    components.duration_parking.get_or_insert(price_component)
                }
                crate::tariff::v2x::DimensionType::Energy => {
                    components.energy.get_or_insert(price_component)
                }
            };
        }

        Self {
            components,
            restrictions,
        }
    }

    fn is_active(&self, period: &PeriodNormalized) -> bool {
        for restriction in &self.restrictions {
            if !restriction.snapshot_validity_exclusive(&period.start_snapshot) {
                return false;
            }

            if !restriction.period_validity(&period.consumed) {
                return false;
            }
        }

        true
    }

    #[expect(dead_code, reason = "pending use in linter")]
    fn is_active_at_end(&self, period: &PeriodNormalized) -> bool {
        for restriction in &self.restrictions {
            if !restriction.snapshot_validity_inclusive(&period.end_snapshot) {
                return false;
            }
        }

        true
    }
}

fn collect_restrictions(restriction: &crate::tariff::v221::Restrictions) -> Vec<Restriction> {
    let mut collected = Vec::new();

    match (restriction.start_time, restriction.end_time) {
        (Some(start_time), Some(end_time)) if end_time < start_time => {
            collected.push(Restriction::WrappingTime {
                start_time,
                end_time,
            });
        }
        (start_time, end_time) => {
            if let Some(start_time) = start_time {
                collected.push(Restriction::StartTime(start_time));
            }

            if let Some(end_time) = end_time {
                collected.push(Restriction::EndTime(end_time));
            }
        }
    }

    if let Some(start_date) = restriction.start_date {
        collected.push(Restriction::StartDate(start_date));
    }

    if let Some(end_date) = restriction.end_date {
        collected.push(Restriction::EndDate(end_date));
    }

    if let Some(min_kwh) = restriction.min_kwh {
        collected.push(Restriction::MinKwh(min_kwh));
    }

    if let Some(max_kwh) = restriction.max_kwh {
        collected.push(Restriction::MaxKwh(max_kwh));
    }

    if let Some(min_current) = restriction.min_current {
        collected.push(Restriction::MinCurrent(min_current));
    }

    if let Some(max_current) = restriction.max_current {
        collected.push(Restriction::MaxCurrent(max_current));
    }

    if let Some(min_power) = restriction.min_power {
        collected.push(Restriction::MinPower(min_power));
    }

    if let Some(max_power) = restriction.max_power {
        collected.push(Restriction::MaxPower(max_power));
    }

    if let Some(min_duration) = restriction.min_duration {
        collected.push(Restriction::MinDuration(min_duration));
    }

    if let Some(max_duration) = restriction.max_duration {
        collected.push(Restriction::MaxDuration(max_duration));
    }

    if let Some(day_of_week) = restriction.day_of_week.as_deref() {
        if !day_of_week.is_empty() {
            collected.push(Restriction::Weekday(
                day_of_week.iter().copied().map(Into::into).collect(),
            ));
        }
    }

    collected
}

/// A `TariffRestrictions` object describes if and when a Tariff Element becomes active or inactive during a Charging Session.
///
/// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_tariffs.asciidoc#146-tariffrestrictions-class>
#[derive(Debug, Clone)]
enum Restriction {
    StartTime(NaiveTime),
    EndTime(NaiveTime),
    WrappingTime {
        start_time: NaiveTime,
        end_time: NaiveTime,
    },
    StartDate(NaiveDate),
    EndDate(NaiveDate),
    MinKwh(Kwh),
    MaxKwh(Kwh),
    MinCurrent(Ampere),
    MaxCurrent(Ampere),
    MinPower(Kw),
    MaxPower(Kw),
    MinDuration(Duration),
    MaxDuration(Duration),
    Weekday(HashSet<Weekday>),
}

impl Restriction {
    /// Returns true if this restriction is valid at time of the given [`TotalsSnapshot`].
    ///
    /// The time based restrictions are treated as exclusive comparisons.
    fn snapshot_validity_exclusive(&self, snapshot: &TotalsSnapshot) -> bool {
        match self {
            &Self::WrappingTime {
                start_time,
                end_time,
            } => snapshot.local_time() >= start_time || snapshot.local_time() < end_time,
            &Self::StartTime(start_time) => snapshot.local_time() >= start_time,
            &Self::EndTime(end_time) => snapshot.local_time() < end_time,
            &Self::StartDate(start_date) => snapshot.local_date() >= start_date,
            &Self::EndDate(end_date) => snapshot.local_date() < end_date,
            &Self::MinKwh(min_energy) => snapshot.energy >= min_energy,
            &Self::MaxKwh(max_energy) => snapshot.energy < max_energy,
            &Self::MinDuration(min_duration) => snapshot.duration_total >= min_duration,
            &Self::MaxDuration(max_duration) => snapshot.duration_total < max_duration,
            Self::Weekday(days) => days.contains(&snapshot.local_weekday()),
            Self::MinCurrent(_) | Self::MaxCurrent(_) | Self::MinPower(_) | Self::MaxPower(_) => {
                true
            }
        }
    }

    /// Returns true if this restriction is valid at the time of the given [`TotalsSnapshot`].
    ///
    /// The time based restrictions are treated as inclusive comparisons. For example, a snapshot
    /// at 00:00 on a Tuesday is regarded as valid for a restriction that has a `Weekday` which
    /// includes Monday.
    fn snapshot_validity_inclusive(&self, snapshot: &TotalsSnapshot) -> bool {
        match self {
            &Self::WrappingTime {
                start_time,
                end_time,
            } => snapshot.local_time() >= start_time || snapshot.local_time() < end_time,
            &Self::EndTime(end_time) => snapshot.local_time() <= end_time,
            &Self::EndDate(end_date) => {
                // Since the end date of this period is derived from the start date of the next period
                // we can't do an exclusive comparison. But we should still check that this period doesn't
                // end in the middle of the day on `end_date`.

                let is_before_end_date = snapshot.local_date() < end_date;
                let is_on_end_date = snapshot.local_date() == end_date;
                let is_at_midnight = snapshot.local_time().num_seconds_from_midnight() == 0;

                is_before_end_date || (is_on_end_date && is_at_midnight)
            }
            &Self::MinKwh(min_energy) => snapshot.energy >= min_energy,
            &Self::MaxKwh(max_energy) => snapshot.energy < max_energy,
            &Self::MinDuration(min_duration) => snapshot.duration_total >= min_duration,
            &Self::MaxDuration(max_duration) => snapshot.duration_total < max_duration,
            Self::Weekday(days) => {
                let includes_weekday = days.contains(&snapshot.local_weekday());
                let includes_day_before = days.contains(&snapshot.local_weekday().pred());
                let is_at_midnight = snapshot.local_time().num_seconds_from_midnight() == 0;
                includes_weekday || (includes_day_before && is_at_midnight)
            }
            _ => true,
        }
    }

    /// Checks if this restriction is valid for `state`.
    fn period_validity(&self, consumed: &Consumed) -> bool {
        match *self {
            Self::MinCurrent(min_current) => consumed
                .current_min
                .map(|current| current >= min_current)
                .unwrap_or(true),
            Self::MaxCurrent(max_current) => consumed
                .current_max
                .map(|current| current < max_current)
                .unwrap_or(true),
            Self::MinPower(min_power) => consumed
                .power_min
                .map(|power| power >= min_power)
                .unwrap_or(true),
            Self::MaxPower(max_power) => consumed
                .power_max
                .map(|power| power < max_power)
                .unwrap_or(true),
            _ => true,
        }
    }
}

/// Find the first active tariff and return it and it's index.
pub(super) fn find_first_active(
    tariffs: Vec<Tariff>,
    start_date_time: DateTime<Utc>,
) -> Option<(usize, Tariff)> {
    tariffs
        .into_iter()
        .enumerate()
        .find(|(_, t)| t.is_active(start_date_time))
}

/// Return a normalized `Tariff` for each `v221::Tariff` given.
///
/// The parsing/deserialization of JSON to a `v221::Tariff` may result in some `UnexpectedFields`.
/// And the creation of the normalized `Tariff` object may result in `Warning`s.
pub(super) fn normalize_all(tariffs: &[crate::tariff::v221::Tariff<'_>]) -> Vec<Tariff> {
    tariffs.iter().map(Tariff::from_v221).collect()
}