matc/clusters/codec/
illuminance_measurement.rs

1//! Generated Matter TLV encoders and decoders for Illuminance Measurement Cluster
2//! Cluster ID: 0x0400
3//! 
4//! This file is automatically generated from IlluminanceMeasurement.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<u16>> {
15    if let tlv::TlvItemValue::Int(v) = inp {
16        Ok(Some(*v as u16))
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<u16>> {
24    if let tlv::TlvItemValue::Int(v) = inp {
25        Ok(Some(*v as u16))
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<u16>> {
33    if let tlv::TlvItemValue::Int(v) = inp {
34        Ok(Some(*v as u16))
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 LightSensorType attribute (0x0004)
50pub fn decode_light_sensor_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
51    if let tlv::TlvItemValue::Int(v) = inp {
52        Ok(Some(*v as u8))
53    } else {
54        Ok(None)
55    }
56}
57
58
59// JSON dispatcher function
60
61/// Decode attribute value and return as JSON string
62/// 
63/// # Parameters
64/// * `cluster_id` - The cluster identifier
65/// * `attribute_id` - The attribute identifier
66/// * `tlv_value` - The TLV value to decode
67/// 
68/// # Returns
69/// JSON string representation of the decoded value or error
70pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
71    // Verify this is the correct cluster
72    if cluster_id != 0x0400 {
73        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0400, got {}\"}}", cluster_id);
74    }
75    
76    match attribute_id {
77        0x0000 => {
78            match decode_measured_value(tlv_value) {
79                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
80                Err(e) => format!("{{\"error\": \"{}\"}}", e),
81            }
82        }
83        0x0001 => {
84            match decode_min_measured_value(tlv_value) {
85                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
86                Err(e) => format!("{{\"error\": \"{}\"}}", e),
87            }
88        }
89        0x0002 => {
90            match decode_max_measured_value(tlv_value) {
91                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
92                Err(e) => format!("{{\"error\": \"{}\"}}", e),
93            }
94        }
95        0x0003 => {
96            match decode_tolerance(tlv_value) {
97                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
98                Err(e) => format!("{{\"error\": \"{}\"}}", e),
99            }
100        }
101        0x0004 => {
102            match decode_light_sensor_type(tlv_value) {
103                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
104                Err(e) => format!("{{\"error\": \"{}\"}}", e),
105            }
106        }
107        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
108    }
109}
110
111/// Get list of all attributes supported by this cluster
112/// 
113/// # Returns
114/// Vector of tuples containing (attribute_id, attribute_name)
115pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
116    vec![
117        (0x0000, "MeasuredValue"),
118        (0x0001, "MinMeasuredValue"),
119        (0x0002, "MaxMeasuredValue"),
120        (0x0003, "Tolerance"),
121        (0x0004, "LightSensorType"),
122    ]
123}
124