matc/clusters/codec/
soil_measurement.rs

1//! Matter TLV encoders and decoders for Soil Measurement Cluster
2//! Cluster ID: 0x0430
3//!
4//! This file is automatically generated from SoilMeasurement.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 SoilMoistureMeasurementLimits attribute (0x0000)
16pub fn decode_soil_moisture_measurement_limits(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
17    if let tlv::TlvItemValue::Int(v) = inp {
18        Ok(*v as u8)
19    } else {
20        Err(anyhow::anyhow!("Expected UInt8"))
21    }
22}
23
24/// Decode SoilMoistureMeasuredValue attribute (0x0001)
25pub fn decode_soil_moisture_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
26    if let tlv::TlvItemValue::Int(v) = inp {
27        Ok(Some(*v as u8))
28    } else {
29        Ok(None)
30    }
31}
32
33
34// JSON dispatcher function
35
36/// Decode attribute value and return as JSON string
37///
38/// # Parameters
39/// * `cluster_id` - The cluster identifier
40/// * `attribute_id` - The attribute identifier
41/// * `tlv_value` - The TLV value to decode
42///
43/// # Returns
44/// JSON string representation of the decoded value or error
45pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
46    // Verify this is the correct cluster
47    if cluster_id != 0x0430 {
48        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0430, got {}\"}}", cluster_id);
49    }
50
51    match attribute_id {
52        0x0000 => {
53            match decode_soil_moisture_measurement_limits(tlv_value) {
54                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
55                Err(e) => format!("{{\"error\": \"{}\"}}", e),
56            }
57        }
58        0x0001 => {
59            match decode_soil_moisture_measured_value(tlv_value) {
60                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
61                Err(e) => format!("{{\"error\": \"{}\"}}", e),
62            }
63        }
64        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
65    }
66}
67
68/// Get list of all attributes supported by this cluster
69///
70/// # Returns
71/// Vector of tuples containing (attribute_id, attribute_name)
72pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
73    vec![
74        (0x0000, "SoilMoistureMeasurementLimits"),
75        (0x0001, "SoilMoistureMeasuredValue"),
76    ]
77}
78
79// Typed facade (invokes + reads)
80
81/// Read `SoilMoistureMeasurementLimits` attribute from cluster `Soil Measurement`.
82pub async fn read_soil_moisture_measurement_limits(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
83    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOIL_MEASUREMENT, crate::clusters::defs::CLUSTER_SOIL_MEASUREMENT_ATTR_ID_SOILMOISTUREMEASUREMENTLIMITS).await?;
84    decode_soil_moisture_measurement_limits(&tlv)
85}
86
87/// Read `SoilMoistureMeasuredValue` attribute from cluster `Soil Measurement`.
88pub async fn read_soil_moisture_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
89    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_SOIL_MEASUREMENT, crate::clusters::defs::CLUSTER_SOIL_MEASUREMENT_ATTR_ID_SOILMOISTUREMEASUREDVALUE).await?;
90    decode_soil_moisture_measured_value(&tlv)
91}
92