matc/clusters/codec/
air_quality.rs

1//! Matter TLV encoders and decoders for Air Quality Cluster
2//! Cluster ID: 0x005B
3//!
4//! This file is automatically generated from AirQuality.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 AirQuality {
16    /// The air quality is unknown.
17    Unknown = 0,
18    /// The air quality is good.
19    Good = 1,
20    /// The air quality is fair.
21    Fair = 2,
22    /// The air quality is moderate.
23    Moderate = 3,
24    /// The air quality is poor.
25    Poor = 4,
26    /// The air quality is very poor.
27    Verypoor = 5,
28    /// The air quality is extremely poor.
29    Extremelypoor = 6,
30}
31
32impl AirQuality {
33    /// Convert from u8 value
34    pub fn from_u8(value: u8) -> Option<Self> {
35        match value {
36            0 => Some(AirQuality::Unknown),
37            1 => Some(AirQuality::Good),
38            2 => Some(AirQuality::Fair),
39            3 => Some(AirQuality::Moderate),
40            4 => Some(AirQuality::Poor),
41            5 => Some(AirQuality::Verypoor),
42            6 => Some(AirQuality::Extremelypoor),
43            _ => None,
44        }
45    }
46
47    /// Convert to u8 value
48    pub fn to_u8(self) -> u8 {
49        self as u8
50    }
51}
52
53impl From<AirQuality> for u8 {
54    fn from(val: AirQuality) -> Self {
55        val as u8
56    }
57}
58
59// Attribute decoders
60
61/// Decode AirQuality attribute (0x0000)
62pub fn decode_air_quality(inp: &tlv::TlvItemValue) -> anyhow::Result<AirQuality> {
63    if let tlv::TlvItemValue::Int(v) = inp {
64        AirQuality::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
65    } else {
66        Err(anyhow::anyhow!("Expected Integer"))
67    }
68}
69
70
71// JSON dispatcher function
72
73/// Decode attribute value and return as JSON string
74///
75/// # Parameters
76/// * `cluster_id` - The cluster identifier
77/// * `attribute_id` - The attribute identifier
78/// * `tlv_value` - The TLV value to decode
79///
80/// # Returns
81/// JSON string representation of the decoded value or error
82pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
83    // Verify this is the correct cluster
84    if cluster_id != 0x005B {
85        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005B, got {}\"}}", cluster_id);
86    }
87
88    match attribute_id {
89        0x0000 => {
90            match decode_air_quality(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        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
96    }
97}
98
99/// Get list of all attributes supported by this cluster
100///
101/// # Returns
102/// Vector of tuples containing (attribute_id, attribute_name)
103pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
104    vec![
105        (0x0000, "AirQuality"),
106    ]
107}
108