use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, serde::Serialize)]
pub struct ChimeSound {
pub chime_id: Option<u8>,
pub name: Option<String>,
}
pub fn decode_installed_chime_sounds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ChimeSound>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
res.push(ChimeSound {
chime_id: item.get_int(&[0]).map(|v| v as u8),
name: item.get_string_owned(&[1]),
});
}
}
Ok(res)
}
pub fn decode_selected_chime(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0556 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0556, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_installed_chime_sounds(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_selected_chime(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_enabled(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, "InstalledChimeSounds"),
(0x0001, "SelectedChime"),
(0x0002, "Enabled"),
]
}