bo4e_core/com/
load_profile_value.rs

1//! Load profile value (Lastgangwert) component.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::{MeasuredValueStatus, Unit};
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9/// A single value in a load profile (time series of power measurements).
10///
11/// German: Lastgangwert
12///
13/// # Example
14///
15/// ```rust
16/// use bo4e_core::com::LoadProfileValue;
17/// use bo4e_core::enums::Unit;
18/// use chrono::Utc;
19///
20/// let value = LoadProfileValue {
21///     timestamp: Some(Utc::now()),
22///     value: Some(125.5),
23///     unit: Some(Unit::Kilowatt),
24///     ..Default::default()
25/// };
26/// ```
27#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct LoadProfileValue {
30    /// BO4E metadata
31    #[serde(flatten)]
32    pub meta: Bo4eMeta,
33
34    /// Timestamp of the measurement (Zeitpunkt)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub timestamp: Option<DateTime<Utc>>,
37
38    /// Power/load value (Wert)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub value: Option<f64>,
41
42    /// Unit of measurement (Einheit)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub unit: Option<Unit>,
45
46    /// Status/quality of the value (Status)
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub status: Option<MeasuredValueStatus>,
49
50    /// OBIS code (OBIS-Kennzahl)
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub obis_code: Option<String>,
53
54    /// Interval duration in minutes (Intervalllaenge)
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub interval_minutes: Option<i32>,
57}
58
59impl Bo4eObject for LoadProfileValue {
60    fn type_name_german() -> &'static str {
61        "Lastgangwert"
62    }
63
64    fn type_name_english() -> &'static str {
65        "LoadProfileValue"
66    }
67
68    fn meta(&self) -> &Bo4eMeta {
69        &self.meta
70    }
71
72    fn meta_mut(&mut self) -> &mut Bo4eMeta {
73        &mut self.meta
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use chrono::TimeZone;
81
82    #[test]
83    fn test_load_profile_value() {
84        let value = LoadProfileValue {
85            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 15, 12, 15, 0).unwrap()),
86            value: Some(125.5),
87            unit: Some(Unit::Kilowatt),
88            interval_minutes: Some(15),
89            ..Default::default()
90        };
91
92        let json = serde_json::to_string(&value).unwrap();
93        assert!(json.contains("125.5"));
94    }
95
96    #[test]
97    fn test_roundtrip() {
98        let value = LoadProfileValue {
99            timestamp: Some(Utc::now()),
100            value: Some(99.9),
101            unit: Some(Unit::Kilowatt),
102            status: Some(MeasuredValueStatus::Read),
103            ..Default::default()
104        };
105
106        let json = serde_json::to_string(&value).unwrap();
107        let parsed: LoadProfileValue = serde_json::from_str(&json).unwrap();
108        assert_eq!(value.value, parsed.value);
109        assert_eq!(value.status, parsed.status);
110    }
111
112    #[test]
113    fn test_bo4e_object_impl() {
114        assert_eq!(LoadProfileValue::type_name_german(), "Lastgangwert");
115        assert_eq!(LoadProfileValue::type_name_english(), "LoadProfileValue");
116    }
117}