matc/clusters/codec/
pressure_measurement.rs

1//! Matter TLV encoders and decoders for Pressure Measurement Cluster
2//! Cluster ID: 0x0403
3//!
4//! This file is automatically generated from PressureMeasurement.xml
5
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Attribute decoders
14
15/// Decode MeasuredValue attribute (0x0000)
16pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
17    if let tlv::TlvItemValue::Int(v) = inp {
18        Ok(Some(*v as i16))
19    } else {
20        Ok(None)
21    }
22}
23
24/// Decode MinMeasuredValue attribute (0x0001)
25pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
26    if let tlv::TlvItemValue::Int(v) = inp {
27        Ok(Some(*v as i16))
28    } else {
29        Ok(None)
30    }
31}
32
33/// Decode MaxMeasuredValue attribute (0x0002)
34pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
35    if let tlv::TlvItemValue::Int(v) = inp {
36        Ok(Some(*v as i16))
37    } else {
38        Ok(None)
39    }
40}
41
42/// Decode Tolerance attribute (0x0003)
43pub fn decode_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
44    if let tlv::TlvItemValue::Int(v) = inp {
45        Ok(*v as u16)
46    } else {
47        Err(anyhow::anyhow!("Expected UInt16"))
48    }
49}
50
51/// Decode ScaledValue attribute (0x0010)
52pub fn decode_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
53    if let tlv::TlvItemValue::Int(v) = inp {
54        Ok(Some(*v as i16))
55    } else {
56        Ok(None)
57    }
58}
59
60/// Decode MinScaledValue attribute (0x0011)
61pub fn decode_min_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
62    if let tlv::TlvItemValue::Int(v) = inp {
63        Ok(Some(*v as i16))
64    } else {
65        Ok(None)
66    }
67}
68
69/// Decode MaxScaledValue attribute (0x0012)
70pub fn decode_max_scaled_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
71    if let tlv::TlvItemValue::Int(v) = inp {
72        Ok(Some(*v as i16))
73    } else {
74        Ok(None)
75    }
76}
77
78/// Decode ScaledTolerance attribute (0x0013)
79pub fn decode_scaled_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
80    if let tlv::TlvItemValue::Int(v) = inp {
81        Ok(*v as u16)
82    } else {
83        Err(anyhow::anyhow!("Expected UInt16"))
84    }
85}
86
87/// Decode Scale attribute (0x0014)
88pub fn decode_scale(inp: &tlv::TlvItemValue) -> anyhow::Result<i8> {
89    if let tlv::TlvItemValue::Int(v) = inp {
90        Ok(*v as i8)
91    } else {
92        Err(anyhow::anyhow!("Expected Int8"))
93    }
94}
95
96
97// JSON dispatcher function
98
99/// Decode attribute value and return as JSON string
100///
101/// # Parameters
102/// * `cluster_id` - The cluster identifier
103/// * `attribute_id` - The attribute identifier
104/// * `tlv_value` - The TLV value to decode
105///
106/// # Returns
107/// JSON string representation of the decoded value or error
108pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
109    // Verify this is the correct cluster
110    if cluster_id != 0x0403 {
111        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0403, got {}\"}}", cluster_id);
112    }
113
114    match attribute_id {
115        0x0000 => {
116            match decode_measured_value(tlv_value) {
117                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
118                Err(e) => format!("{{\"error\": \"{}\"}}", e),
119            }
120        }
121        0x0001 => {
122            match decode_min_measured_value(tlv_value) {
123                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
124                Err(e) => format!("{{\"error\": \"{}\"}}", e),
125            }
126        }
127        0x0002 => {
128            match decode_max_measured_value(tlv_value) {
129                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
130                Err(e) => format!("{{\"error\": \"{}\"}}", e),
131            }
132        }
133        0x0003 => {
134            match decode_tolerance(tlv_value) {
135                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
136                Err(e) => format!("{{\"error\": \"{}\"}}", e),
137            }
138        }
139        0x0010 => {
140            match decode_scaled_value(tlv_value) {
141                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
142                Err(e) => format!("{{\"error\": \"{}\"}}", e),
143            }
144        }
145        0x0011 => {
146            match decode_min_scaled_value(tlv_value) {
147                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
148                Err(e) => format!("{{\"error\": \"{}\"}}", e),
149            }
150        }
151        0x0012 => {
152            match decode_max_scaled_value(tlv_value) {
153                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
154                Err(e) => format!("{{\"error\": \"{}\"}}", e),
155            }
156        }
157        0x0013 => {
158            match decode_scaled_tolerance(tlv_value) {
159                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
160                Err(e) => format!("{{\"error\": \"{}\"}}", e),
161            }
162        }
163        0x0014 => {
164            match decode_scale(tlv_value) {
165                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
166                Err(e) => format!("{{\"error\": \"{}\"}}", e),
167            }
168        }
169        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
170    }
171}
172
173/// Get list of all attributes supported by this cluster
174///
175/// # Returns
176/// Vector of tuples containing (attribute_id, attribute_name)
177pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
178    vec![
179        (0x0000, "MeasuredValue"),
180        (0x0001, "MinMeasuredValue"),
181        (0x0002, "MaxMeasuredValue"),
182        (0x0003, "Tolerance"),
183        (0x0010, "ScaledValue"),
184        (0x0011, "MinScaledValue"),
185        (0x0012, "MaxScaledValue"),
186        (0x0013, "ScaledTolerance"),
187        (0x0014, "Scale"),
188    ]
189}
190
191// Typed facade (invokes + reads)
192
193/// Read `MeasuredValue` attribute from cluster `Pressure Measurement`.
194pub async fn read_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
195    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_MEASUREDVALUE).await?;
196    decode_measured_value(&tlv)
197}
198
199/// Read `MinMeasuredValue` attribute from cluster `Pressure Measurement`.
200pub async fn read_min_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
201    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_MINMEASUREDVALUE).await?;
202    decode_min_measured_value(&tlv)
203}
204
205/// Read `MaxMeasuredValue` attribute from cluster `Pressure Measurement`.
206pub async fn read_max_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
207    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_MAXMEASUREDVALUE).await?;
208    decode_max_measured_value(&tlv)
209}
210
211/// Read `Tolerance` attribute from cluster `Pressure Measurement`.
212pub async fn read_tolerance(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
213    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_TOLERANCE).await?;
214    decode_tolerance(&tlv)
215}
216
217/// Read `ScaledValue` attribute from cluster `Pressure Measurement`.
218pub async fn read_scaled_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
219    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_SCALEDVALUE).await?;
220    decode_scaled_value(&tlv)
221}
222
223/// Read `MinScaledValue` attribute from cluster `Pressure Measurement`.
224pub async fn read_min_scaled_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
225    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_MINSCALEDVALUE).await?;
226    decode_min_scaled_value(&tlv)
227}
228
229/// Read `MaxScaledValue` attribute from cluster `Pressure Measurement`.
230pub async fn read_max_scaled_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
231    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_MAXSCALEDVALUE).await?;
232    decode_max_scaled_value(&tlv)
233}
234
235/// Read `ScaledTolerance` attribute from cluster `Pressure Measurement`.
236pub async fn read_scaled_tolerance(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
237    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_SCALEDTOLERANCE).await?;
238    decode_scaled_tolerance(&tlv)
239}
240
241/// Read `Scale` attribute from cluster `Pressure Measurement`.
242pub async fn read_scale(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<i8> {
243    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_PRESSURE_MEASUREMENT, crate::clusters::defs::CLUSTER_PRESSURE_MEASUREMENT_ATTR_ID_SCALE).await?;
244    decode_scale(&tlv)
245}
246