Skip to main content

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#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
29#[cfg_attr(feature = "json-schema", schemars(rename = "Lastprofilwert"))]
30#[serde(rename_all = "camelCase")]
31pub struct LoadProfileValue {
32    /// BO4E metadata
33    #[serde(flatten)]
34    pub meta: Bo4eMeta,
35
36    /// Timestamp of the measurement (Zeitpunkt)
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[cfg_attr(feature = "json-schema", schemars(rename = "zeitpunkt"))]
39    pub timestamp: Option<DateTime<Utc>>,
40
41    /// Power/load value (Wert)
42    #[serde(skip_serializing_if = "Option::is_none")]
43    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
44    pub value: Option<f64>,
45
46    /// Unit of measurement (Einheit)
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[cfg_attr(feature = "json-schema", schemars(rename = "einheit"))]
49    pub unit: Option<Unit>,
50
51    /// Status/quality of the value (Status)
52    #[serde(skip_serializing_if = "Option::is_none")]
53    #[cfg_attr(feature = "json-schema", schemars(rename = "status"))]
54    pub status: Option<MeasuredValueStatus>,
55
56    /// OBIS code (OBIS-Kennzahl)
57    #[serde(skip_serializing_if = "Option::is_none")]
58    #[cfg_attr(feature = "json-schema", schemars(rename = "obisKennzahl"))]
59    pub obis_code: Option<String>,
60
61    /// Interval duration in minutes (Intervalllaenge)
62    #[serde(skip_serializing_if = "Option::is_none")]
63    #[cfg_attr(feature = "json-schema", schemars(rename = "intervalllaenge"))]
64    pub interval_minutes: Option<i32>,
65}
66
67impl Bo4eObject for LoadProfileValue {
68    fn type_name_german() -> &'static str {
69        "Lastgangwert"
70    }
71
72    fn type_name_english() -> &'static str {
73        "LoadProfileValue"
74    }
75
76    fn meta(&self) -> &Bo4eMeta {
77        &self.meta
78    }
79
80    fn meta_mut(&mut self) -> &mut Bo4eMeta {
81        &mut self.meta
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use chrono::TimeZone;
89
90    #[test]
91    fn test_load_profile_value() {
92        let value = LoadProfileValue {
93            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 15, 12, 15, 0).unwrap()),
94            value: Some(125.5),
95            unit: Some(Unit::Kilowatt),
96            interval_minutes: Some(15),
97            ..Default::default()
98        };
99
100        let json = serde_json::to_string(&value).unwrap();
101        assert!(json.contains("125.5"));
102    }
103
104    #[test]
105    fn test_roundtrip() {
106        let value = LoadProfileValue {
107            timestamp: Some(Utc::now()),
108            value: Some(99.9),
109            unit: Some(Unit::Kilowatt),
110            status: Some(MeasuredValueStatus::Read),
111            ..Default::default()
112        };
113
114        let json = serde_json::to_string(&value).unwrap();
115        let parsed: LoadProfileValue = serde_json::from_str(&json).unwrap();
116        assert_eq!(value.value, parsed.value);
117        assert_eq!(value.status, parsed.status);
118    }
119
120    #[test]
121    fn test_bo4e_object_impl() {
122        assert_eq!(LoadProfileValue::type_name_german(), "Lastgangwert");
123        assert_eq!(LoadProfileValue::type_name_english(), "LoadProfileValue");
124    }
125}