#![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 LightSensorType {
Photodiode = 0,
Cmos = 1,
}
impl LightSensorType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(LightSensorType::Photodiode),
1 => Some(LightSensorType::Cmos),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<LightSensorType> for u8 {
fn from(val: LightSensorType) -> Self {
val as u8
}
}
pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u16))
} else {
Ok(None)
}
}
pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u16))
} else {
Ok(None)
}
}
pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u16))
} else {
Ok(None)
}
}
pub fn decode_tolerance(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_light_sensor_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<LightSensorType>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(LightSensorType::from_u8(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0400 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0400, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_measured_value(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_min_measured_value(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_max_measured_value(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_tolerance(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_light_sensor_type(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, "MeasuredValue"),
(0x0001, "MinMeasuredValue"),
(0x0002, "MaxMeasuredValue"),
(0x0003, "Tolerance"),
(0x0004, "LightSensorType"),
]
}
pub async fn read_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ILLUMINANCE_MEASUREMENT, crate::clusters::defs::CLUSTER_ILLUMINANCE_MEASUREMENT_ATTR_ID_MEASUREDVALUE).await?;
decode_measured_value(&tlv)
}
pub async fn read_min_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ILLUMINANCE_MEASUREMENT, crate::clusters::defs::CLUSTER_ILLUMINANCE_MEASUREMENT_ATTR_ID_MINMEASUREDVALUE).await?;
decode_min_measured_value(&tlv)
}
pub async fn read_max_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ILLUMINANCE_MEASUREMENT, crate::clusters::defs::CLUSTER_ILLUMINANCE_MEASUREMENT_ATTR_ID_MAXMEASUREDVALUE).await?;
decode_max_measured_value(&tlv)
}
pub async fn read_tolerance(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ILLUMINANCE_MEASUREMENT, crate::clusters::defs::CLUSTER_ILLUMINANCE_MEASUREMENT_ATTR_ID_TOLERANCE).await?;
decode_tolerance(&tlv)
}
pub async fn read_light_sensor_type(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<LightSensorType>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_ILLUMINANCE_MEASUREMENT, crate::clusters::defs::CLUSTER_ILLUMINANCE_MEASUREMENT_ATTR_ID_LIGHTSENSORTYPE).await?;
decode_light_sensor_type(&tlv)
}