matc/clusters/codec/
power_topology.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, serde::Serialize)]
16pub struct CircuitNode {
17 pub node: Option<u64>,
18 pub endpoint: Option<u16>,
19 pub label: Option<String>,
20}
21
22pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
26 let mut res = Vec::new();
27 if let tlv::TlvItemValue::List(v) = inp {
28 for item in v {
29 if let tlv::TlvItemValue::Int(i) = &item.value {
30 res.push(*i as u16);
31 }
32 }
33 }
34 Ok(res)
35}
36
37pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
39 let mut res = Vec::new();
40 if let tlv::TlvItemValue::List(v) = inp {
41 for item in v {
42 if let tlv::TlvItemValue::Int(i) = &item.value {
43 res.push(*i as u16);
44 }
45 }
46 }
47 Ok(res)
48}
49
50
51pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
63 if cluster_id != 0x009C {
65 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009C, got {}\"}}", cluster_id);
66 }
67
68 match attribute_id {
69 0x0000 => {
70 match decode_available_endpoints(tlv_value) {
71 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
72 Err(e) => format!("{{\"error\": \"{}\"}}", e),
73 }
74 }
75 0x0001 => {
76 match decode_active_endpoints(tlv_value) {
77 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
78 Err(e) => format!("{{\"error\": \"{}\"}}", e),
79 }
80 }
81 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
82 }
83}
84
85pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
90 vec![
91 (0x0000, "AvailableEndpoints"),
92 (0x0001, "ActiveEndpoints"),
93 ]
94}
95
96pub async fn read_available_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
100 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_AVAILABLEENDPOINTS).await?;
101 decode_available_endpoints(&tlv)
102}
103
104pub async fn read_active_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
106 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_ACTIVEENDPOINTS).await?;
107 decode_active_endpoints(&tlv)
108}
109