Skip to main content

bo4e_core/com/
aggregated_value.rs

1//! Aggregated value (Aggregiertwert) component.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::Unit;
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9/// An aggregated value computed from multiple source values.
10///
11/// German: Aggregiertwert
12///
13/// # Example
14///
15/// ```rust
16/// use bo4e_core::com::AggregatedValue;
17/// use bo4e_core::enums::Unit;
18/// use chrono::Utc;
19///
20/// let value = AggregatedValue {
21///     timestamp: Some(Utc::now()),
22///     value: Some(15000.0),
23///     unit: Some(Unit::KilowattHour),
24///     aggregation_method: Some("SUM".to_string()),
25///     ..Default::default()
26/// };
27/// ```
28#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
29#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
30#[cfg_attr(feature = "json-schema", schemars(rename = "Aggregiertwert"))]
31#[serde(rename_all = "camelCase")]
32pub struct AggregatedValue {
33    /// BO4E metadata
34    #[serde(flatten)]
35    pub meta: Bo4eMeta,
36
37    /// Timestamp for the aggregated value (Zeitpunkt)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[cfg_attr(feature = "json-schema", schemars(rename = "zeitpunkt"))]
40    pub timestamp: Option<DateTime<Utc>>,
41
42    /// The aggregated value (Wert)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
45    pub value: Option<f64>,
46
47    /// Unit of the value (Einheit)
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[cfg_attr(feature = "json-schema", schemars(rename = "einheit"))]
50    pub unit: Option<Unit>,
51
52    /// Aggregation method (Aggregationsmethode)
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[cfg_attr(feature = "json-schema", schemars(rename = "aggregationsmethode"))]
55    pub aggregation_method: Option<String>,
56
57    /// Period start for aggregation (Periodenbeginn)
58    #[serde(skip_serializing_if = "Option::is_none")]
59    #[cfg_attr(feature = "json-schema", schemars(rename = "periodenbeginn"))]
60    pub period_start: Option<DateTime<Utc>>,
61
62    /// Period end for aggregation (Periodenende)
63    #[serde(skip_serializing_if = "Option::is_none")]
64    #[cfg_attr(feature = "json-schema", schemars(rename = "periodenende"))]
65    pub period_end: Option<DateTime<Utc>>,
66
67    /// Number of source values aggregated (Anzahl Quellwerte)
68    #[serde(skip_serializing_if = "Option::is_none")]
69    #[cfg_attr(feature = "json-schema", schemars(rename = "anzahlQuellwerte"))]
70    pub source_count: Option<i32>,
71
72    /// OBIS code (OBIS-Kennzahl)
73    #[serde(skip_serializing_if = "Option::is_none")]
74    #[cfg_attr(feature = "json-schema", schemars(rename = "obisKennzahl"))]
75    pub obis_code: Option<String>,
76}
77
78impl Bo4eObject for AggregatedValue {
79    fn type_name_german() -> &'static str {
80        "Aggregiertwert"
81    }
82
83    fn type_name_english() -> &'static str {
84        "AggregatedValue"
85    }
86
87    fn meta(&self) -> &Bo4eMeta {
88        &self.meta
89    }
90
91    fn meta_mut(&mut self) -> &mut Bo4eMeta {
92        &mut self.meta
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use chrono::TimeZone;
100
101    #[test]
102    fn test_aggregated_value() {
103        let value = AggregatedValue {
104            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap()),
105            value: Some(15000.0),
106            unit: Some(Unit::KilowattHour),
107            aggregation_method: Some("SUM".to_string()),
108            source_count: Some(31),
109            ..Default::default()
110        };
111
112        let json = serde_json::to_string(&value).unwrap();
113        assert!(json.contains("15000"));
114        assert!(json.contains("SUM"));
115    }
116
117    #[test]
118    fn test_average_aggregation() {
119        let start = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
120        let end = Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap();
121
122        let value = AggregatedValue {
123            value: Some(125.5),
124            aggregation_method: Some("AVERAGE".to_string()),
125            period_start: Some(start),
126            period_end: Some(end),
127            source_count: Some(2976),
128            ..Default::default()
129        };
130
131        let json = serde_json::to_string(&value).unwrap();
132        assert!(json.contains("AVERAGE"));
133        assert!(json.contains("2976"));
134    }
135
136    #[test]
137    fn test_roundtrip() {
138        let value = AggregatedValue {
139            timestamp: Some(Utc::now()),
140            value: Some(999.99),
141            aggregation_method: Some("MAX".to_string()),
142            ..Default::default()
143        };
144
145        let json = serde_json::to_string(&value).unwrap();
146        let parsed: AggregatedValue = serde_json::from_str(&json).unwrap();
147        assert_eq!(value.value, parsed.value);
148        assert_eq!(value.aggregation_method, parsed.aggregation_method);
149    }
150
151    #[test]
152    fn test_bo4e_object_impl() {
153        assert_eq!(AggregatedValue::type_name_german(), "Aggregiertwert");
154        assert_eq!(AggregatedValue::type_name_english(), "AggregatedValue");
155    }
156}