matc/clusters/codec/
mode_dishwasher.rs

1//! Generated Matter TLV encoders and decoders for Dishwasher Mode Cluster
2//! Cluster ID: 0x0059
3//! 
4//! This file is automatically generated from Mode_Dishwasher.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct ModeOption {
15    pub label: Option<u8>,
16    pub mode: Option<u8>,
17    pub mode_tags: Option<u8>,
18}
19
20// Attribute decoders
21
22/// Decode SupportedModes attribute (0x0000)
23pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
24    if let tlv::TlvItemValue::Int(v) = inp {
25        Ok(*v as u8)
26    } else {
27        Err(anyhow::anyhow!("Expected Integer"))
28    }
29}
30
31/// Decode CurrentMode attribute (0x0001)
32pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
33    if let tlv::TlvItemValue::Int(v) = inp {
34        Ok(*v as u8)
35    } else {
36        Err(anyhow::anyhow!("Expected Integer"))
37    }
38}
39
40/// Decode StartUpMode attribute (0x0002)
41pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
42    if let tlv::TlvItemValue::Int(v) = inp {
43        Ok(*v as u8)
44    } else {
45        Err(anyhow::anyhow!("Expected Integer"))
46    }
47}
48
49/// Decode OnMode attribute (0x0003)
50pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
51    if let tlv::TlvItemValue::Int(v) = inp {
52        Ok(*v as u8)
53    } else {
54        Err(anyhow::anyhow!("Expected Integer"))
55    }
56}
57
58
59// JSON dispatcher function
60
61/// Decode attribute value and return as JSON string
62/// 
63/// # Parameters
64/// * `cluster_id` - The cluster identifier
65/// * `attribute_id` - The attribute identifier
66/// * `tlv_value` - The TLV value to decode
67/// 
68/// # Returns
69/// JSON string representation of the decoded value or error
70pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
71    // Verify this is the correct cluster
72    if cluster_id != 0x0059 {
73        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0059, got {}\"}}", cluster_id);
74    }
75    
76    match attribute_id {
77        0x0000 => {
78            match decode_supported_modes(tlv_value) {
79                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
80                Err(e) => format!("{{\"error\": \"{}\"}}", e),
81            }
82        }
83        0x0001 => {
84            match decode_current_mode(tlv_value) {
85                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
86                Err(e) => format!("{{\"error\": \"{}\"}}", e),
87            }
88        }
89        0x0002 => {
90            match decode_start_up_mode(tlv_value) {
91                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
92                Err(e) => format!("{{\"error\": \"{}\"}}", e),
93            }
94        }
95        0x0003 => {
96            match decode_on_mode(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        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
102    }
103}
104
105/// Get list of all attributes supported by this cluster
106/// 
107/// # Returns
108/// Vector of tuples containing (attribute_id, attribute_name)
109pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
110    vec![
111        (0x0000, "SupportedModes"),
112        (0x0001, "CurrentMode"),
113        (0x0002, "StartUpMode"),
114        (0x0003, "OnMode"),
115    ]
116}
117