matc/clusters/codec/
mode_rvc_run.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct ModeOption {
15 pub label: Option<u8>,
16 pub mode: Option<u8>,
17 pub mode_tags: Option<u8>,
18}
19
20pub fn decode_supported_modes(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_current_mode(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
40pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
42 if let tlv::TlvItemValue::Int(v) = inp {
43 Ok(*v as u8)
44 } else {
45 Err(anyhow::anyhow!("Expected Integer"))
46 }
47}
48
49pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
51 if let tlv::TlvItemValue::Int(v) = inp {
52 Ok(*v as u8)
53 } else {
54 Err(anyhow::anyhow!("Expected Integer"))
55 }
56}
57
58
59pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
71 if cluster_id != 0x0054 {
73 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0054, got {}\"}}", cluster_id);
74 }
75
76 match attribute_id {
77 0x0000 => {
78 match decode_supported_modes(tlv_value) {
79 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
80 Err(e) => format!("{{\"error\": \"{}\"}}", e),
81 }
82 }
83 0x0001 => {
84 match decode_current_mode(tlv_value) {
85 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
86 Err(e) => format!("{{\"error\": \"{}\"}}", e),
87 }
88 }
89 0x0002 => {
90 match decode_start_up_mode(tlv_value) {
91 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
92 Err(e) => format!("{{\"error\": \"{}\"}}", e),
93 }
94 }
95 0x0003 => {
96 match decode_on_mode(tlv_value) {
97 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
98 Err(e) => format!("{{\"error\": \"{}\"}}", e),
99 }
100 }
101 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
102 }
103}
104
105pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
110 vec![
111 (0x0000, "SupportedModes"),
112 (0x0001, "CurrentMode"),
113 (0x0002, "StartUpMode"),
114 (0x0003, "OnMode"),
115 ]
116}
117