#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, serde::Serialize)]
pub struct Target {
pub node: Option<u64>,
pub group: Option<u8>,
pub endpoint: Option<u16>,
pub cluster: Option<u32>,
}
pub fn decode_binding(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<Target>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
res.push(Target {
node: item.get_int(&[1]),
group: item.get_int(&[2]).map(|v| v as u8),
endpoint: item.get_int(&[3]).map(|v| v as u16),
cluster: item.get_int(&[4]).map(|v| v as u32),
});
}
}
Ok(res)
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x001E {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x001E, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_binding(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, "Binding"),
]
}
pub async fn read_binding(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<Target>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_BINDING, crate::clusters::defs::CLUSTER_BINDING_ATTR_ID_BINDING).await?;
decode_binding(&tlv)
}