matc/clusters/codec/
fixed_label_cluster.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub fn decode_label_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
17 let mut res = Vec::new();
18 if let tlv::TlvItemValue::List(v) = inp {
19 for item in v {
20 if let tlv::TlvItemValue::Int(i) = &item.value {
21 res.push(*i as u8);
22 }
23 }
24 }
25 Ok(res)
26}
27
28
29pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
41 if cluster_id != 0x0040 {
43 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0040, got {}\"}}", cluster_id);
44 }
45
46 match attribute_id {
47 0x0000 => {
48 match decode_label_list(tlv_value) {
49 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
50 Err(e) => format!("{{\"error\": \"{}\"}}", e),
51 }
52 }
53 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
54 }
55}
56
57pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
62 vec![
63 (0x0000, "LabelList"),
64 ]
65}
66
67pub async fn read_label_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u8>> {
71 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_FIXED_LABEL, crate::clusters::defs::CLUSTER_FIXED_LABEL_ATTR_ID_LABELLIST).await?;
72 decode_label_list(&tlv)
73}
74