Skip to main content

bo4e_core/com/
measured_value.rs

1//! Measured value (Messwert) component.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::{MeasuredValueStatus, Unit};
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9/// A measured value at a specific timestamp.
10///
11/// German: Messwert
12///
13/// # Example
14///
15/// ```rust
16/// use bo4e_core::com::MeasuredValue;
17/// use bo4e_core::enums::Unit;
18/// use chrono::Utc;
19///
20/// let value = MeasuredValue {
21///     timestamp: Some(Utc::now()),
22///     value: Some(12345.67),
23///     unit: Some(Unit::KilowattHour),
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 = "Messwert"))]
30#[serde(rename_all = "camelCase")]
31pub struct MeasuredValue {
32    /// BO4E metadata
33    #[serde(flatten)]
34    pub meta: Bo4eMeta,
35
36    /// Timestamp of 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    /// Measured 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 identifying the measurement (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
62impl Bo4eObject for MeasuredValue {
63    fn type_name_german() -> &'static str {
64        "Messwert"
65    }
66
67    fn type_name_english() -> &'static str {
68        "MeasuredValue"
69    }
70
71    fn meta(&self) -> &Bo4eMeta {
72        &self.meta
73    }
74
75    fn meta_mut(&mut self) -> &mut Bo4eMeta {
76        &mut self.meta
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use chrono::TimeZone;
84
85    #[test]
86    fn test_measured_value() {
87        let value = MeasuredValue {
88            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 15, 12, 0, 0).unwrap()),
89            value: Some(12345.67),
90            unit: Some(Unit::KilowattHour),
91            obis_code: Some("1-0:1.8.0".to_string()),
92            ..Default::default()
93        };
94
95        let json = serde_json::to_string(&value).unwrap();
96        assert!(json.contains("12345.67"));
97        assert!(json.contains("1-0:1.8.0"));
98    }
99
100    #[test]
101    fn test_roundtrip() {
102        let value = MeasuredValue {
103            timestamp: Some(Utc::now()),
104            value: Some(999.99),
105            unit: Some(Unit::CubicMeter),
106            ..Default::default()
107        };
108
109        let json = serde_json::to_string(&value).unwrap();
110        let parsed: MeasuredValue = serde_json::from_str(&json).unwrap();
111        assert_eq!(value.value, parsed.value);
112        assert_eq!(value.unit, parsed.unit);
113    }
114
115    #[test]
116    fn test_bo4e_object_impl() {
117        assert_eq!(MeasuredValue::type_name_german(), "Messwert");
118        assert_eq!(MeasuredValue::type_name_english(), "MeasuredValue");
119    }
120}