matc/clusters/codec/
water_heater_management.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct WaterHeaterBoostInfo {
15 pub duration: Option<u8>,
16 pub one_shot: Option<bool>,
17 pub emergency_boost: Option<bool>,
18 pub temporary_setpoint: Option<u8>,
19 pub target_percentage: Option<u8>,
20 pub target_reheat: Option<u8>,
21}
22
23pub fn encode_boost(boost_info: u8) -> anyhow::Result<Vec<u8>> {
27 let tlv = tlv::TlvItemEnc {
28 tag: 0,
29 value: tlv::TlvItemValueEnc::StructInvisible(vec![
30 (0, tlv::TlvItemValueEnc::UInt8(boost_info)).into(),
31 ]),
32 };
33 Ok(tlv.encode()?)
34}
35
36pub fn decode_heater_types(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
40 if let tlv::TlvItemValue::Int(v) = inp {
41 Ok(*v as u8)
42 } else {
43 Err(anyhow::anyhow!("Expected Integer"))
44 }
45}
46
47pub fn decode_heat_demand(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
49 if let tlv::TlvItemValue::Int(v) = inp {
50 Ok(*v as u8)
51 } else {
52 Err(anyhow::anyhow!("Expected Integer"))
53 }
54}
55
56pub fn decode_tank_volume(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
58 if let tlv::TlvItemValue::Int(v) = inp {
59 Ok(*v as u16)
60 } else {
61 Err(anyhow::anyhow!("Expected Integer"))
62 }
63}
64
65pub fn decode_estimated_heat_required(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
67 if let tlv::TlvItemValue::Int(v) = inp {
68 Ok(*v as u8)
69 } else {
70 Err(anyhow::anyhow!("Expected Integer"))
71 }
72}
73
74pub fn decode_tank_percentage(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
76 if let tlv::TlvItemValue::Int(v) = inp {
77 Ok(*v as u8)
78 } else {
79 Err(anyhow::anyhow!("Expected Integer"))
80 }
81}
82
83pub fn decode_boost_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
85 if let tlv::TlvItemValue::Int(v) = inp {
86 Ok(*v as u8)
87 } else {
88 Err(anyhow::anyhow!("Expected Integer"))
89 }
90}
91
92
93pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
105 if cluster_id != 0x0094 {
107 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0094, got {}\"}}", cluster_id);
108 }
109
110 match attribute_id {
111 0x0000 => {
112 match decode_heater_types(tlv_value) {
113 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
114 Err(e) => format!("{{\"error\": \"{}\"}}", e),
115 }
116 }
117 0x0001 => {
118 match decode_heat_demand(tlv_value) {
119 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
120 Err(e) => format!("{{\"error\": \"{}\"}}", e),
121 }
122 }
123 0x0002 => {
124 match decode_tank_volume(tlv_value) {
125 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
126 Err(e) => format!("{{\"error\": \"{}\"}}", e),
127 }
128 }
129 0x0003 => {
130 match decode_estimated_heat_required(tlv_value) {
131 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
132 Err(e) => format!("{{\"error\": \"{}\"}}", e),
133 }
134 }
135 0x0004 => {
136 match decode_tank_percentage(tlv_value) {
137 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
138 Err(e) => format!("{{\"error\": \"{}\"}}", e),
139 }
140 }
141 0x0005 => {
142 match decode_boost_state(tlv_value) {
143 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
144 Err(e) => format!("{{\"error\": \"{}\"}}", e),
145 }
146 }
147 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
148 }
149}
150
151pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
156 vec![
157 (0x0000, "HeaterTypes"),
158 (0x0001, "HeatDemand"),
159 (0x0002, "TankVolume"),
160 (0x0003, "EstimatedHeatRequired"),
161 (0x0004, "TankPercentage"),
162 (0x0005, "BoostState"),
163 ]
164}
165