#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum OccupancySensorType {
Pir = 0,
Ultrasonic = 1,
Pirandultrasonic = 2,
Physicalcontact = 3,
}
impl OccupancySensorType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(OccupancySensorType::Pir),
1 => Some(OccupancySensorType::Ultrasonic),
2 => Some(OccupancySensorType::Pirandultrasonic),
3 => Some(OccupancySensorType::Physicalcontact),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<OccupancySensorType> for u8 {
fn from(val: OccupancySensorType) -> Self {
val as u8
}
}
pub type Occupancy = u8;
pub mod occupancy {
pub const OCCUPIED: u8 = 0x01;
}
pub type OccupancySensorTypeBitmap = u8;
pub mod occupancysensortype {
pub const PIR: u8 = 0x01;
pub const ULTRASONIC: u8 = 0x02;
pub const PHYSICAL_CONTACT: u8 = 0x04;
}
#[derive(Debug, serde::Serialize)]
pub struct HoldTimeLimits {
pub hold_time_min: Option<u16>,
pub hold_time_max: Option<u16>,
pub hold_time_default: Option<u16>,
}
pub fn decode_occupancy(inp: &tlv::TlvItemValue) -> anyhow::Result<Occupancy> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_occupancy_sensor_type(inp: &tlv::TlvItemValue) -> anyhow::Result<OccupancySensorType> {
if let tlv::TlvItemValue::Int(v) = inp {
OccupancySensorType::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_occupancy_sensor_type_bitmap(inp: &tlv::TlvItemValue) -> anyhow::Result<OccupancySensorTypeBitmap> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_hold_time(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_hold_time_limits(inp: &tlv::TlvItemValue) -> anyhow::Result<HoldTimeLimits> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(HoldTimeLimits {
hold_time_min: item.get_int(&[0]).map(|v| v as u16),
hold_time_max: item.get_int(&[1]).map(|v| v as u16),
hold_time_default: item.get_int(&[2]).map(|v| v as u16),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_pir_occupied_to_unoccupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_pir_unoccupied_to_occupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_pir_unoccupied_to_occupied_threshold(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_ultrasonic_occupied_to_unoccupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_ultrasonic_unoccupied_to_occupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_ultrasonic_unoccupied_to_occupied_threshold(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_physical_contact_occupied_to_unoccupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_physical_contact_unoccupied_to_occupied_delay(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u16)
} else {
Err(anyhow::anyhow!("Expected UInt16"))
}
}
pub fn decode_physical_contact_unoccupied_to_occupied_threshold(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_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0406 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0406, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_occupancy(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_occupancy_sensor_type(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_occupancy_sensor_type_bitmap(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_hold_time(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_hold_time_limits(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0010 => {
match decode_pir_occupied_to_unoccupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0011 => {
match decode_pir_unoccupied_to_occupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0012 => {
match decode_pir_unoccupied_to_occupied_threshold(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0020 => {
match decode_ultrasonic_occupied_to_unoccupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0021 => {
match decode_ultrasonic_unoccupied_to_occupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0022 => {
match decode_ultrasonic_unoccupied_to_occupied_threshold(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0030 => {
match decode_physical_contact_occupied_to_unoccupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0031 => {
match decode_physical_contact_unoccupied_to_occupied_delay(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0032 => {
match decode_physical_contact_unoccupied_to_occupied_threshold(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, "Occupancy"),
(0x0001, "OccupancySensorType"),
(0x0002, "OccupancySensorTypeBitmap"),
(0x0003, "HoldTime"),
(0x0004, "HoldTimeLimits"),
(0x0010, "PIROccupiedToUnoccupiedDelay"),
(0x0011, "PIRUnoccupiedToOccupiedDelay"),
(0x0012, "PIRUnoccupiedToOccupiedThreshold"),
(0x0020, "UltrasonicOccupiedToUnoccupiedDelay"),
(0x0021, "UltrasonicUnoccupiedToOccupiedDelay"),
(0x0022, "UltrasonicUnoccupiedToOccupiedThreshold"),
(0x0030, "PhysicalContactOccupiedToUnoccupiedDelay"),
(0x0031, "PhysicalContactUnoccupiedToOccupiedDelay"),
(0x0032, "PhysicalContactUnoccupiedToOccupiedThreshold"),
]
}
pub async fn read_occupancy(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Occupancy> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_OCCUPANCY).await?;
decode_occupancy(&tlv)
}
pub async fn read_occupancy_sensor_type(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<OccupancySensorType> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_OCCUPANCYSENSORTYPE).await?;
decode_occupancy_sensor_type(&tlv)
}
pub async fn read_occupancy_sensor_type_bitmap(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<OccupancySensorTypeBitmap> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_OCCUPANCYSENSORTYPEBITMAP).await?;
decode_occupancy_sensor_type_bitmap(&tlv)
}
pub async fn read_hold_time(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_HOLDTIME).await?;
decode_hold_time(&tlv)
}
pub async fn read_hold_time_limits(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<HoldTimeLimits> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_HOLDTIMELIMITS).await?;
decode_hold_time_limits(&tlv)
}
pub async fn read_pir_occupied_to_unoccupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PIROCCUPIEDTOUNOCCUPIEDDELAY).await?;
decode_pir_occupied_to_unoccupied_delay(&tlv)
}
pub async fn read_pir_unoccupied_to_occupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PIRUNOCCUPIEDTOOCCUPIEDDELAY).await?;
decode_pir_unoccupied_to_occupied_delay(&tlv)
}
pub async fn read_pir_unoccupied_to_occupied_threshold(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PIRUNOCCUPIEDTOOCCUPIEDTHRESHOLD).await?;
decode_pir_unoccupied_to_occupied_threshold(&tlv)
}
pub async fn read_ultrasonic_occupied_to_unoccupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_ULTRASONICOCCUPIEDTOUNOCCUPIEDDELAY).await?;
decode_ultrasonic_occupied_to_unoccupied_delay(&tlv)
}
pub async fn read_ultrasonic_unoccupied_to_occupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_ULTRASONICUNOCCUPIEDTOOCCUPIEDDELAY).await?;
decode_ultrasonic_unoccupied_to_occupied_delay(&tlv)
}
pub async fn read_ultrasonic_unoccupied_to_occupied_threshold(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_ULTRASONICUNOCCUPIEDTOOCCUPIEDTHRESHOLD).await?;
decode_ultrasonic_unoccupied_to_occupied_threshold(&tlv)
}
pub async fn read_physical_contact_occupied_to_unoccupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PHYSICALCONTACTOCCUPIEDTOUNOCCUPIEDDELAY).await?;
decode_physical_contact_occupied_to_unoccupied_delay(&tlv)
}
pub async fn read_physical_contact_unoccupied_to_occupied_delay(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PHYSICALCONTACTUNOCCUPIEDTOOCCUPIEDDELAY).await?;
decode_physical_contact_unoccupied_to_occupied_delay(&tlv)
}
pub async fn read_physical_contact_unoccupied_to_occupied_threshold(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_OCCUPANCY_SENSING, crate::clusters::defs::CLUSTER_OCCUPANCY_SENSING_ATTR_ID_PHYSICALCONTACTUNOCCUPIEDTOOCCUPIEDTHRESHOLD).await?;
decode_physical_contact_unoccupied_to_occupied_threshold(&tlv)
}
#[derive(Debug, serde::Serialize)]
pub struct OccupancyChangedEvent {
pub occupancy: Option<Occupancy>,
}
pub fn decode_occupancy_changed_event(inp: &tlv::TlvItemValue) -> anyhow::Result<OccupancyChangedEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(OccupancyChangedEvent {
occupancy: item.get_int(&[0]).map(|v| v as u8),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}