matc/clusters/codec/
laundry_dryer_controls.rs

1//! Matter TLV encoders and decoders for Laundry Dryer Controls Cluster
2//! Cluster ID: 0x004A
3//!
4//! This file is automatically generated from LaundryDryerControls.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Enum definitions
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum DrynessLevel {
16    /// Provides a low dryness level for the selected mode
17    Low = 0,
18    /// Provides the normal level of dryness for the selected mode
19    Normal = 1,
20    /// Provides an extra dryness level for the selected mode
21    Extra = 2,
22    /// Provides the max dryness level for the selected mode
23    Max = 3,
24}
25
26impl DrynessLevel {
27    /// Convert from u8 value
28    pub fn from_u8(value: u8) -> Option<Self> {
29        match value {
30            0 => Some(DrynessLevel::Low),
31            1 => Some(DrynessLevel::Normal),
32            2 => Some(DrynessLevel::Extra),
33            3 => Some(DrynessLevel::Max),
34            _ => None,
35        }
36    }
37
38    /// Convert to u8 value
39    pub fn to_u8(self) -> u8 {
40        self as u8
41    }
42}
43
44impl From<DrynessLevel> for u8 {
45    fn from(val: DrynessLevel) -> Self {
46        val as u8
47    }
48}
49
50// Attribute decoders
51
52/// Decode SupportedDrynessLevels attribute (0x0000)
53pub fn decode_supported_dryness_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<DrynessLevel>> {
54    let mut res = Vec::new();
55    if let tlv::TlvItemValue::List(v) = inp {
56        for item in v {
57            if let tlv::TlvItemValue::Int(i) = &item.value {
58                if let Some(enum_val) = DrynessLevel::from_u8(*i as u8) {
59                    res.push(enum_val);
60                }
61            }
62        }
63    }
64    Ok(res)
65}
66
67/// Decode SelectedDrynessLevel attribute (0x0001)
68pub fn decode_selected_dryness_level(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<DrynessLevel>> {
69    if let tlv::TlvItemValue::Int(v) = inp {
70        Ok(DrynessLevel::from_u8(*v as u8))
71    } else {
72        Ok(None)
73    }
74}
75
76
77// JSON dispatcher function
78
79/// Decode attribute value and return as JSON string
80///
81/// # Parameters
82/// * `cluster_id` - The cluster identifier
83/// * `attribute_id` - The attribute identifier
84/// * `tlv_value` - The TLV value to decode
85///
86/// # Returns
87/// JSON string representation of the decoded value or error
88pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
89    // Verify this is the correct cluster
90    if cluster_id != 0x004A {
91        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x004A, got {}\"}}", cluster_id);
92    }
93
94    match attribute_id {
95        0x0000 => {
96            match decode_supported_dryness_levels(tlv_value) {
97                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
98                Err(e) => format!("{{\"error\": \"{}\"}}", e),
99            }
100        }
101        0x0001 => {
102            match decode_selected_dryness_level(tlv_value) {
103                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
104                Err(e) => format!("{{\"error\": \"{}\"}}", e),
105            }
106        }
107        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
108    }
109}
110
111/// Get list of all attributes supported by this cluster
112///
113/// # Returns
114/// Vector of tuples containing (attribute_id, attribute_name)
115pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
116    vec![
117        (0x0000, "SupportedDrynessLevels"),
118        (0x0001, "SelectedDrynessLevel"),
119    ]
120}
121