Skip to content

Commit

Permalink
Fix [min|max]_duration restriction in combination with `PARKING_TIM…
Browse files Browse the repository at this point in the history
…E` (#53)

* fix restrictions and add test

* sum up duration
  • Loading branch information
remkop22 authored Jul 5, 2023
1 parent fd39ac5 commit c26006b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ocpi-tariffs/src/pricer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl StepSize {
}
}

if period.period_data.duration.is_some() {
if period.period_data.charging_duration.is_some() {
if let Some(time) = components.time {
self.time = Some((index, time));
}
Expand Down Expand Up @@ -373,7 +373,7 @@ impl Dimensions {
components.parking,
data.parking_duration.map(Into::into),
),
time: DimensionReport::new(components.time, data.duration.map(Into::into)),
time: DimensionReport::new(components.time, data.charging_duration.map(Into::into)),
energy: DimensionReport::new(components.energy, data.energy),
flat: DimensionReport::new(components.flat, Some(())),
}
Expand Down
13 changes: 8 additions & 5 deletions ocpi-tariffs/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct PeriodData {
pub min_current: Option<Ampere>,
pub max_power: Option<Kw>,
pub min_power: Option<Kw>,
pub duration: Option<Duration>,
pub charging_duration: Option<Duration>,
pub parking_duration: Option<Duration>,
pub reservation_duration: Option<Duration>,
pub energy: Option<Kwh>,
Expand All @@ -99,6 +99,7 @@ pub struct PeriodData {
pub struct InstantData {
local_timezone: Tz,
pub date_time: DateTime,
pub total_charging_duration: Duration,
pub total_duration: Duration,
pub total_energy: Kwh,
}
Expand All @@ -108,6 +109,7 @@ impl InstantData {
Self {
date_time,
local_timezone,
total_charging_duration: Duration::zero(),
total_duration: Duration::zero(),
total_energy: Kwh::zero(),
}
Expand All @@ -116,10 +118,11 @@ impl InstantData {
fn next(&self, state: &PeriodData, date_time: DateTime) -> Self {
let mut next = self.clone();

next.total_duration = next.total_duration + (date_time - next.date_time);
next.date_time = date_time;

if let Some(duration) = state.duration {
next.total_duration = next.total_duration + duration;
if let Some(duration) = state.charging_duration {
next.total_charging_duration = next.total_charging_duration + duration;
}

if let Some(energy) = state.energy {
Expand Down Expand Up @@ -153,7 +156,7 @@ impl PeriodData {
min_current: None,
max_power: None,
min_power: None,
duration: None,
charging_duration: None,
energy: None,
};

Expand All @@ -165,7 +168,7 @@ impl PeriodData {
OcpiCdrDimension::MinPower(volume) => inst.min_power = Some(volume),
OcpiCdrDimension::Energy(volume) => inst.energy = Some(volume),
OcpiCdrDimension::Time(volume) => {
inst.duration = Some(volume.into());
inst.charging_duration = Some(volume.into());
}
OcpiCdrDimension::ParkingTime(volume) => {
inst.parking_duration = Some(volume.into());
Expand Down
4 changes: 2 additions & 2 deletions ocpi-tariffs/src/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'de> Deserialize<'de> for HoursDecimal {
{
use serde::de::Error;

let hours = <Number as serde::Deserialize>::deserialize(deserializer)?;
let hours = Number::deserialize(deserializer)?;
let duration =
Self::from_hours_decimal(hours).map_err(|_e| D::Error::custom("overflow"))?;
Ok(duration)
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<'de> Deserialize<'de> for SecondsRound {
{
use serde::de::Error;

let seconds = <u64 as serde::Deserialize>::deserialize(deserializer)?;
let seconds = u64::deserialize(deserializer)?;
let duration = Duration::seconds(
seconds
.try_into()
Expand Down
44 changes: 44 additions & 0 deletions ocpi-tariffs/test_data/grace_period_parking_time/cdr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"country_code": "BE",
"start_date_time": "2015-06-29T15:00:00Z",
"end_date_time": "2015-06-29T16:15:00Z",
"currency": "EUR",
"tariffs": [],
"charging_periods": [{
"start_date_time": "2015-06-29T15:00:00Z",
"dimensions": [{
"type": "PARKING_TIME",
"volume": 0.5
}]
}, {
"start_date_time": "2015-06-29T15:30:00Z",
"dimensions": [{
"type": "PARKING_TIME",
"volume": 0.25
}]
}, {
"start_date_time": "2015-06-29T15:45:00Z",
"dimensions": [{
"type": "ENERGY",
"volume": 10
}]
}],
"total_parking_cost": {
"excl_vat": 0.75,
"incl_vat": 0.75
},
"total_energy_cost": {
"excl_vat": 2.5,
"incl_vat": 2.75
},
"total_cost": {
"excl_vat": 3.25,
"incl_vat": 3.5
},
"total_energy": 10,
"total_time": 1.25,
"total_parking_time": 0.75,
"last_updated": "2015-06-29T22:01:13Z"
}


44 changes: 44 additions & 0 deletions ocpi-tariffs/test_data/grace_period_parking_time/tariff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"currency": "EUR",
"elements": [
{
"price_components": [
{
"type": "PARKING_TIME",
"price": 0.0,
"vat": 0.0,
"step_size": 1
}
],
"restrictions": {
"min_duration": 0,
"max_duration": 1800
}
},
{
"price_components": [
{
"type": "PARKING_TIME",
"price": 3.0,
"vat": 0.0,
"step_size": 1
}
],
"restrictions": {
"min_duration": 1800
}
},
{
"price_components": [
{
"type": "ENERGY",
"price": 0.25,
"vat": 10.0,
"step_size": 1
}
]
}
],
"end_date_time": "2019-06-30T23:59:59Z",
"last_updated": "2018-12-17T17:15:01Z"
}

0 comments on commit c26006b

Please sign in to comment.