matc/clusters/codec/
illuminance_measurement.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u8)]
15pub enum LightSensorType {
16 Photodiode = 0,
18 Cmos = 1,
20}
21
22impl LightSensorType {
23 pub fn from_u8(value: u8) -> Option<Self> {
25 match value {
26 0 => Some(LightSensorType::Photodiode),
27 1 => Some(LightSensorType::Cmos),
28 _ => None,
29 }
30 }
31
32 pub fn to_u8(self) -> u8 {
34 self as u8
35 }
36}
37
38impl From<LightSensorType> for u8 {
39 fn from(val: LightSensorType) -> Self {
40 val as u8
41 }
42}
43
44pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
48 if let tlv::TlvItemValue::Int(v) = inp {
49 Ok(Some(*v as u16))
50 } else {
51 Ok(None)
52 }
53}
54
55pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
57 if let tlv::TlvItemValue::Int(v) = inp {
58 Ok(Some(*v as u16))
59 } else {
60 Ok(None)
61 }
62}
63
64pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
66 if let tlv::TlvItemValue::Int(v) = inp {
67 Ok(Some(*v as u16))
68 } else {
69 Ok(None)
70 }
71}
72
73pub fn decode_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
75 if let tlv::TlvItemValue::Int(v) = inp {
76 Ok(*v as u16)
77 } else {
78 Err(anyhow::anyhow!("Expected UInt16"))
79 }
80}
81
82pub fn decode_light_sensor_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<LightSensorType>> {
84 if let tlv::TlvItemValue::Int(v) = inp {
85 Ok(LightSensorType::from_u8(*v as u8))
86 } else {
87 Ok(None)
88 }
89}
90
91
92pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
104 if cluster_id != 0x0400 {
106 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0400, got {}\"}}", cluster_id);
107 }
108
109 match attribute_id {
110 0x0000 => {
111 match decode_measured_value(tlv_value) {
112 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113 Err(e) => format!("{{\"error\": \"{}\"}}", e),
114 }
115 }
116 0x0001 => {
117 match decode_min_measured_value(tlv_value) {
118 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
119 Err(e) => format!("{{\"error\": \"{}\"}}", e),
120 }
121 }
122 0x0002 => {
123 match decode_max_measured_value(tlv_value) {
124 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
125 Err(e) => format!("{{\"error\": \"{}\"}}", e),
126 }
127 }
128 0x0003 => {
129 match decode_tolerance(tlv_value) {
130 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
131 Err(e) => format!("{{\"error\": \"{}\"}}", e),
132 }
133 }
134 0x0004 => {
135 match decode_light_sensor_type(tlv_value) {
136 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
137 Err(e) => format!("{{\"error\": \"{}\"}}", e),
138 }
139 }
140 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
141 }
142}
143
144pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
149 vec![
150 (0x0000, "MeasuredValue"),
151 (0x0001, "MinMeasuredValue"),
152 (0x0002, "MaxMeasuredValue"),
153 (0x0003, "Tolerance"),
154 (0x0004, "LightSensorType"),
155 ]
156}
157