matc/clusters/codec/
boolean_state_configuration.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_suppress_alarm(alarms_to_suppress: u8) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt8(alarms_to_suppress)).into(),
19 ]),
20 };
21 Ok(tlv.encode()?)
22}
23
24pub fn encode_enable_disable_alarm(alarms_to_enable_disable: u8) -> anyhow::Result<Vec<u8>> {
26 let tlv = tlv::TlvItemEnc {
27 tag: 0,
28 value: tlv::TlvItemValueEnc::StructInvisible(vec![
29 (0, tlv::TlvItemValueEnc::UInt8(alarms_to_enable_disable)).into(),
30 ]),
31 };
32 Ok(tlv.encode()?)
33}
34
35pub fn decode_current_sensitivity_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
39 if let tlv::TlvItemValue::Int(v) = inp {
40 Ok(*v as u8)
41 } else {
42 Err(anyhow::anyhow!("Expected Integer"))
43 }
44}
45
46pub fn decode_supported_sensitivity_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
48 if let tlv::TlvItemValue::Int(v) = inp {
49 Ok(*v as u8)
50 } else {
51 Err(anyhow::anyhow!("Expected Integer"))
52 }
53}
54
55pub fn decode_default_sensitivity_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
57 if let tlv::TlvItemValue::Int(v) = inp {
58 Ok(*v as u8)
59 } else {
60 Err(anyhow::anyhow!("Expected Integer"))
61 }
62}
63
64pub fn decode_alarms_active(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
66 if let tlv::TlvItemValue::Int(v) = inp {
67 Ok(*v as u8)
68 } else {
69 Err(anyhow::anyhow!("Expected Integer"))
70 }
71}
72
73pub fn decode_alarms_suppressed(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
75 if let tlv::TlvItemValue::Int(v) = inp {
76 Ok(*v as u8)
77 } else {
78 Err(anyhow::anyhow!("Expected Integer"))
79 }
80}
81
82pub fn decode_alarms_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
84 if let tlv::TlvItemValue::Int(v) = inp {
85 Ok(*v as u8)
86 } else {
87 Err(anyhow::anyhow!("Expected Integer"))
88 }
89}
90
91pub fn decode_alarms_supported(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
93 if let tlv::TlvItemValue::Int(v) = inp {
94 Ok(*v as u8)
95 } else {
96 Err(anyhow::anyhow!("Expected Integer"))
97 }
98}
99
100pub fn decode_sensor_fault(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
102 if let tlv::TlvItemValue::Int(v) = inp {
103 Ok(*v as u8)
104 } else {
105 Err(anyhow::anyhow!("Expected Integer"))
106 }
107}
108
109
110pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
122 if cluster_id != 0x0080 {
124 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0080, got {}\"}}", cluster_id);
125 }
126
127 match attribute_id {
128 0x0000 => {
129 match decode_current_sensitivity_level(tlv_value) {
130 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
131 Err(e) => format!("{{\"error\": \"{}\"}}", e),
132 }
133 }
134 0x0001 => {
135 match decode_supported_sensitivity_levels(tlv_value) {
136 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137 Err(e) => format!("{{\"error\": \"{}\"}}", e),
138 }
139 }
140 0x0002 => {
141 match decode_default_sensitivity_level(tlv_value) {
142 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
143 Err(e) => format!("{{\"error\": \"{}\"}}", e),
144 }
145 }
146 0x0003 => {
147 match decode_alarms_active(tlv_value) {
148 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
149 Err(e) => format!("{{\"error\": \"{}\"}}", e),
150 }
151 }
152 0x0004 => {
153 match decode_alarms_suppressed(tlv_value) {
154 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
155 Err(e) => format!("{{\"error\": \"{}\"}}", e),
156 }
157 }
158 0x0005 => {
159 match decode_alarms_enabled(tlv_value) {
160 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
161 Err(e) => format!("{{\"error\": \"{}\"}}", e),
162 }
163 }
164 0x0006 => {
165 match decode_alarms_supported(tlv_value) {
166 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
167 Err(e) => format!("{{\"error\": \"{}\"}}", e),
168 }
169 }
170 0x0007 => {
171 match decode_sensor_fault(tlv_value) {
172 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
173 Err(e) => format!("{{\"error\": \"{}\"}}", e),
174 }
175 }
176 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
177 }
178}
179
180pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
185 vec![
186 (0x0000, "CurrentSensitivityLevel"),
187 (0x0001, "SupportedSensitivityLevels"),
188 (0x0002, "DefaultSensitivityLevel"),
189 (0x0003, "AlarmsActive"),
190 (0x0004, "AlarmsSuppressed"),
191 (0x0005, "AlarmsEnabled"),
192 (0x0006, "AlarmsSupported"),
193 (0x0007, "SensorFault"),
194 ]
195}
196