#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, serde::Serialize)]
pub struct CircuitNode {
pub node: Option<u64>,
pub endpoint: Option<u16>,
pub label: Option<String>,
}
pub fn decode_available_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u16);
}
}
}
Ok(res)
}
pub fn decode_active_endpoints(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u16);
}
}
}
Ok(res)
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x009C {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009C, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_available_endpoints(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_active_endpoints(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
_ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "AvailableEndpoints"),
(0x0001, "ActiveEndpoints"),
]
}
pub async fn read_available_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_AVAILABLEENDPOINTS).await?;
decode_available_endpoints(&tlv)
}
pub async fn read_active_endpoints(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_TOPOLOGY, crate::clusters::defs::CLUSTER_POWER_TOPOLOGY_ATTR_ID_ACTIVEENDPOINTS).await?;
decode_active_endpoints(&tlv)
}