matc/clusters/codec/
boolean_state.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub fn decode_state_value(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
17 if let tlv::TlvItemValue::Bool(v) = inp {
18 Ok(*v)
19 } else {
20 Err(anyhow::anyhow!("Expected Bool"))
21 }
22}
23
24
25pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
37 if cluster_id != 0x0045 {
39 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0045, got {}\"}}", cluster_id);
40 }
41
42 match attribute_id {
43 0x0000 => {
44 match decode_state_value(tlv_value) {
45 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
46 Err(e) => format!("{{\"error\": \"{}\"}}", e),
47 }
48 }
49 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
50 }
51}
52
53pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
58 vec![
59 (0x0000, "StateValue"),
60 ]
61}
62
63pub async fn read_state_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
67 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_BOOLEAN_STATE, crate::clusters::defs::CLUSTER_BOOLEAN_STATE_ATTR_ID_STATEVALUE).await?;
68 decode_state_value(&tlv)
69}
70
71#[derive(Debug, serde::Serialize)]
72pub struct StateChangeEvent {
73 pub state_value: Option<bool>,
74}
75
76pub fn decode_state_change_event(inp: &tlv::TlvItemValue) -> anyhow::Result<StateChangeEvent> {
80 if let tlv::TlvItemValue::List(_fields) = inp {
81 let item = tlv::TlvItem { tag: 0, value: inp.clone() };
82 Ok(StateChangeEvent {
83 state_value: item.get_bool(&[0]),
84 })
85 } else {
86 Err(anyhow::anyhow!("Expected struct fields"))
87 }
88}
89