matc/clusters/codec/
localization_time_format.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_hour_format(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_active_calendar_type(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_supported_calendar_types(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u8>> {
33 let mut res = Vec::new();
34 if let tlv::TlvItemValue::List(v) = inp {
35 for item in v {
36 if let tlv::TlvItemValue::Int(i) = &item.value {
37 res.push(*i as u8);
38 }
39 }
40 }
41 Ok(res)
42}
43
44
45pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
57 if cluster_id != 0x002C {
59 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002C, got {}\"}}", cluster_id);
60 }
61
62 match attribute_id {
63 0x0000 => {
64 match decode_hour_format(tlv_value) {
65 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
66 Err(e) => format!("{{\"error\": \"{}\"}}", e),
67 }
68 }
69 0x0001 => {
70 match decode_active_calendar_type(tlv_value) {
71 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
72 Err(e) => format!("{{\"error\": \"{}\"}}", e),
73 }
74 }
75 0x0002 => {
76 match decode_supported_calendar_types(tlv_value) {
77 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
78 Err(e) => format!("{{\"error\": \"{}\"}}", e),
79 }
80 }
81 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
82 }
83}
84
85pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
90 vec![
91 (0x0000, "HourFormat"),
92 (0x0001, "ActiveCalendarType"),
93 (0x0002, "SupportedCalendarTypes"),
94 ]
95}
96