matc/clusters/codec/
pressure_measurement.rs

1//! Generated Matter TLV encoders and decoders for Pressure Measurement Cluster
2//! Cluster ID: 0x0403
3//! 
4//! This file is automatically generated from PressureMeasurement.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Attribute decoders
12
13/// Decode MeasuredValue attribute (0x0000)
14pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
15    if let tlv::TlvItemValue::Int(v) = inp {
16        Ok(Some(*v as i16))
17    } else {
18        Ok(None)
19    }
20}
21
22/// Decode MinMeasuredValue attribute (0x0001)
23pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
24    if let tlv::TlvItemValue::Int(v) = inp {
25        Ok(Some(*v as i16))
26    } else {
27        Ok(None)
28    }
29}
30
31/// Decode MaxMeasuredValue attribute (0x0002)
32pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
33    if let tlv::TlvItemValue::Int(v) = inp {
34        Ok(Some(*v as i16))
35    } else {
36        Ok(None)
37    }
38}
39
40/// Decode Tolerance attribute (0x0003)
41pub fn decode_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
42    if let tlv::TlvItemValue::Int(v) = inp {
43        Ok(*v as u16)
44    } else {
45        Err(anyhow::anyhow!("Expected Integer"))
46    }
47}
48
49/// Decode ScaledValue attribute (0x0010)
50pub fn decode_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
51    if let tlv::TlvItemValue::Int(v) = inp {
52        Ok(Some(*v as i16))
53    } else {
54        Ok(None)
55    }
56}
57
58/// Decode MinScaledValue attribute (0x0011)
59pub fn decode_min_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
60    if let tlv::TlvItemValue::Int(v) = inp {
61        Ok(Some(*v as i16))
62    } else {
63        Ok(None)
64    }
65}
66
67/// Decode MaxScaledValue attribute (0x0012)
68pub fn decode_max_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
69    if let tlv::TlvItemValue::Int(v) = inp {
70        Ok(Some(*v as i16))
71    } else {
72        Ok(None)
73    }
74}
75
76/// Decode ScaledTolerance attribute (0x0013)
77pub fn decode_scaled_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
78    if let tlv::TlvItemValue::Int(v) = inp {
79        Ok(*v as u16)
80    } else {
81        Err(anyhow::anyhow!("Expected Integer"))
82    }
83}
84
85/// Decode Scale attribute (0x0014)
86pub fn decode_scale(inp: &tlv::TlvItemValue) -> anyhow::Result<i8> {
87    if let tlv::TlvItemValue::Int(v) = inp {
88        Ok(*v as i8)
89    } else {
90        Err(anyhow::anyhow!("Expected Integer"))
91    }
92}
93
94
95// JSON dispatcher function
96
97/// Decode attribute value and return as JSON string
98/// 
99/// # Parameters
100/// * `cluster_id` - The cluster identifier
101/// * `attribute_id` - The attribute identifier
102/// * `tlv_value` - The TLV value to decode
103/// 
104/// # Returns
105/// JSON string representation of the decoded value or error
106pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
107    // Verify this is the correct cluster
108    if cluster_id != 0x0403 {
109        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0403, got {}\"}}", cluster_id);
110    }
111    
112    match attribute_id {
113        0x0000 => {
114            match decode_measured_value(tlv_value) {
115                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
116                Err(e) => format!("{{\"error\": \"{}\"}}", e),
117            }
118        }
119        0x0001 => {
120            match decode_min_measured_value(tlv_value) {
121                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
122                Err(e) => format!("{{\"error\": \"{}\"}}", e),
123            }
124        }
125        0x0002 => {
126            match decode_max_measured_value(tlv_value) {
127                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
128                Err(e) => format!("{{\"error\": \"{}\"}}", e),
129            }
130        }
131        0x0003 => {
132            match decode_tolerance(tlv_value) {
133                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
134                Err(e) => format!("{{\"error\": \"{}\"}}", e),
135            }
136        }
137        0x0010 => {
138            match decode_scaled_value(tlv_value) {
139                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
140                Err(e) => format!("{{\"error\": \"{}\"}}", e),
141            }
142        }
143        0x0011 => {
144            match decode_min_scaled_value(tlv_value) {
145                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
146                Err(e) => format!("{{\"error\": \"{}\"}}", e),
147            }
148        }
149        0x0012 => {
150            match decode_max_scaled_value(tlv_value) {
151                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
152                Err(e) => format!("{{\"error\": \"{}\"}}", e),
153            }
154        }
155        0x0013 => {
156            match decode_scaled_tolerance(tlv_value) {
157                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
158                Err(e) => format!("{{\"error\": \"{}\"}}", e),
159            }
160        }
161        0x0014 => {
162            match decode_scale(tlv_value) {
163                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
164                Err(e) => format!("{{\"error\": \"{}\"}}", e),
165            }
166        }
167        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
168    }
169}
170
171/// Get list of all attributes supported by this cluster
172/// 
173/// # Returns
174/// Vector of tuples containing (attribute_id, attribute_name)
175pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
176    vec![
177        (0x0000, "MeasuredValue"),
178        (0x0001, "MinMeasuredValue"),
179        (0x0002, "MaxMeasuredValue"),
180        (0x0003, "Tolerance"),
181        (0x0010, "ScaledValue"),
182        (0x0011, "MinScaledValue"),
183        (0x0012, "MaxScaledValue"),
184        (0x0013, "ScaledTolerance"),
185        (0x0014, "Scale"),
186    ]
187}
188