grid_tariffs/
power_tariffs.rs1use chrono::DateTime;
2use chrono_tz::Tz;
3use serde::Serialize;
4
5use crate::{
6 costs::{CostPeriods, LoadType},
7 money::Money,
8};
9
10#[derive(Debug, Clone, Serialize)]
11#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
12#[serde(tag = "type", content = "value")]
13pub enum PowerTariff {
14 Unverified,
15 NotImplemented,
16 Implemented {
17 method: TariffCalculationMethod,
18 #[serde(flatten)]
19 periods: CostPeriods,
20 },
21}
22
23impl PowerTariff {
24 pub const fn is_unverified(&self) -> bool {
25 matches!(self, Self::Unverified)
26 }
27
28 pub(super) const fn new(method: TariffCalculationMethod, periods: CostPeriods) -> Self {
29 Self::Implemented { method, periods }
30 }
31
32 pub(super) fn kw_cost(
33 &self,
34 timestamp: DateTime<Tz>,
35 fuse_size: u16,
36 yearly_consumption: u32,
37 ) -> Money {
38 let cost = Money::ZERO;
39 match self {
40 PowerTariff::Unverified | PowerTariff::NotImplemented => cost,
41 PowerTariff::Implemented { method, periods } => {
42 for period in periods.iter() {
43 let money = period.cost().cost_for(fuse_size, yearly_consumption);
44 }
45 cost
46 }
47 }
48 }
49}
50
51#[derive(Debug, Clone, Copy, Serialize)]
53#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
54pub enum TariffCalculationMethod {
55 AverageDays(u8),
57 AverageHours(u8),
59 AverageDaysDifferentiated {
61 peak: u8,
62 base: u8,
63 },
64 PeakHour,
66 PeakHours(&'static [LoadType]),
68 AverageDayNightDifferentiated {
71 day: i32,
72 night: i32,
73 },
74}
75
76impl TariffCalculationMethod {
77 pub(super) fn relevant_samples(
78 &self,
79 grid_consumption: Vec<GridConsumption>,
80 ) -> Vec<GridConsumption> {
81 todo!()
109 }
110}
111
112#[derive(Clone, Copy)]
113pub(super) struct HourPower(DateTime<Tz>, u32);
114
115impl HourPower {
116 fn timestamp(&self) -> DateTime<Tz> {
117 self.0
118 }
119
120 fn watts(&self) -> u32 {
121 self.1
122 }
123}
124
125pub struct Peak {
128 time_period: (DateTime<Tz>, DateTime<Tz>),
130 current_max_w: u32,
131 cost_per_kw: Money,
132 kw_divider: u8,
133}
134
135impl Peak {
136 pub fn contains(&self, timestamp: DateTime<Tz>) -> bool {
141 timestamp >= self.time_period.0 && timestamp < self.time_period.1
142 }
143}
144
145impl PowerTariff {
146 pub fn get_peaks(
147 &self,
148 time_period: (DateTime<Tz>, DateTime<Tz>),
149 grid_consumption: Vec<GridConsumption>,
150 ) -> Option<Vec<Peak>> {
151 let Self::Implemented { method, periods } = self else {
152 return None;
153 };
154 let _samples = method.relevant_samples(grid_consumption);
155 todo!()
156 }
157}
158
159#[derive(Debug, Clone, Copy)]
160pub struct GridConsumption {
161 timestamp: DateTime<Tz>,
162 wh: u32,
163}