matc/clusters/codec/
identify.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_identify(identify_time: u16) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt16(identify_time)).into(),
19 ]),
20 };
21 Ok(tlv.encode()?)
22}
23
24pub fn encode_trigger_effect(effect_identifier: u8, effect_variant: u8) -> anyhow::Result<Vec<u8>> {
26 let tlv = tlv::TlvItemEnc {
27 tag: 0,
28 value: tlv::TlvItemValueEnc::StructInvisible(vec![
29 (0, tlv::TlvItemValueEnc::UInt8(effect_identifier)).into(),
30 (1, tlv::TlvItemValueEnc::UInt8(effect_variant)).into(),
31 ]),
32 };
33 Ok(tlv.encode()?)
34}
35
36pub fn decode_identify_time(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
40 if let tlv::TlvItemValue::Int(v) = inp {
41 Ok(*v as u16)
42 } else {
43 Err(anyhow::anyhow!("Expected Integer"))
44 }
45}
46
47pub fn decode_identify_type(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
49 if let tlv::TlvItemValue::Int(v) = inp {
50 Ok(*v as u8)
51 } else {
52 Err(anyhow::anyhow!("Expected Integer"))
53 }
54}
55
56
57pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
69 if cluster_id != 0x0003 {
71 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0003, got {}\"}}", cluster_id);
72 }
73
74 match attribute_id {
75 0x0000 => {
76 match decode_identify_time(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 0x0001 => {
82 match decode_identify_type(tlv_value) {
83 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
84 Err(e) => format!("{{\"error\": \"{}\"}}", e),
85 }
86 }
87 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
88 }
89}
90
91pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
96 vec![
97 (0x0000, "IdentifyTime"),
98 (0x0001, "IdentifyType"),
99 ]
100}
101