#![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 KeypadLockout {
Nolockout = 0,
Lockout1 = 1,
Lockout2 = 2,
Lockout3 = 3,
Lockout4 = 4,
Lockout5 = 5,
}
impl KeypadLockout {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(KeypadLockout::Nolockout),
1 => Some(KeypadLockout::Lockout1),
2 => Some(KeypadLockout::Lockout2),
3 => Some(KeypadLockout::Lockout3),
4 => Some(KeypadLockout::Lockout4),
5 => Some(KeypadLockout::Lockout5),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<KeypadLockout> for u8 {
fn from(val: KeypadLockout) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ScheduleProgrammingVisibility {
Scheduleprogrammingpermitted = 0,
Scheduleprogrammingdenied = 1,
}
impl ScheduleProgrammingVisibility {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(ScheduleProgrammingVisibility::Scheduleprogrammingpermitted),
1 => Some(ScheduleProgrammingVisibility::Scheduleprogrammingdenied),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<ScheduleProgrammingVisibility> for u8 {
fn from(val: ScheduleProgrammingVisibility) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum TemperatureDisplayMode {
Celsius = 0,
Fahrenheit = 1,
}
impl TemperatureDisplayMode {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(TemperatureDisplayMode::Celsius),
1 => Some(TemperatureDisplayMode::Fahrenheit),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<TemperatureDisplayMode> for u8 {
fn from(val: TemperatureDisplayMode) -> Self {
val as u8
}
}
pub fn decode_temperature_display_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<TemperatureDisplayMode> {
if let tlv::TlvItemValue::Int(v) = inp {
TemperatureDisplayMode::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_keypad_lockout(inp: &tlv::TlvItemValue) -> anyhow::Result<KeypadLockout> {
if let tlv::TlvItemValue::Int(v) = inp {
KeypadLockout::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_schedule_programming_visibility(inp: &tlv::TlvItemValue) -> anyhow::Result<ScheduleProgrammingVisibility> {
if let tlv::TlvItemValue::Int(v) = inp {
ScheduleProgrammingVisibility::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} 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 != 0x0204 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0204, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_temperature_display_mode(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_keypad_lockout(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_schedule_programming_visibility(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, "TemperatureDisplayMode"),
(0x0001, "KeypadLockout"),
(0x0002, "ScheduleProgrammingVisibility"),
]
}
pub async fn read_temperature_display_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<TemperatureDisplayMode> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_TEMPERATUREDISPLAYMODE).await?;
decode_temperature_display_mode(&tlv)
}
pub async fn read_keypad_lockout(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<KeypadLockout> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_KEYPADLOCKOUT).await?;
decode_keypad_lockout(&tlv)
}
pub async fn read_schedule_programming_visibility(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<ScheduleProgrammingVisibility> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_THERMOSTAT_USER_INTERFACE_CONFIGURATION, crate::clusters::defs::CLUSTER_THERMOSTAT_USER_INTERFACE_CONFIGURATION_ATTR_ID_SCHEDULEPROGRAMMINGVISIBILITY).await?;
decode_schedule_programming_visibility(&tlv)
}