matc/clusters/codec/
switch.rs

1//! Generated Matter TLV encoders and decoders for Switch Cluster
2//! Cluster ID: 0x003B
3//! 
4//! This file is automatically generated from Switch.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Attribute decoders
12
13/// Decode NumberOfPositions attribute (0x0000)
14pub fn decode_number_of_positions(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
15    if let tlv::TlvItemValue::Int(v) = inp {
16        Ok(*v as u8)
17    } else {
18        Err(anyhow::anyhow!("Expected Integer"))
19    }
20}
21
22/// Decode CurrentPosition attribute (0x0001)
23pub fn decode_current_position(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 MultiPressMax attribute (0x0002)
32pub fn decode_multi_press_max(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
41// JSON dispatcher function
42
43/// Decode attribute value and return as JSON string
44/// 
45/// # Parameters
46/// * `cluster_id` - The cluster identifier
47/// * `attribute_id` - The attribute identifier
48/// * `tlv_value` - The TLV value to decode
49/// 
50/// # Returns
51/// JSON string representation of the decoded value or error
52pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
53    // Verify this is the correct cluster
54    if cluster_id != 0x003B {
55        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x003B, got {}\"}}", cluster_id);
56    }
57    
58    match attribute_id {
59        0x0000 => {
60            match decode_number_of_positions(tlv_value) {
61                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
62                Err(e) => format!("{{\"error\": \"{}\"}}", e),
63            }
64        }
65        0x0001 => {
66            match decode_current_position(tlv_value) {
67                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
68                Err(e) => format!("{{\"error\": \"{}\"}}", e),
69            }
70        }
71        0x0002 => {
72            match decode_multi_press_max(tlv_value) {
73                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
74                Err(e) => format!("{{\"error\": \"{}\"}}", e),
75            }
76        }
77        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
78    }
79}
80
81/// Get list of all attributes supported by this cluster
82/// 
83/// # Returns
84/// Vector of tuples containing (attribute_id, attribute_name)
85pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
86    vec![
87        (0x0000, "NumberOfPositions"),
88        (0x0001, "CurrentPosition"),
89        (0x0002, "MultiPressMax"),
90    ]
91}
92