#![allow(clippy::too_many_arguments)]
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"),
]
}
pub fn get_command_list() -> Vec<(u32, &'static str)> {
vec![
(0x00, "Reset"),
(0x01, "ModifyEnabledAlarms"),
]
}
pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
match cmd_id {
0x00 => Some("Reset"),
0x01 => Some("ModifyEnabledAlarms"),
_ => None,
}
}
pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
match cmd_id {
0x00 => Some(vec![
crate::clusters::codec::CommandField { tag: 0, name: "alarms", kind: crate::clusters::codec::FieldKind::Bitmap { name: "Alarm", bits: &[] }, optional: false, nullable: false },
]),
0x01 => Some(vec![
crate::clusters::codec::CommandField { tag: 0, name: "mask", kind: crate::clusters::codec::FieldKind::Bitmap { name: "Alarm", bits: &[] }, optional: false, nullable: false },
]),
_ => None,
}
}
pub fn encode_command_json(cmd_id: u32, args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
match cmd_id {
0x00 => {
let alarms = crate::clusters::codec::json_util::get_u8(args, "alarms")?;
encode_reset(alarms)
}
0x01 => {
let mask = crate::clusters::codec::json_util::get_u8(args, "mask")?;
encode_modify_enabled_alarms(mask)
}
_ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
}
}
#[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"))
}
}