matc/clusters/codec/
temperature_control.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11pub fn encode_set_temperature(target_temperature: u8, target_temperature_level: u8) -> anyhow::Result<Vec<u8>> {
15 let tlv = tlv::TlvItemEnc {
16 tag: 0,
17 value: tlv::TlvItemValueEnc::StructInvisible(vec![
18 (0, tlv::TlvItemValueEnc::UInt8(target_temperature)).into(),
19 (1, tlv::TlvItemValueEnc::UInt8(target_temperature_level)).into(),
20 ]),
21 };
22 Ok(tlv.encode()?)
23}
24
25pub fn decode_temperature_setpoint(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
29 if let tlv::TlvItemValue::Int(v) = inp {
30 Ok(*v as u8)
31 } else {
32 Err(anyhow::anyhow!("Expected Integer"))
33 }
34}
35
36pub fn decode_min_temperature(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
38 if let tlv::TlvItemValue::Int(v) = inp {
39 Ok(*v as u8)
40 } else {
41 Err(anyhow::anyhow!("Expected Integer"))
42 }
43}
44
45pub fn decode_max_temperature(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
47 if let tlv::TlvItemValue::Int(v) = inp {
48 Ok(*v as u8)
49 } else {
50 Err(anyhow::anyhow!("Expected Integer"))
51 }
52}
53
54pub fn decode_step(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
56 if let tlv::TlvItemValue::Int(v) = inp {
57 Ok(*v as u8)
58 } else {
59 Err(anyhow::anyhow!("Expected Integer"))
60 }
61}
62
63pub fn decode_selected_temperature_level(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
65 if let tlv::TlvItemValue::Int(v) = inp {
66 Ok(*v as u8)
67 } else {
68 Err(anyhow::anyhow!("Expected Integer"))
69 }
70}
71
72pub fn decode_supported_temperature_levels(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
74 let mut res = Vec::new();
75 if let tlv::TlvItemValue::List(v) = inp {
76 for item in v {
77 if let tlv::TlvItemValue::String(s) = &item.value {
78 res.push(s.clone());
79 }
80 }
81 }
82 Ok(res)
83}
84
85
86pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
98 if cluster_id != 0x0056 {
100 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0056, got {}\"}}", cluster_id);
101 }
102
103 match attribute_id {
104 0x0000 => {
105 match decode_temperature_setpoint(tlv_value) {
106 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
107 Err(e) => format!("{{\"error\": \"{}\"}}", e),
108 }
109 }
110 0x0001 => {
111 match decode_min_temperature(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 0x0002 => {
117 match decode_max_temperature(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 0x0003 => {
123 match decode_step(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 0x0004 => {
129 match decode_selected_temperature_level(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 0x0005 => {
135 match decode_supported_temperature_levels(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, "TemperatureSetpoint"),
151 (0x0001, "MinTemperature"),
152 (0x0002, "MaxTemperature"),
153 (0x0003, "Step"),
154 (0x0004, "SelectedTemperatureLevel"),
155 (0x0005, "SupportedTemperatureLevels"),
156 ]
157}
158