matc/clusters/codec/
power_topology.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
17 let mut res = Vec::new();
18 if let tlv::TlvItemValue::List(v) = inp {
19 for item in v {
20 if let tlv::TlvItemValue::Int(i) = &item.value {
21 res.push(*i as u16);
22 }
23 }
24 }
25 Ok(res)
26}
27
28pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
30 let mut res = Vec::new();
31 if let tlv::TlvItemValue::List(v) = inp {
32 for item in v {
33 if let tlv::TlvItemValue::Int(i) = &item.value {
34 res.push(*i as u16);
35 }
36 }
37 }
38 Ok(res)
39}
40
41
42pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
54 if ![0x009C].contains(&cluster_id) {
56 return format!("{{\"error\": \"Invalid cluster ID. Expected [0x009C], got {}\"}}", cluster_id);
57 }
58
59 match attribute_id {
60 0x0000 => {
61 match decode_available_endpoints(tlv_value) {
62 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
63 Err(e) => format!("{{\"error\": \"{}\"}}", e),
64 }
65 }
66 0x0001 => {
67 match decode_active_endpoints(tlv_value) {
68 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
69 Err(e) => format!("{{\"error\": \"{}\"}}", e),
70 }
71 }
72 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
73 }
74}
75
76pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
81 vec![
82 (0x0000, "AvailableEndpoints"),
83 (0x0001, "ActiveEndpoints"),
84 ]
85}
86
87pub async fn read_available_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
91 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_AVAILABLEENDPOINTS).await?;
92 decode_available_endpoints(&tlv)
93}
94
95pub async fn read_active_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
97 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_ACTIVEENDPOINTS).await?;
98 decode_active_endpoints(&tlv)
99}
100