use serde::{Deserialize, Serialize};
use crate::com::Address;
use crate::enums::{Division, EnergyDirection, TechnicalResourceUsage};
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json-schema", schemars(rename = "TechnischeRessource"))]
#[serde(rename_all = "camelCase")]
pub struct TechnicalResource {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "technischeRessourceId"))]
pub technical_resource_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "sparte"))]
pub division: Option<Division>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "verwendungszweck"))]
pub usage: Option<TechnicalResourceUsage>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "energierichtung"))]
pub energy_direction: Option<EnergyDirection>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "standort"))]
pub address: Option<Address>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "nennleistung"))]
pub nominal_power: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "maximalleistung"))]
pub max_power: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "minimalleistung"))]
pub min_power: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "speicherkapazitaet"))]
pub energy_capacity: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "messlokationsId"))]
pub metering_location_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "marktlokationsId"))]
pub market_location_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "inbetriebnahmedatum"))]
pub commissioning_date: Option<chrono::DateTime<chrono::Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "stilllegungsdatum"))]
pub decommissioning_date: Option<chrono::DateTime<chrono::Utc>>,
}
impl Bo4eObject for TechnicalResource {
fn type_name_german() -> &'static str {
"TechnischeRessource"
}
fn type_name_english() -> &'static str {
"TechnicalResource"
}
fn meta(&self) -> &Bo4eMeta {
&self.meta
}
fn meta_mut(&mut self) -> &mut Bo4eMeta {
&mut self.meta
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_technical_resource_creation() {
let resource = TechnicalResource {
technical_resource_id: Some("TR001".to_string()),
division: Some(Division::Electricity),
usage: Some(TechnicalResourceUsage::ElectricityGenerationType),
..Default::default()
};
assert_eq!(resource.technical_resource_id, Some("TR001".to_string()));
}
#[test]
fn test_serialize() {
let resource = TechnicalResource {
meta: Bo4eMeta::with_type("TechnischeRessource"),
technical_resource_id: Some("TR001".to_string()),
..Default::default()
};
let json = serde_json::to_string(&resource).unwrap();
assert!(json.contains(r#""_typ":"TechnischeRessource""#));
}
#[test]
fn test_roundtrip() {
let resource = TechnicalResource {
meta: Bo4eMeta::with_type("TechnischeRessource"),
technical_resource_id: Some("TR001".to_string()),
division: Some(Division::Electricity),
usage: Some(TechnicalResourceUsage::ElectricityGenerationType),
nominal_power: Some(100.0),
max_power: Some(120.0),
..Default::default()
};
let json = serde_json::to_string(&resource).unwrap();
let parsed: TechnicalResource = serde_json::from_str(&json).unwrap();
assert_eq!(resource, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(TechnicalResource::type_name_german(), "TechnischeRessource");
assert_eq!(TechnicalResource::type_name_english(), "TechnicalResource");
}
}