matc/clusters/codec/
air_quality.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum AirQuality {
16 Unknown = 0,
18 Good = 1,
20 Fair = 2,
22 Moderate = 3,
24 Poor = 4,
26 Verypoor = 5,
28 Extremelypoor = 6,
30}
31
32impl AirQuality {
33 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 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
59pub 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
71pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
83 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
99pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
104 vec![
105 (0x0000, "AirQuality"),
106 ]
107}
108