matc/clusters/codec/
laundry_washer_controls.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_spin_speeds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
15 let mut res = Vec::new();
16 if let tlv::TlvItemValue::List(v) = inp {
17 for item in v {
18 if let tlv::TlvItemValue::String(s) = &item.value {
19 res.push(s.clone());
20 }
21 }
22 }
23 Ok(res)
24}
25
26pub fn decode_spin_speed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
28 if let tlv::TlvItemValue::Int(v) = inp {
29 Ok(Some(*v as u8))
30 } else {
31 Ok(None)
32 }
33}
34
35pub fn decode_number_of_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
37 if let tlv::TlvItemValue::Int(v) = inp {
38 Ok(*v as u8)
39 } else {
40 Err(anyhow::anyhow!("Expected Integer"))
41 }
42}
43
44pub fn decode_supported_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
46 let mut res = Vec::new();
47 if let tlv::TlvItemValue::List(v) = inp {
48 for item in v {
49 if let tlv::TlvItemValue::Int(i) = &item.value {
50 res.push(*i as u8);
51 }
52 }
53 }
54 Ok(res)
55}
56
57
58pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
70 if cluster_id != 0x0053 {
72 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0053, got {}\"}}", cluster_id);
73 }
74
75 match attribute_id {
76 0x0000 => {
77 match decode_spin_speeds(tlv_value) {
78 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
79 Err(e) => format!("{{\"error\": \"{}\"}}", e),
80 }
81 }
82 0x0001 => {
83 match decode_spin_speed_current(tlv_value) {
84 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
85 Err(e) => format!("{{\"error\": \"{}\"}}", e),
86 }
87 }
88 0x0002 => {
89 match decode_number_of_rinses(tlv_value) {
90 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
91 Err(e) => format!("{{\"error\": \"{}\"}}", e),
92 }
93 }
94 0x0003 => {
95 match decode_supported_rinses(tlv_value) {
96 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
97 Err(e) => format!("{{\"error\": \"{}\"}}", e),
98 }
99 }
100 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
101 }
102}
103
104pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
109 vec![
110 (0x0000, "SpinSpeeds"),
111 (0x0001, "SpinSpeedCurrent"),
112 (0x0002, "NumberOfRinses"),
113 (0x0003, "SupportedRinses"),
114 ]
115}
116