bo4e_core/com/
load_profile_value.rs1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::{MeasuredValueStatus, Unit};
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct LoadProfileValue {
30 #[serde(flatten)]
32 pub meta: Bo4eMeta,
33
34 #[serde(skip_serializing_if = "Option::is_none")]
36 pub timestamp: Option<DateTime<Utc>>,
37
38 #[serde(skip_serializing_if = "Option::is_none")]
40 pub value: Option<f64>,
41
42 #[serde(skip_serializing_if = "Option::is_none")]
44 pub unit: Option<Unit>,
45
46 #[serde(skip_serializing_if = "Option::is_none")]
48 pub status: Option<MeasuredValueStatus>,
49
50 #[serde(skip_serializing_if = "Option::is_none")]
52 pub obis_code: Option<String>,
53
54 #[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}