matc/clusters/codec/
temperature_control.rs

1//! Generated Matter TLV encoders and decoders for Temperature Control Cluster
2//! Cluster ID: 0x0056
3//! 
4//! This file is automatically generated from TemperatureControl.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Command encoders
12
13/// Encode SetTemperature command (0x00)
14pub fn encode_set_temperature(target_temperature: u8, target_temperature_level: u8) -> anyhow::Result<Vec<u8>> {
15    let tlv = tlv::TlvItemEnc {
16        tag: 0,
17        value: tlv::TlvItemValueEnc::StructInvisible(vec![
18        (0, tlv::TlvItemValueEnc::UInt8(target_temperature)).into(),
19        (1, tlv::TlvItemValueEnc::UInt8(target_temperature_level)).into(),
20        ]),
21    };
22    Ok(tlv.encode()?)
23}
24
25// Attribute decoders
26
27/// Decode TemperatureSetpoint attribute (0x0000)
28pub fn decode_temperature_setpoint(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
29    if let tlv::TlvItemValue::Int(v) = inp {
30        Ok(*v as u8)
31    } else {
32        Err(anyhow::anyhow!("Expected Integer"))
33    }
34}
35
36/// Decode MinTemperature attribute (0x0001)
37pub fn decode_min_temperature(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
38    if let tlv::TlvItemValue::Int(v) = inp {
39        Ok(*v as u8)
40    } else {
41        Err(anyhow::anyhow!("Expected Integer"))
42    }
43}
44
45/// Decode MaxTemperature attribute (0x0002)
46pub fn decode_max_temperature(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
47    if let tlv::TlvItemValue::Int(v) = inp {
48        Ok(*v as u8)
49    } else {
50        Err(anyhow::anyhow!("Expected Integer"))
51    }
52}
53
54/// Decode Step attribute (0x0003)
55pub fn decode_step(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
56    if let tlv::TlvItemValue::Int(v) = inp {
57        Ok(*v as u8)
58    } else {
59        Err(anyhow::anyhow!("Expected Integer"))
60    }
61}
62
63/// Decode SelectedTemperatureLevel attribute (0x0004)
64pub fn decode_selected_temperature_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
65    if let tlv::TlvItemValue::Int(v) = inp {
66        Ok(*v as u8)
67    } else {
68        Err(anyhow::anyhow!("Expected Integer"))
69    }
70}
71
72/// Decode SupportedTemperatureLevels attribute (0x0005)
73pub fn decode_supported_temperature_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
74    let mut res = Vec::new();
75    if let tlv::TlvItemValue::List(v) = inp {
76        for item in v {
77            if let tlv::TlvItemValue::String(s) = &item.value {
78                res.push(s.clone());
79            }
80        }
81    }
82    Ok(res)
83}
84
85
86// JSON dispatcher function
87
88/// Decode attribute value and return as JSON string
89/// 
90/// # Parameters
91/// * `cluster_id` - The cluster identifier
92/// * `attribute_id` - The attribute identifier
93/// * `tlv_value` - The TLV value to decode
94/// 
95/// # Returns
96/// JSON string representation of the decoded value or error
97pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
98    // Verify this is the correct cluster
99    if cluster_id != 0x0056 {
100        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0056, got {}\"}}", cluster_id);
101    }
102    
103    match attribute_id {
104        0x0000 => {
105            match decode_temperature_setpoint(tlv_value) {
106                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
107                Err(e) => format!("{{\"error\": \"{}\"}}", e),
108            }
109        }
110        0x0001 => {
111            match decode_min_temperature(tlv_value) {
112                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113                Err(e) => format!("{{\"error\": \"{}\"}}", e),
114            }
115        }
116        0x0002 => {
117            match decode_max_temperature(tlv_value) {
118                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
119                Err(e) => format!("{{\"error\": \"{}\"}}", e),
120            }
121        }
122        0x0003 => {
123            match decode_step(tlv_value) {
124                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
125                Err(e) => format!("{{\"error\": \"{}\"}}", e),
126            }
127        }
128        0x0004 => {
129            match decode_selected_temperature_level(tlv_value) {
130                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
131                Err(e) => format!("{{\"error\": \"{}\"}}", e),
132            }
133        }
134        0x0005 => {
135            match decode_supported_temperature_levels(tlv_value) {
136                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137                Err(e) => format!("{{\"error\": \"{}\"}}", e),
138            }
139        }
140        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
141    }
142}
143
144/// Get list of all attributes supported by this cluster
145/// 
146/// # Returns
147/// Vector of tuples containing (attribute_id, attribute_name)
148pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
149    vec![
150        (0x0000, "TemperatureSetpoint"),
151        (0x0001, "MinTemperature"),
152        (0x0002, "MaxTemperature"),
153        (0x0003, "Step"),
154        (0x0004, "SelectedTemperatureLevel"),
155        (0x0005, "SupportedTemperatureLevels"),
156    ]
157}
158