matc/clusters/codec/
illuminance_measurement.rs

1//! 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// Enum definitions
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum LightSensorType {
16    /// Indicates photodiode sensor type
17    Photodiode = 0,
18    /// Indicates CMOS sensor type
19    Cmos = 1,
20}
21
22impl LightSensorType {
23    /// Convert from u8 value
24    pub fn from_u8(value: u8) -> Option<Self> {
25        match value {
26            0 => Some(LightSensorType::Photodiode),
27            1 => Some(LightSensorType::Cmos),
28            _ => None,
29        }
30    }
31
32    /// Convert to u8 value
33    pub fn to_u8(self) -> u8 {
34        self as u8
35    }
36}
37
38impl From<LightSensorType> for u8 {
39    fn from(val: LightSensorType) -> Self {
40        val as u8
41    }
42}
43
44// Attribute decoders
45
46/// Decode MeasuredValue attribute (0x0000)
47pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
48    if let tlv::TlvItemValue::Int(v) = inp {
49        Ok(Some(*v as u16))
50    } else {
51        Ok(None)
52    }
53}
54
55/// Decode MinMeasuredValue attribute (0x0001)
56pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
57    if let tlv::TlvItemValue::Int(v) = inp {
58        Ok(Some(*v as u16))
59    } else {
60        Ok(None)
61    }
62}
63
64/// Decode MaxMeasuredValue attribute (0x0002)
65pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
66    if let tlv::TlvItemValue::Int(v) = inp {
67        Ok(Some(*v as u16))
68    } else {
69        Ok(None)
70    }
71}
72
73/// Decode Tolerance attribute (0x0003)
74pub fn decode_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
75    if let tlv::TlvItemValue::Int(v) = inp {
76        Ok(*v as u16)
77    } else {
78        Err(anyhow::anyhow!("Expected UInt16"))
79    }
80}
81
82/// Decode LightSensorType attribute (0x0004)
83pub fn decode_light_sensor_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<LightSensorType>> {
84    if let tlv::TlvItemValue::Int(v) = inp {
85        Ok(LightSensorType::from_u8(*v as u8))
86    } else {
87        Ok(None)
88    }
89}
90
91
92// JSON dispatcher function
93
94/// Decode attribute value and return as JSON string
95///
96/// # Parameters
97/// * `cluster_id` - The cluster identifier
98/// * `attribute_id` - The attribute identifier
99/// * `tlv_value` - The TLV value to decode
100///
101/// # Returns
102/// JSON string representation of the decoded value or error
103pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
104    // Verify this is the correct cluster
105    if cluster_id != 0x0400 {
106        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0400, got {}\"}}", cluster_id);
107    }
108
109    match attribute_id {
110        0x0000 => {
111            match decode_measured_value(tlv_value) {
112                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113                Err(e) => format!("{{\"error\": \"{}\"}}", e),
114            }
115        }
116        0x0001 => {
117            match decode_min_measured_value(tlv_value) {
118                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
119                Err(e) => format!("{{\"error\": \"{}\"}}", e),
120            }
121        }
122        0x0002 => {
123            match decode_max_measured_value(tlv_value) {
124                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
125                Err(e) => format!("{{\"error\": \"{}\"}}", e),
126            }
127        }
128        0x0003 => {
129            match decode_tolerance(tlv_value) {
130                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
131                Err(e) => format!("{{\"error\": \"{}\"}}", e),
132            }
133        }
134        0x0004 => {
135            match decode_light_sensor_type(tlv_value) {
136                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137                Err(e) => format!("{{\"error\": \"{}\"}}", e),
138            }
139        }
140        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
141    }
142}
143
144/// Get list of all attributes supported by this cluster
145///
146/// # Returns
147/// Vector of tuples containing (attribute_id, attribute_name)
148pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
149    vec![
150        (0x0000, "MeasuredValue"),
151        (0x0001, "MinMeasuredValue"),
152        (0x0002, "MaxMeasuredValue"),
153        (0x0003, "Tolerance"),
154        (0x0004, "LightSensorType"),
155    ]
156}
157