Skip to main content

bo4e_core/bo/
device.rs

1//! Device (Geraet) business object.
2//!
3//! Represents a technical device in the energy infrastructure.
4
5use serde::{Deserialize, Serialize};
6
7use crate::enums::{DeviceCategory, DeviceType};
8use crate::traits::{Bo4eMeta, Bo4eObject};
9
10/// A technical device used in the energy infrastructure.
11///
12/// German: Geraet
13///
14/// Devices are technical equipment like transformers, switches,
15/// or other equipment in the energy network.
16///
17/// # Example
18///
19/// ```rust
20/// use bo4e_core::bo::Device;
21/// use bo4e_core::enums::DeviceCategory;
22///
23/// let device = Device {
24///     device_id: Some("DEV001".to_string()),
25///     device_category: Some(DeviceCategory::MeteringDevice),
26///     ..Default::default()
27/// };
28/// ```
29#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
30#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
31#[cfg_attr(feature = "json-schema", schemars(rename = "Geraet"))]
32#[serde(rename_all = "camelCase")]
33pub struct Device {
34    /// BO4E metadata
35    #[serde(flatten)]
36    pub meta: Bo4eMeta,
37
38    /// Device identification (Geraetkennung)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    #[cfg_attr(feature = "json-schema", schemars(rename = "geraetkennung"))]
41    pub device_id: Option<String>,
42
43    /// Serial number (Seriennummer)
44    #[serde(skip_serializing_if = "Option::is_none")]
45    #[cfg_attr(feature = "json-schema", schemars(rename = "seriennummer"))]
46    pub serial_number: Option<String>,
47
48    /// Device category (Geraeteklasse)
49    #[serde(skip_serializing_if = "Option::is_none")]
50    #[cfg_attr(feature = "json-schema", schemars(rename = "geraeteklasse"))]
51    pub device_category: Option<DeviceCategory>,
52
53    /// Device type (Geraetetyp)
54    #[serde(skip_serializing_if = "Option::is_none")]
55    #[cfg_attr(feature = "json-schema", schemars(rename = "geraetetyp"))]
56    pub device_type: Option<DeviceType>,
57
58    /// Manufacturer (Hersteller)
59    #[serde(skip_serializing_if = "Option::is_none")]
60    #[cfg_attr(feature = "json-schema", schemars(rename = "hersteller"))]
61    pub manufacturer: Option<String>,
62
63    /// Model name (Modellbezeichnung)
64    #[serde(skip_serializing_if = "Option::is_none")]
65    #[cfg_attr(feature = "json-schema", schemars(rename = "modellbezeichnung"))]
66    pub model: Option<String>,
67
68    /// Manufacturing year (Baujahr)
69    #[serde(skip_serializing_if = "Option::is_none")]
70    #[cfg_attr(feature = "json-schema", schemars(rename = "baujahr"))]
71    pub manufacturing_year: Option<i32>,
72
73    /// Installation date (Einbaudatum)
74    #[serde(skip_serializing_if = "Option::is_none")]
75    #[cfg_attr(feature = "json-schema", schemars(rename = "einbaudatum"))]
76    pub installation_date: Option<chrono::DateTime<chrono::Utc>>,
77
78    /// Removal date (Ausbaudatum)
79    #[serde(skip_serializing_if = "Option::is_none")]
80    #[cfg_attr(feature = "json-schema", schemars(rename = "ausbaudatum"))]
81    pub removal_date: Option<chrono::DateTime<chrono::Utc>>,
82
83    /// Firmware version (Firmware-Version)
84    #[serde(skip_serializing_if = "Option::is_none")]
85    #[cfg_attr(feature = "json-schema", schemars(rename = "firmwareVersion"))]
86    pub firmware_version: Option<String>,
87
88    /// Description (Beschreibung)
89    #[serde(skip_serializing_if = "Option::is_none")]
90    #[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
91    pub description: Option<String>,
92
93    /// Associated metering location ID
94    #[serde(skip_serializing_if = "Option::is_none")]
95    #[cfg_attr(feature = "json-schema", schemars(rename = "messlokationsId"))]
96    pub metering_location_id: Option<String>,
97}
98
99impl Bo4eObject for Device {
100    fn type_name_german() -> &'static str {
101        "Geraet"
102    }
103
104    fn type_name_english() -> &'static str {
105        "Device"
106    }
107
108    fn meta(&self) -> &Bo4eMeta {
109        &self.meta
110    }
111
112    fn meta_mut(&mut self) -> &mut Bo4eMeta {
113        &mut self.meta
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn test_device_creation() {
123        let device = Device {
124            device_id: Some("DEV001".to_string()),
125            device_category: Some(DeviceCategory::MeteringDevice),
126            ..Default::default()
127        };
128
129        assert_eq!(device.device_id, Some("DEV001".to_string()));
130    }
131
132    #[test]
133    fn test_serialize() {
134        let device = Device {
135            meta: Bo4eMeta::with_type("Geraet"),
136            device_id: Some("DEV001".to_string()),
137            ..Default::default()
138        };
139
140        let json = serde_json::to_string(&device).unwrap();
141        assert!(json.contains(r#""_typ":"Geraet""#));
142    }
143
144    #[test]
145    fn test_roundtrip() {
146        let device = Device {
147            meta: Bo4eMeta::with_type("Geraet"),
148            device_id: Some("DEV001".to_string()),
149            serial_number: Some("SN123456".to_string()),
150            device_category: Some(DeviceCategory::MeteringDevice),
151            manufacturer: Some("Acme Corp".to_string()),
152            manufacturing_year: Some(2023),
153            ..Default::default()
154        };
155
156        let json = serde_json::to_string(&device).unwrap();
157        let parsed: Device = serde_json::from_str(&json).unwrap();
158        assert_eq!(device, parsed);
159    }
160
161    #[test]
162    fn test_bo4e_object_impl() {
163        assert_eq!(Device::type_name_german(), "Geraet");
164        assert_eq!(Device::type_name_english(), "Device");
165    }
166}