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#[serde(rename_all = "camelCase")]
29pub struct MeasuredValue {
30    /// BO4E metadata
31    #[serde(flatten)]
32    pub meta: Bo4eMeta,
33
34    /// Timestamp of measurement (Zeitpunkt)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub timestamp: Option<DateTime<Utc>>,
37
38    /// Measured 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 identifying the measurement (OBIS-Kennzahl)
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub obis_code: Option<String>,
53}
54
55impl Bo4eObject for MeasuredValue {
56    fn type_name_german() -> &'static str {
57        "Messwert"
58    }
59
60    fn type_name_english() -> &'static str {
61        "MeasuredValue"
62    }
63
64    fn meta(&self) -> &Bo4eMeta {
65        &self.meta
66    }
67
68    fn meta_mut(&mut self) -> &mut Bo4eMeta {
69        &mut self.meta
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use chrono::TimeZone;
77
78    #[test]
79    fn test_measured_value() {
80        let value = MeasuredValue {
81            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 15, 12, 0, 0).unwrap()),
82            value: Some(12345.67),
83            unit: Some(Unit::KilowattHour),
84            obis_code: Some("1-0:1.8.0".to_string()),
85            ..Default::default()
86        };
87
88        let json = serde_json::to_string(&value).unwrap();
89        assert!(json.contains("12345.67"));
90        assert!(json.contains("1-0:1.8.0"));
91    }
92
93    #[test]
94    fn test_roundtrip() {
95        let value = MeasuredValue {
96            timestamp: Some(Utc::now()),
97            value: Some(999.99),
98            unit: Some(Unit::CubicMeter),
99            ..Default::default()
100        };
101
102        let json = serde_json::to_string(&value).unwrap();
103        let parsed: MeasuredValue = serde_json::from_str(&json).unwrap();
104        assert_eq!(value.value, parsed.value);
105        assert_eq!(value.unit, parsed.unit);
106    }
107
108    #[test]
109    fn test_bo4e_object_impl() {
110        assert_eq!(MeasuredValue::type_name_german(), "Messwert");
111        assert_eq!(MeasuredValue::type_name_english(), "MeasuredValue");
112    }
113}