matc/clusters/codec/
laundry_washer_controls.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum NumberOfRinses {
18 None = 0,
20 Normal = 1,
22 Extra = 2,
24 Max = 3,
26}
27
28impl NumberOfRinses {
29 pub fn from_u8(value: u8) -> Option<Self> {
31 match value {
32 0 => Some(NumberOfRinses::None),
33 1 => Some(NumberOfRinses::Normal),
34 2 => Some(NumberOfRinses::Extra),
35 3 => Some(NumberOfRinses::Max),
36 _ => None,
37 }
38 }
39
40 pub fn to_u8(self) -> u8 {
42 self as u8
43 }
44}
45
46impl From<NumberOfRinses> for u8 {
47 fn from(val: NumberOfRinses) -> Self {
48 val as u8
49 }
50}
51
52pub fn decode_spin_speeds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
56 let mut res = Vec::new();
57 if let tlv::TlvItemValue::List(v) = inp {
58 for item in v {
59 if let tlv::TlvItemValue::String(s) = &item.value {
60 res.push(s.clone());
61 }
62 }
63 }
64 Ok(res)
65}
66
67pub fn decode_spin_speed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
69 if let tlv::TlvItemValue::Int(v) = inp {
70 Ok(Some(*v as u8))
71 } else {
72 Ok(None)
73 }
74}
75
76pub fn decode_number_of_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<NumberOfRinses> {
78 if let tlv::TlvItemValue::Int(v) = inp {
79 NumberOfRinses::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
80 } else {
81 Err(anyhow::anyhow!("Expected Integer"))
82 }
83}
84
85pub fn decode_supported_rinses(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<NumberOfRinses>> {
87 let mut res = Vec::new();
88 if let tlv::TlvItemValue::List(v) = inp {
89 for item in v {
90 if let tlv::TlvItemValue::Int(i) = &item.value {
91 if let Some(enum_val) = NumberOfRinses::from_u8(*i as u8) {
92 res.push(enum_val);
93 }
94 }
95 }
96 }
97 Ok(res)
98}
99
100
101pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
113 if cluster_id != 0x0053 {
115 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0053, got {}\"}}", cluster_id);
116 }
117
118 match attribute_id {
119 0x0000 => {
120 match decode_spin_speeds(tlv_value) {
121 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
122 Err(e) => format!("{{\"error\": \"{}\"}}", e),
123 }
124 }
125 0x0001 => {
126 match decode_spin_speed_current(tlv_value) {
127 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
128 Err(e) => format!("{{\"error\": \"{}\"}}", e),
129 }
130 }
131 0x0002 => {
132 match decode_number_of_rinses(tlv_value) {
133 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
134 Err(e) => format!("{{\"error\": \"{}\"}}", e),
135 }
136 }
137 0x0003 => {
138 match decode_supported_rinses(tlv_value) {
139 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
140 Err(e) => format!("{{\"error\": \"{}\"}}", e),
141 }
142 }
143 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
144 }
145}
146
147pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
152 vec![
153 (0x0000, "SpinSpeeds"),
154 (0x0001, "SpinSpeedCurrent"),
155 (0x0002, "NumberOfRinses"),
156 (0x0003, "SupportedRinses"),
157 ]
158}
159
160pub async fn read_spin_speeds(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<String>> {
164 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SPINSPEEDS).await?;
165 decode_spin_speeds(&tlv)
166}
167
168pub async fn read_spin_speed_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
170 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SPINSPEEDCURRENT).await?;
171 decode_spin_speed_current(&tlv)
172}
173
174pub async fn read_number_of_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<NumberOfRinses> {
176 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_NUMBEROFRINSES).await?;
177 decode_number_of_rinses(&tlv)
178}
179
180pub async fn read_supported_rinses(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<NumberOfRinses>> {
182 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_CONTROLS, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_CONTROLS_ATTR_ID_SUPPORTEDRINSES).await?;
183 decode_supported_rinses(&tlv)
184}
185