matc/clusters/codec/
air_quality.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum AirQuality {
18 Unknown = 0,
20 Good = 1,
22 Fair = 2,
24 Moderate = 3,
26 Poor = 4,
28 Verypoor = 5,
30 Extremelypoor = 6,
32}
33
34impl AirQuality {
35 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 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
61pub 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
73pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
85 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
101pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
106 vec![
107 (0x0000, "AirQuality"),
108 ]
109}
110
111pub 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