matc/clusters/codec/
laundry_washer_controls.rs

1//! Generated Matter TLV encoders and decoders for Laundry Washer Controls Cluster
2//! Cluster ID: 0x0053
3//! 
4//! This file is automatically generated from LaundryWasherControls.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Attribute decoders
12
13/// Decode SpinSpeeds attribute (0x0000)
14pub 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
26/// Decode SpinSpeedCurrent attribute (0x0001)
27pub 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
35/// Decode NumberOfRinses attribute (0x0002)
36pub 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
44/// Decode SupportedRinses attribute (0x0003)
45pub 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
58// JSON dispatcher function
59
60/// Decode attribute value and return as JSON string
61/// 
62/// # Parameters
63/// * `cluster_id` - The cluster identifier
64/// * `attribute_id` - The attribute identifier
65/// * `tlv_value` - The TLV value to decode
66/// 
67/// # Returns
68/// JSON string representation of the decoded value or error
69pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
70    // Verify this is the correct cluster
71    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
104/// Get list of all attributes supported by this cluster
105/// 
106/// # Returns
107/// Vector of tuples containing (attribute_id, attribute_name)
108pub 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