matc/clusters/codec/
electrical_grid_conditions.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum ThreeLevel {
16 Low = 0,
18 Medium = 1,
20 High = 2,
22}
23
24impl ThreeLevel {
25 pub fn from_u8(value: u8) -> Option<Self> {
27 match value {
28 0 => Some(ThreeLevel::Low),
29 1 => Some(ThreeLevel::Medium),
30 2 => Some(ThreeLevel::High),
31 _ => None,
32 }
33 }
34
35 pub fn to_u8(self) -> u8 {
37 self as u8
38 }
39}
40
41impl From<ThreeLevel> for u8 {
42 fn from(val: ThreeLevel) -> Self {
43 val as u8
44 }
45}
46
47#[derive(Debug, serde::Serialize)]
50pub struct ElectricalGridConditions {
51 pub period_start: Option<u64>,
52 pub period_end: Option<u64>,
53 pub grid_carbon_intensity: Option<i16>,
54 pub grid_carbon_level: Option<ThreeLevel>,
55 pub local_carbon_intensity: Option<i16>,
56 pub local_carbon_level: Option<ThreeLevel>,
57}
58
59pub fn decode_local_generation_available(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<bool>> {
63 if let tlv::TlvItemValue::Bool(v) = inp {
64 Ok(Some(*v))
65 } else {
66 Ok(None)
67 }
68}
69
70pub fn decode_current_conditions(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<ElectricalGridConditions>> {
72 if let tlv::TlvItemValue::List(_fields) = inp {
73 let item = tlv::TlvItem { tag: 0, value: inp.clone() };
75 Ok(Some(ElectricalGridConditions {
76 period_start: item.get_int(&[0]),
77 period_end: item.get_int(&[1]),
78 grid_carbon_intensity: item.get_int(&[2]).map(|v| v as i16),
79 grid_carbon_level: item.get_int(&[3]).and_then(|v| ThreeLevel::from_u8(v as u8)),
80 local_carbon_intensity: item.get_int(&[4]).map(|v| v as i16),
81 local_carbon_level: item.get_int(&[5]).and_then(|v| ThreeLevel::from_u8(v as u8)),
82 }))
83 } else {
87 Ok(None)
88 }
90}
91
92pub fn decode_forecast_conditions(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ElectricalGridConditions>> {
94 let mut res = Vec::new();
95 if let tlv::TlvItemValue::List(v) = inp {
96 for item in v {
97 res.push(ElectricalGridConditions {
98 period_start: item.get_int(&[0]),
99 period_end: item.get_int(&[1]),
100 grid_carbon_intensity: item.get_int(&[2]).map(|v| v as i16),
101 grid_carbon_level: item.get_int(&[3]).and_then(|v| ThreeLevel::from_u8(v as u8)),
102 local_carbon_intensity: item.get_int(&[4]).map(|v| v as i16),
103 local_carbon_level: item.get_int(&[5]).and_then(|v| ThreeLevel::from_u8(v as u8)),
104 });
105 }
106 }
107 Ok(res)
108}
109
110
111pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
123 if cluster_id != 0x00A0 {
125 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x00A0, got {}\"}}", cluster_id);
126 }
127
128 match attribute_id {
129 0x0000 => {
130 match decode_local_generation_available(tlv_value) {
131 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
132 Err(e) => format!("{{\"error\": \"{}\"}}", e),
133 }
134 }
135 0x0001 => {
136 match decode_current_conditions(tlv_value) {
137 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
138 Err(e) => format!("{{\"error\": \"{}\"}}", e),
139 }
140 }
141 0x0002 => {
142 match decode_forecast_conditions(tlv_value) {
143 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
144 Err(e) => format!("{{\"error\": \"{}\"}}", e),
145 }
146 }
147 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
148 }
149}
150
151pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
156 vec![
157 (0x0000, "LocalGenerationAvailable"),
158 (0x0001, "CurrentConditions"),
159 (0x0002, "ForecastConditions"),
160 ]
161}
162
163#[derive(Debug, serde::Serialize)]
164pub struct CurrentConditionsChangedEvent {
165 pub current_conditions: Option<ElectricalGridConditions>,
166}
167
168pub fn decode_current_conditions_changed_event(inp: &tlv::TlvItemValue) -> anyhow::Result<CurrentConditionsChangedEvent> {
172 if let tlv::TlvItemValue::List(_fields) = inp {
173 let item = tlv::TlvItem { tag: 0, value: inp.clone() };
174 Ok(CurrentConditionsChangedEvent {
175 current_conditions: {
176 if let Some(nested_tlv) = item.get(&[0]) {
177 if let tlv::TlvItemValue::List(_) = nested_tlv {
178 let nested_item = tlv::TlvItem { tag: 0, value: nested_tlv.clone() };
179 Some(ElectricalGridConditions {
180 period_start: nested_item.get_int(&[0]),
181 period_end: nested_item.get_int(&[1]),
182 grid_carbon_intensity: nested_item.get_int(&[2]).map(|v| v as i16),
183 grid_carbon_level: nested_item.get_int(&[3]).and_then(|v| ThreeLevel::from_u8(v as u8)),
184 local_carbon_intensity: nested_item.get_int(&[4]).map(|v| v as i16),
185 local_carbon_level: nested_item.get_int(&[5]).and_then(|v| ThreeLevel::from_u8(v as u8)),
186 })
187 } else {
188 None
189 }
190 } else {
191 None
192 }
193 },
194 })
195 } else {
196 Err(anyhow::anyhow!("Expected struct fields"))
197 }
198}
199