matc/clusters/codec/
mode_microwave_oven.rs

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