matc/clusters/codec/
microwave_oven_control.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_set_cooking_parameters(cook_mode: u8, cook_time: u8, power_setting: u8, watt_setting_index: u8, start_after_setting: bool) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt8(cook_mode)).into(),
19 (1, tlv::TlvItemValueEnc::UInt8(cook_time)).into(),
20 (2, tlv::TlvItemValueEnc::UInt8(power_setting)).into(),
21 (3, tlv::TlvItemValueEnc::UInt8(watt_setting_index)).into(),
22 (4, tlv::TlvItemValueEnc::Bool(start_after_setting)).into(),
23 ]),
24 };
25 Ok(tlv.encode()?)
26}
27
28pub fn encode_add_more_time(time_to_add: u8) -> anyhow::Result<Vec<u8>> {
30 let tlv = tlv::TlvItemEnc {
31 tag: 0,
32 value: tlv::TlvItemValueEnc::StructInvisible(vec![
33 (0, tlv::TlvItemValueEnc::UInt8(time_to_add)).into(),
34 ]),
35 };
36 Ok(tlv.encode()?)
37}
38
39pub fn decode_cook_time(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
43 if let tlv::TlvItemValue::Int(v) = inp {
44 Ok(*v as u8)
45 } else {
46 Err(anyhow::anyhow!("Expected Integer"))
47 }
48}
49
50pub fn decode_max_cook_time(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
52 if let tlv::TlvItemValue::Int(v) = inp {
53 Ok(*v as u8)
54 } else {
55 Err(anyhow::anyhow!("Expected Integer"))
56 }
57}
58
59pub fn decode_power_setting(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
61 if let tlv::TlvItemValue::Int(v) = inp {
62 Ok(*v as u8)
63 } else {
64 Err(anyhow::anyhow!("Expected Integer"))
65 }
66}
67
68pub fn decode_min_power(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
70 if let tlv::TlvItemValue::Int(v) = inp {
71 Ok(*v as u8)
72 } else {
73 Err(anyhow::anyhow!("Expected Integer"))
74 }
75}
76
77pub fn decode_max_power(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
79 if let tlv::TlvItemValue::Int(v) = inp {
80 Ok(*v as u8)
81 } else {
82 Err(anyhow::anyhow!("Expected Integer"))
83 }
84}
85
86pub fn decode_power_step(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
88 if let tlv::TlvItemValue::Int(v) = inp {
89 Ok(*v as u8)
90 } else {
91 Err(anyhow::anyhow!("Expected Integer"))
92 }
93}
94
95pub fn decode_supported_watts(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
97 let mut res = Vec::new();
98 if let tlv::TlvItemValue::List(v) = inp {
99 for item in v {
100 if let tlv::TlvItemValue::Int(i) = &item.value {
101 res.push(*i as u16);
102 }
103 }
104 }
105 Ok(res)
106}
107
108pub fn decode_selected_watt_index(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
110 if let tlv::TlvItemValue::Int(v) = inp {
111 Ok(*v as u8)
112 } else {
113 Err(anyhow::anyhow!("Expected Integer"))
114 }
115}
116
117pub fn decode_watt_rating(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
119 if let tlv::TlvItemValue::Int(v) = inp {
120 Ok(*v as u16)
121 } else {
122 Err(anyhow::anyhow!("Expected Integer"))
123 }
124}
125
126
127pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
139 if cluster_id != 0x005F {
141 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005F, got {}\"}}", cluster_id);
142 }
143
144 match attribute_id {
145 0x0000 => {
146 match decode_cook_time(tlv_value) {
147 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
148 Err(e) => format!("{{\"error\": \"{}\"}}", e),
149 }
150 }
151 0x0001 => {
152 match decode_max_cook_time(tlv_value) {
153 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154 Err(e) => format!("{{\"error\": \"{}\"}}", e),
155 }
156 }
157 0x0002 => {
158 match decode_power_setting(tlv_value) {
159 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
160 Err(e) => format!("{{\"error\": \"{}\"}}", e),
161 }
162 }
163 0x0003 => {
164 match decode_min_power(tlv_value) {
165 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
166 Err(e) => format!("{{\"error\": \"{}\"}}", e),
167 }
168 }
169 0x0004 => {
170 match decode_max_power(tlv_value) {
171 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
172 Err(e) => format!("{{\"error\": \"{}\"}}", e),
173 }
174 }
175 0x0005 => {
176 match decode_power_step(tlv_value) {
177 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
178 Err(e) => format!("{{\"error\": \"{}\"}}", e),
179 }
180 }
181 0x0006 => {
182 match decode_supported_watts(tlv_value) {
183 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
184 Err(e) => format!("{{\"error\": \"{}\"}}", e),
185 }
186 }
187 0x0007 => {
188 match decode_selected_watt_index(tlv_value) {
189 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
190 Err(e) => format!("{{\"error\": \"{}\"}}", e),
191 }
192 }
193 0x0008 => {
194 match decode_watt_rating(tlv_value) {
195 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
196 Err(e) => format!("{{\"error\": \"{}\"}}", e),
197 }
198 }
199 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
200 }
201}
202
203pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
208 vec![
209 (0x0000, "CookTime"),
210 (0x0001, "MaxCookTime"),
211 (0x0002, "PowerSetting"),
212 (0x0003, "MinPower"),
213 (0x0004, "MaxPower"),
214 (0x0005, "PowerStep"),
215 (0x0006, "SupportedWatts"),
216 (0x0007, "SelectedWattIndex"),
217 (0x0008, "WattRating"),
218 ]
219}
220