matc/clusters/codec/
flow_measurement.rs

1//! Matter TLV encoders and decoders for Flow Measurement Cluster
2//! Cluster ID: 0x0404
3//!
4//! This file is automatically generated from FlowMeasurement.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<u16>> {
17    if let tlv::TlvItemValue::Int(v) = inp {
18        Ok(Some(*v as u16))
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<u16>> {
26    if let tlv::TlvItemValue::Int(v) = inp {
27        Ok(Some(*v as u16))
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<u16>> {
35    if let tlv::TlvItemValue::Int(v) = inp {
36        Ok(Some(*v as u16))
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
52// JSON dispatcher function
53
54/// Decode attribute value and return as JSON string
55///
56/// # Parameters
57/// * `cluster_id` - The cluster identifier
58/// * `attribute_id` - The attribute identifier
59/// * `tlv_value` - The TLV value to decode
60///
61/// # Returns
62/// JSON string representation of the decoded value or error
63pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
64    // Verify this is the correct cluster
65    if cluster_id != 0x0404 {
66        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0404, got {}\"}}", cluster_id);
67    }
68
69    match attribute_id {
70        0x0000 => {
71            match decode_measured_value(tlv_value) {
72                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
73                Err(e) => format!("{{\"error\": \"{}\"}}", e),
74            }
75        }
76        0x0001 => {
77            match decode_min_measured_value(tlv_value) {
78                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
79                Err(e) => format!("{{\"error\": \"{}\"}}", e),
80            }
81        }
82        0x0002 => {
83            match decode_max_measured_value(tlv_value) {
84                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
85                Err(e) => format!("{{\"error\": \"{}\"}}", e),
86            }
87        }
88        0x0003 => {
89            match decode_tolerance(tlv_value) {
90                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
91                Err(e) => format!("{{\"error\": \"{}\"}}", e),
92            }
93        }
94        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
95    }
96}
97
98/// Get list of all attributes supported by this cluster
99///
100/// # Returns
101/// Vector of tuples containing (attribute_id, attribute_name)
102pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
103    vec![
104        (0x0000, "MeasuredValue"),
105        (0x0001, "MinMeasuredValue"),
106        (0x0002, "MaxMeasuredValue"),
107        (0x0003, "Tolerance"),
108    ]
109}
110
111// Typed facade (invokes + reads)
112
113/// Read `MeasuredValue` attribute from cluster `Flow Measurement`.
114pub async fn read_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
115    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FLOW_MEASUREMENT, crate::clusters::defs::CLUSTER_FLOW_MEASUREMENT_ATTR_ID_MEASUREDVALUE).await?;
116    decode_measured_value(&tlv)
117}
118
119/// Read `MinMeasuredValue` attribute from cluster `Flow Measurement`.
120pub async fn read_min_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
121    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FLOW_MEASUREMENT, crate::clusters::defs::CLUSTER_FLOW_MEASUREMENT_ATTR_ID_MINMEASUREDVALUE).await?;
122    decode_min_measured_value(&tlv)
123}
124
125/// Read `MaxMeasuredValue` attribute from cluster `Flow Measurement`.
126pub async fn read_max_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
127    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FLOW_MEASUREMENT, crate::clusters::defs::CLUSTER_FLOW_MEASUREMENT_ATTR_ID_MAXMEASUREDVALUE).await?;
128    decode_max_measured_value(&tlv)
129}
130
131/// Read `Tolerance` attribute from cluster `Flow Measurement`.
132pub async fn read_tolerance(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
133    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FLOW_MEASUREMENT, crate::clusters::defs::CLUSTER_FLOW_MEASUREMENT_ATTR_ID_TOLERANCE).await?;
134    decode_tolerance(&tlv)
135}
136