matc/clusters/codec/
mode_microwave_oven.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
17 if let tlv::TlvItemValue::Int(v) = inp {
18 Ok(*v as u8)
19 } else {
20 Err(anyhow::anyhow!("Expected Integer"))
21 }
22}
23
24pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
26 if let tlv::TlvItemValue::Int(v) = inp {
27 Ok(*v as u8)
28 } else {
29 Err(anyhow::anyhow!("Expected Integer"))
30 }
31}
32
33pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
35 if let tlv::TlvItemValue::Int(v) = inp {
36 Ok(*v as u8)
37 } else {
38 Err(anyhow::anyhow!("Expected Integer"))
39 }
40}
41
42pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
44 if let tlv::TlvItemValue::Int(v) = inp {
45 Ok(*v as u8)
46 } else {
47 Err(anyhow::anyhow!("Expected Integer"))
48 }
49}
50
51
52pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
64 if cluster_id != 0x005E {
66 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005E, got {}\"}}", cluster_id);
67 }
68
69 match attribute_id {
70 0x0000 => {
71 match decode_supported_modes(tlv_value) {
72 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
73 Err(e) => format!("{{\"error\": \"{}\"}}", e),
74 }
75 }
76 0x0001 => {
77 match decode_current_mode(tlv_value) {
78 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
79 Err(e) => format!("{{\"error\": \"{}\"}}", e),
80 }
81 }
82 0x0002 => {
83 match decode_start_up_mode(tlv_value) {
84 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
85 Err(e) => format!("{{\"error\": \"{}\"}}", e),
86 }
87 }
88 0x0003 => {
89 match decode_on_mode(tlv_value) {
90 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
91 Err(e) => format!("{{\"error\": \"{}\"}}", e),
92 }
93 }
94 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
95 }
96}
97
98pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
103 vec![
104 (0x0000, "SupportedModes"),
105 (0x0001, "CurrentMode"),
106 (0x0002, "StartUpMode"),
107 (0x0003, "OnMode"),
108 ]
109}
110