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
6#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13// Enum definitions
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum AirQuality {
18    /// The air quality is unknown.
19    Unknown = 0,
20    /// The air quality is good.
21    Good = 1,
22    /// The air quality is fair.
23    Fair = 2,
24    /// The air quality is moderate.
25    Moderate = 3,
26    /// The air quality is poor.
27    Poor = 4,
28    /// The air quality is very poor.
29    Verypoor = 5,
30    /// The air quality is extremely poor.
31    Extremelypoor = 6,
32}
33
34impl AirQuality {
35    /// Convert from u8 value
36    pub fn from_u8(value: u8) -> Option<Self> {
37        match value {
38            0 => Some(AirQuality::Unknown),
39            1 => Some(AirQuality::Good),
40            2 => Some(AirQuality::Fair),
41            3 => Some(AirQuality::Moderate),
42            4 => Some(AirQuality::Poor),
43            5 => Some(AirQuality::Verypoor),
44            6 => Some(AirQuality::Extremelypoor),
45            _ => None,
46        }
47    }
48
49    /// Convert to u8 value
50    pub fn to_u8(self) -> u8 {
51        self as u8
52    }
53}
54
55impl From<AirQuality> for u8 {
56    fn from(val: AirQuality) -> Self {
57        val as u8
58    }
59}
60
61// Attribute decoders
62
63/// Decode AirQuality attribute (0x0000)
64pub fn decode_air_quality(inp: &tlv::TlvItemValue) -> anyhow::Result<AirQuality> {
65    if let tlv::TlvItemValue::Int(v) = inp {
66        AirQuality::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
67    } else {
68        Err(anyhow::anyhow!("Expected Integer"))
69    }
70}
71
72
73// JSON dispatcher function
74
75/// Decode attribute value and return as JSON string
76///
77/// # Parameters
78/// * `cluster_id` - The cluster identifier
79/// * `attribute_id` - The attribute identifier
80/// * `tlv_value` - The TLV value to decode
81///
82/// # Returns
83/// JSON string representation of the decoded value or error
84pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
85    // Verify this is the correct cluster
86    if cluster_id != 0x005B {
87        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005B, got {}\"}}", cluster_id);
88    }
89
90    match attribute_id {
91        0x0000 => {
92            match decode_air_quality(tlv_value) {
93                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
94                Err(e) => format!("{{\"error\": \"{}\"}}", e),
95            }
96        }
97        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
98    }
99}
100
101/// Get list of all attributes supported by this cluster
102///
103/// # Returns
104/// Vector of tuples containing (attribute_id, attribute_name)
105pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
106    vec![
107        (0x0000, "AirQuality"),
108    ]
109}
110
111// Typed facade (invokes + reads)
112
113/// Read `AirQuality` attribute from cluster `Air Quality`.
114pub async fn read_air_quality(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<AirQuality> {
115    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_AIR_QUALITY, crate::clusters::defs::CLUSTER_AIR_QUALITY_ATTR_ID_AIRQUALITY).await?;
116    decode_air_quality(&tlv)
117}
118