matc/clusters/codec/
mode_refrigerator.rs

1//! Matter TLV encoders and decoders for Refrigerator And Temperature Controlled Cabinet Mode Cluster
2//! Cluster ID: 0x0052
3//!
4//! This file is automatically generated from Mode_Refrigerator.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(u16)]
15pub enum ModeTag {
16    Auto = 0,
17    Quick = 1,
18    Quiet = 2,
19    Lownoise = 3,
20    Lowenergy = 4,
21    Vacation = 5,
22    Min = 6,
23    Max = 7,
24    Night = 8,
25    Day = 9,
26    Rapidcool = 16384,
27    Rapidfreeze = 16385,
28}
29
30impl ModeTag {
31    /// Convert from u8 value (promoted to u16)
32    pub fn from_u8(value: u8) -> Option<Self> {
33        Self::from_u16(value as u16)
34    }
35
36    /// Convert from u16 value
37    pub fn from_u16(value: u16) -> Option<Self> {
38        match value {
39            0 => Some(ModeTag::Auto),
40            1 => Some(ModeTag::Quick),
41            2 => Some(ModeTag::Quiet),
42            3 => Some(ModeTag::Lownoise),
43            4 => Some(ModeTag::Lowenergy),
44            5 => Some(ModeTag::Vacation),
45            6 => Some(ModeTag::Min),
46            7 => Some(ModeTag::Max),
47            8 => Some(ModeTag::Night),
48            9 => Some(ModeTag::Day),
49            16384 => Some(ModeTag::Rapidcool),
50            16385 => Some(ModeTag::Rapidfreeze),
51            _ => None,
52        }
53    }
54
55    /// Convert to u8 value (truncated if value > 255)
56    pub fn to_u8(self) -> u8 {
57        self as u8
58    }
59
60    /// Convert to u16 value
61    pub fn to_u16(self) -> u16 {
62        self as u16
63    }
64}
65
66impl From<ModeTag> for u16 {
67    fn from(val: ModeTag) -> Self {
68        val as u16
69    }
70}
71
72// Struct definitions
73
74#[derive(Debug, serde::Serialize)]
75pub struct ModeOption {
76    pub label: Option<u8>,
77    pub mode: Option<u8>,
78    pub mode_tags: Option<u8>,
79}
80
81// Attribute decoders
82
83/// Decode SupportedModes attribute (0x0000)
84pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
85    if let tlv::TlvItemValue::Int(v) = inp {
86        Ok(*v as u8)
87    } else {
88        Err(anyhow::anyhow!("Expected UInt8"))
89    }
90}
91
92/// Decode CurrentMode attribute (0x0001)
93pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
94    if let tlv::TlvItemValue::Int(v) = inp {
95        Ok(*v as u8)
96    } else {
97        Err(anyhow::anyhow!("Expected UInt8"))
98    }
99}
100
101/// Decode StartUpMode attribute (0x0002)
102pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
103    if let tlv::TlvItemValue::Int(v) = inp {
104        Ok(*v as u8)
105    } else {
106        Err(anyhow::anyhow!("Expected UInt8"))
107    }
108}
109
110/// Decode OnMode attribute (0x0003)
111pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
112    if let tlv::TlvItemValue::Int(v) = inp {
113        Ok(*v as u8)
114    } else {
115        Err(anyhow::anyhow!("Expected UInt8"))
116    }
117}
118
119
120// JSON dispatcher function
121
122/// Decode attribute value and return as JSON string
123///
124/// # Parameters
125/// * `cluster_id` - The cluster identifier
126/// * `attribute_id` - The attribute identifier
127/// * `tlv_value` - The TLV value to decode
128///
129/// # Returns
130/// JSON string representation of the decoded value or error
131pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
132    // Verify this is the correct cluster
133    if cluster_id != 0x0052 {
134        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0052, got {}\"}}", cluster_id);
135    }
136
137    match attribute_id {
138        0x0000 => {
139            match decode_supported_modes(tlv_value) {
140                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
141                Err(e) => format!("{{\"error\": \"{}\"}}", e),
142            }
143        }
144        0x0001 => {
145            match decode_current_mode(tlv_value) {
146                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
147                Err(e) => format!("{{\"error\": \"{}\"}}", e),
148            }
149        }
150        0x0002 => {
151            match decode_start_up_mode(tlv_value) {
152                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
153                Err(e) => format!("{{\"error\": \"{}\"}}", e),
154            }
155        }
156        0x0003 => {
157            match decode_on_mode(tlv_value) {
158                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
159                Err(e) => format!("{{\"error\": \"{}\"}}", e),
160            }
161        }
162        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
163    }
164}
165
166/// Get list of all attributes supported by this cluster
167///
168/// # Returns
169/// Vector of tuples containing (attribute_id, attribute_name)
170pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
171    vec![
172        (0x0000, "SupportedModes"),
173        (0x0001, "CurrentMode"),
174        (0x0002, "StartUpMode"),
175        (0x0003, "OnMode"),
176    ]
177}
178