matc/clusters/codec/
switch.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_number_of_positions(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
15 if let tlv::TlvItemValue::Int(v) = inp {
16 Ok(*v as u8)
17 } else {
18 Err(anyhow::anyhow!("Expected Integer"))
19 }
20}
21
22pub fn decode_current_position(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
24 if let tlv::TlvItemValue::Int(v) = inp {
25 Ok(*v as u8)
26 } else {
27 Err(anyhow::anyhow!("Expected Integer"))
28 }
29}
30
31pub fn decode_multi_press_max(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
33 if let tlv::TlvItemValue::Int(v) = inp {
34 Ok(*v as u8)
35 } else {
36 Err(anyhow::anyhow!("Expected Integer"))
37 }
38}
39
40
41pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
53 if cluster_id != 0x003B {
55 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x003B, got {}\"}}", cluster_id);
56 }
57
58 match attribute_id {
59 0x0000 => {
60 match decode_number_of_positions(tlv_value) {
61 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
62 Err(e) => format!("{{\"error\": \"{}\"}}", e),
63 }
64 }
65 0x0001 => {
66 match decode_current_position(tlv_value) {
67 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
68 Err(e) => format!("{{\"error\": \"{}\"}}", e),
69 }
70 }
71 0x0002 => {
72 match decode_multi_press_max(tlv_value) {
73 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
74 Err(e) => format!("{{\"error\": \"{}\"}}", e),
75 }
76 }
77 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
78 }
79}
80
81pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
86 vec![
87 (0x0000, "NumberOfPositions"),
88 (0x0001, "CurrentPosition"),
89 (0x0002, "MultiPressMax"),
90 ]
91}
92