Skip to main content

cimstructs/
Switch.rs

1/// A generic device designed to close, or open, or both, one or more electric circuits. All switches are two terminal devices including grounding switches. The ACDCTerminal.connected at the two sides of the switch shall not be considered for assessing switch connectivity, i.e. only Switch.open, .normalOpen and .locked are relevant.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct Switch {
4    #[serde(flatten)]
5    pub base: super::ConductingEquipment,
6    /// If true, the switch is locked. The resulting switch state is a combination of locked and Switch.open attributes as follows: locked=true and Switch.open=true. The resulting state is open and locked; locked=false and Switch.open=true. The resulting state is open; locked=false and Switch.open=false. The resulting state is closed.
7    #[serde(skip_serializing_if = "Option::is_none")]
8    pub locked: Option<bool>,
9    /// The attribute is used in cases when no Measurement for the status value is present. If the Switch has a status measurement the Discrete.normalValue is expected to match with the Switch.normalOpen.
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub normal_open: Option<bool>,
12    /// The attribute tells if the switch is considered open when used as input to topology processing.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub open: Option<bool>,
15    /// The maximum continuous current carrying capacity in amps governed by the device material and construction. The attribute shall be a positive value.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub rated_current: Option<f64>,
18    /// Branch is retained in the topological solution. The flow through retained switches will normally be calculated in power flow.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub retained: Option<bool>,
21}
22impl crate::base::CimElement for Switch {
23    fn mrid(&self) -> &str { &self.base.base.base.base.id }
24    fn type_name(&self) -> &'static str { "Switch" }
25    fn as_any(&self) -> &dyn std::any::Any { self }
26    fn to_json_value(&self) -> serde_json::Value {
27        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
28    }
29    fn to_block(&self) -> crate::base::RdfBlock {
30        let mut block = self.base.to_block();
31        block.type_name = "Switch".to_string();
32        if let Some(v) = self.locked {
33            block.fields.insert("Switch.locked".into(), crate::base::FieldValue::Text(v.to_string()));
34        }
35        if let Some(v) = self.normal_open {
36            block.fields.insert("Switch.normalOpen".into(), crate::base::FieldValue::Text(v.to_string()));
37        }
38        if let Some(v) = self.open {
39            block.fields.insert("Switch.open".into(), crate::base::FieldValue::Text(v.to_string()));
40        }
41        if let Some(v) = self.rated_current {
42            block.fields.insert("Switch.ratedCurrent".into(), crate::base::FieldValue::Text(v.to_string()));
43        }
44        if let Some(v) = self.retained {
45            block.fields.insert("Switch.retained".into(), crate::base::FieldValue::Text(v.to_string()));
46        }
47        block
48    }
49}
50
51impl Switch {
52    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
53        let mut obj = Self::default();
54        obj.base.base.base.base.id.clone_from(&b.mrid);
55        for (key, val) in &b.fields {
56            match key.as_str() {
57                "Switch.locked" => {
58                    match val {
59                        crate::base::FieldValue::Text(sv) => { obj.locked = Some(sv.trim() == "true"); }
60                        crate::base::FieldValue::TextList(svs) => {
61                            if let Some(sv) = svs.last() { obj.locked = Some(sv.trim() == "true"); }
62                        }
63                        _ => {}
64                    }
65                }
66                "Switch.normalOpen" => {
67                    match val {
68                        crate::base::FieldValue::Text(sv) => { obj.normal_open = Some(sv.trim() == "true"); }
69                        crate::base::FieldValue::TextList(svs) => {
70                            if let Some(sv) = svs.last() { obj.normal_open = Some(sv.trim() == "true"); }
71                        }
72                        _ => {}
73                    }
74                }
75                "Switch.open" => {
76                    match val {
77                        crate::base::FieldValue::Text(sv) => { obj.open = Some(sv.trim() == "true"); }
78                        crate::base::FieldValue::TextList(svs) => {
79                            if let Some(sv) = svs.last() { obj.open = Some(sv.trim() == "true"); }
80                        }
81                        _ => {}
82                    }
83                }
84                "Switch.ratedCurrent" => {
85                    match val {
86                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.rated_current = Some(v); } }
87                        crate::base::FieldValue::TextList(svs) => {
88                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.rated_current = Some(v); } }
89                        }
90                        _ => {}
91                    }
92                }
93                "Switch.retained" => {
94                    match val {
95                        crate::base::FieldValue::Text(sv) => { obj.retained = Some(sv.trim() == "true"); }
96                        crate::base::FieldValue::TextList(svs) => {
97                            if let Some(sv) = svs.last() { obj.retained = Some(sv.trim() == "true"); }
98                        }
99                        _ => {}
100                    }
101                }
102                "ConductingEquipment.BaseVoltage" => {
103                    if let crate::base::FieldValue::Resource(sv) = val {
104                        obj.base.base_voltage = Some(crate::base::MridRef { mrid: sv.clone() });
105                    }
106                }
107                "Equipment.EquipmentContainer" => {
108                    if let crate::base::FieldValue::Resource(sv) = val {
109                        obj.base.base.equipment_container = Some(crate::base::MridRef { mrid: sv.clone() });
110                    }
111                }
112                "Equipment.aggregate" => {
113                    match val {
114                        crate::base::FieldValue::Text(sv) => { obj.base.base.aggregate = Some(sv.trim() == "true"); }
115                        crate::base::FieldValue::TextList(svs) => {
116                            if let Some(sv) = svs.last() { obj.base.base.aggregate = Some(sv.trim() == "true"); }
117                        }
118                        _ => {}
119                    }
120                }
121                "Equipment.inService" => {
122                    match val {
123                        crate::base::FieldValue::Text(sv) => { obj.base.base.in_service = Some(sv.trim() == "true"); }
124                        crate::base::FieldValue::TextList(svs) => {
125                            if let Some(sv) = svs.last() { obj.base.base.in_service = Some(sv.trim() == "true"); }
126                        }
127                        _ => {}
128                    }
129                }
130                "Equipment.normallyInService" => {
131                    match val {
132                        crate::base::FieldValue::Text(sv) => { obj.base.base.normally_in_service = Some(sv.trim() == "true"); }
133                        crate::base::FieldValue::TextList(svs) => {
134                            if let Some(sv) = svs.last() { obj.base.base.normally_in_service = Some(sv.trim() == "true"); }
135                        }
136                        _ => {}
137                    }
138                }
139                "IdentifiedObject.description" => {
140                    match val {
141                        crate::base::FieldValue::Text(sv) => { obj.base.base.base.base.description = sv.clone(); }
142                        crate::base::FieldValue::TextList(svs) => {
143                            if let Some(sv) = svs.last() { obj.base.base.base.base.description = sv.clone(); }
144                        }
145                        _ => {}
146                    }
147                }
148                "IdentifiedObject.energyIdentCodeEic" => {
149                    match val {
150                        crate::base::FieldValue::Text(sv) => { obj.base.base.base.base.energy_ident_code_eic = sv.clone(); }
151                        crate::base::FieldValue::TextList(svs) => {
152                            if let Some(sv) = svs.last() { obj.base.base.base.base.energy_ident_code_eic = sv.clone(); }
153                        }
154                        _ => {}
155                    }
156                }
157                "IdentifiedObject.mRID" => {
158                    match val {
159                        crate::base::FieldValue::Text(sv) => { obj.base.base.base.base.m_rid = sv.clone(); }
160                        crate::base::FieldValue::TextList(svs) => {
161                            if let Some(sv) = svs.last() { obj.base.base.base.base.m_rid = sv.clone(); }
162                        }
163                        _ => {}
164                    }
165                }
166                "IdentifiedObject.name" => {
167                    match val {
168                        crate::base::FieldValue::Text(sv) => { obj.base.base.base.base.name = sv.clone(); }
169                        crate::base::FieldValue::TextList(svs) => {
170                            if let Some(sv) = svs.last() { obj.base.base.base.base.name = sv.clone(); }
171                        }
172                        _ => {}
173                    }
174                }
175                "IdentifiedObject.shortName" => {
176                    match val {
177                        crate::base::FieldValue::Text(sv) => { obj.base.base.base.base.short_name = sv.clone(); }
178                        crate::base::FieldValue::TextList(svs) => {
179                            if let Some(sv) = svs.last() { obj.base.base.base.base.short_name = sv.clone(); }
180                        }
181                        _ => {}
182                    }
183                }
184                _ => {}
185            }
186        }
187        obj
188    }
189}