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#[serde(rename_all = "camelCase")]
30pub struct AggregatedValue {
31    /// BO4E metadata
32    #[serde(flatten)]
33    pub meta: Bo4eMeta,
34
35    /// Timestamp for the aggregated value (Zeitpunkt)
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub timestamp: Option<DateTime<Utc>>,
38
39    /// The aggregated value (Wert)
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub value: Option<f64>,
42
43    /// Unit of the value (Einheit)
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub unit: Option<Unit>,
46
47    /// Aggregation method (Aggregationsmethode)
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub aggregation_method: Option<String>,
50
51    /// Period start for aggregation (Periodenbeginn)
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub period_start: Option<DateTime<Utc>>,
54
55    /// Period end for aggregation (Periodenende)
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub period_end: Option<DateTime<Utc>>,
58
59    /// Number of source values aggregated (Anzahl Quellwerte)
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub source_count: Option<i32>,
62
63    /// OBIS code (OBIS-Kennzahl)
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub obis_code: Option<String>,
66}
67
68impl Bo4eObject for AggregatedValue {
69    fn type_name_german() -> &'static str {
70        "Aggregiertwert"
71    }
72
73    fn type_name_english() -> &'static str {
74        "AggregatedValue"
75    }
76
77    fn meta(&self) -> &Bo4eMeta {
78        &self.meta
79    }
80
81    fn meta_mut(&mut self) -> &mut Bo4eMeta {
82        &mut self.meta
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use chrono::TimeZone;
90
91    #[test]
92    fn test_aggregated_value() {
93        let value = AggregatedValue {
94            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap()),
95            value: Some(15000.0),
96            unit: Some(Unit::KilowattHour),
97            aggregation_method: Some("SUM".to_string()),
98            source_count: Some(31),
99            ..Default::default()
100        };
101
102        let json = serde_json::to_string(&value).unwrap();
103        assert!(json.contains("15000"));
104        assert!(json.contains("SUM"));
105    }
106
107    #[test]
108    fn test_average_aggregation() {
109        let start = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
110        let end = Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap();
111
112        let value = AggregatedValue {
113            value: Some(125.5),
114            aggregation_method: Some("AVERAGE".to_string()),
115            period_start: Some(start),
116            period_end: Some(end),
117            source_count: Some(2976),
118            ..Default::default()
119        };
120
121        let json = serde_json::to_string(&value).unwrap();
122        assert!(json.contains("AVERAGE"));
123        assert!(json.contains("2976"));
124    }
125
126    #[test]
127    fn test_roundtrip() {
128        let value = AggregatedValue {
129            timestamp: Some(Utc::now()),
130            value: Some(999.99),
131            aggregation_method: Some("MAX".to_string()),
132            ..Default::default()
133        };
134
135        let json = serde_json::to_string(&value).unwrap();
136        let parsed: AggregatedValue = serde_json::from_str(&json).unwrap();
137        assert_eq!(value.value, parsed.value);
138        assert_eq!(value.aggregation_method, parsed.aggregation_method);
139    }
140
141    #[test]
142    fn test_bo4e_object_impl() {
143        assert_eq!(AggregatedValue::type_name_german(), "Aggregiertwert");
144        assert_eq!(AggregatedValue::type_name_english(), "AggregatedValue");
145    }
146}