use crate::tlv;
use anyhow;
use serde_json;
pub type Alarm = u8;
pub fn encode_reset(alarms: Alarm) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(alarms)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_modify_enabled_alarms(mask: Alarm) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(mask)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn decode_mask(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_latch(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_state(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_supported(inp: &tlv::TlvItemValue) -> anyhow::Result<Alarm> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0000 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0000, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_mask(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_latch(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_state(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_supported(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, "Mask"),
(0x0001, "Latch"),
(0x0002, "State"),
(0x0003, "Supported"),
]
}
#[derive(Debug, serde::Serialize)]
pub struct NotifyEvent {
pub active: Option<Alarm>,
pub inactive: Option<Alarm>,
pub state: Option<Alarm>,
pub mask: Option<Alarm>,
}
pub fn decode_notify_event(inp: &tlv::TlvItemValue) -> anyhow::Result<NotifyEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(NotifyEvent {
active: item.get_int(&[0]).map(|v| v as u8),
inactive: item.get_int(&[1]).map(|v| v as u8),
state: item.get_int(&[2]).map(|v| v as u8),
mask: item.get_int(&[3]).map(|v| v as u8),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}