matc/clusters/codec/
water_heater_management.rs

1//! Generated Matter TLV encoders and decoders for Water Heater Management Cluster
2//! Cluster ID: 0x0094
3//! 
4//! This file is automatically generated from WaterHeaterManagement.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Struct definitions
12
13#[derive(Debug, serde::Serialize)]
14pub struct WaterHeaterBoostInfo {
15    pub duration: Option<u8>,
16    pub one_shot: Option<bool>,
17    pub emergency_boost: Option<bool>,
18    pub temporary_setpoint: Option<u8>,
19    pub target_percentage: Option<u8>,
20    pub target_reheat: Option<u8>,
21}
22
23// Command encoders
24
25/// Encode Boost command (0x00)
26pub fn encode_boost(boost_info: u8) -> anyhow::Result<Vec<u8>> {
27    let tlv = tlv::TlvItemEnc {
28        tag: 0,
29        value: tlv::TlvItemValueEnc::StructInvisible(vec![
30        (0, tlv::TlvItemValueEnc::UInt8(boost_info)).into(),
31        ]),
32    };
33    Ok(tlv.encode()?)
34}
35
36// Attribute decoders
37
38/// Decode HeaterTypes attribute (0x0000)
39pub fn decode_heater_types(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
40    if let tlv::TlvItemValue::Int(v) = inp {
41        Ok(*v as u8)
42    } else {
43        Err(anyhow::anyhow!("Expected Integer"))
44    }
45}
46
47/// Decode HeatDemand attribute (0x0001)
48pub fn decode_heat_demand(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
49    if let tlv::TlvItemValue::Int(v) = inp {
50        Ok(*v as u8)
51    } else {
52        Err(anyhow::anyhow!("Expected Integer"))
53    }
54}
55
56/// Decode TankVolume attribute (0x0002)
57pub fn decode_tank_volume(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
58    if let tlv::TlvItemValue::Int(v) = inp {
59        Ok(*v as u16)
60    } else {
61        Err(anyhow::anyhow!("Expected Integer"))
62    }
63}
64
65/// Decode EstimatedHeatRequired attribute (0x0003)
66pub fn decode_estimated_heat_required(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
67    if let tlv::TlvItemValue::Int(v) = inp {
68        Ok(*v as u8)
69    } else {
70        Err(anyhow::anyhow!("Expected Integer"))
71    }
72}
73
74/// Decode TankPercentage attribute (0x0004)
75pub fn decode_tank_percentage(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
76    if let tlv::TlvItemValue::Int(v) = inp {
77        Ok(*v as u8)
78    } else {
79        Err(anyhow::anyhow!("Expected Integer"))
80    }
81}
82
83/// Decode BoostState attribute (0x0005)
84pub fn decode_boost_state(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 Integer"))
89    }
90}
91
92
93// JSON dispatcher function
94
95/// Decode attribute value and return as JSON string
96/// 
97/// # Parameters
98/// * `cluster_id` - The cluster identifier
99/// * `attribute_id` - The attribute identifier
100/// * `tlv_value` - The TLV value to decode
101/// 
102/// # Returns
103/// JSON string representation of the decoded value or error
104pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
105    // Verify this is the correct cluster
106    if cluster_id != 0x0094 {
107        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0094, got {}\"}}", cluster_id);
108    }
109    
110    match attribute_id {
111        0x0000 => {
112            match decode_heater_types(tlv_value) {
113                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
114                Err(e) => format!("{{\"error\": \"{}\"}}", e),
115            }
116        }
117        0x0001 => {
118            match decode_heat_demand(tlv_value) {
119                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
120                Err(e) => format!("{{\"error\": \"{}\"}}", e),
121            }
122        }
123        0x0002 => {
124            match decode_tank_volume(tlv_value) {
125                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
126                Err(e) => format!("{{\"error\": \"{}\"}}", e),
127            }
128        }
129        0x0003 => {
130            match decode_estimated_heat_required(tlv_value) {
131                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
132                Err(e) => format!("{{\"error\": \"{}\"}}", e),
133            }
134        }
135        0x0004 => {
136            match decode_tank_percentage(tlv_value) {
137                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
138                Err(e) => format!("{{\"error\": \"{}\"}}", e),
139            }
140        }
141        0x0005 => {
142            match decode_boost_state(tlv_value) {
143                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
144                Err(e) => format!("{{\"error\": \"{}\"}}", e),
145            }
146        }
147        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
148    }
149}
150
151/// Get list of all attributes supported by this cluster
152/// 
153/// # Returns
154/// Vector of tuples containing (attribute_id, attribute_name)
155pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
156    vec![
157        (0x0000, "HeaterTypes"),
158        (0x0001, "HeatDemand"),
159        (0x0002, "TankVolume"),
160        (0x0003, "EstimatedHeatRequired"),
161        (0x0004, "TankPercentage"),
162        (0x0005, "BoostState"),
163    ]
164}
165