matc/clusters/codec/
mode_laundry_washer.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(u16)]
17pub enum ModeTag {
18 Auto = 0,
19 Quick = 1,
20 Quiet = 2,
21 Lownoise = 3,
22 Lowenergy = 4,
23 Vacation = 5,
24 Min = 6,
25 Max = 7,
26 Night = 8,
27 Day = 9,
28 Normal = 16384,
29 Delicate = 16385,
30 Heavy = 16386,
31 Whites = 16387,
32}
33
34impl ModeTag {
35 pub fn from_u8(value: u8) -> Option<Self> {
37 Self::from_u16(value as u16)
38 }
39
40 pub fn from_u16(value: u16) -> Option<Self> {
42 match value {
43 0 => Some(ModeTag::Auto),
44 1 => Some(ModeTag::Quick),
45 2 => Some(ModeTag::Quiet),
46 3 => Some(ModeTag::Lownoise),
47 4 => Some(ModeTag::Lowenergy),
48 5 => Some(ModeTag::Vacation),
49 6 => Some(ModeTag::Min),
50 7 => Some(ModeTag::Max),
51 8 => Some(ModeTag::Night),
52 9 => Some(ModeTag::Day),
53 16384 => Some(ModeTag::Normal),
54 16385 => Some(ModeTag::Delicate),
55 16386 => Some(ModeTag::Heavy),
56 16387 => Some(ModeTag::Whites),
57 _ => None,
58 }
59 }
60
61 pub fn to_u8(self) -> u8 {
63 self as u8
64 }
65
66 pub fn to_u16(self) -> u16 {
68 self as u16
69 }
70}
71
72impl From<ModeTag> for u16 {
73 fn from(val: ModeTag) -> Self {
74 val as u16
75 }
76}
77
78#[derive(Debug, serde::Serialize)]
81pub struct ModeOption {
82 pub label: Option<u8>,
83 pub mode: Option<u8>,
84 pub mode_tags: Option<u8>,
85}
86
87pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
91 if let tlv::TlvItemValue::Int(v) = inp {
92 Ok(*v as u8)
93 } else {
94 Err(anyhow::anyhow!("Expected UInt8"))
95 }
96}
97
98pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
100 if let tlv::TlvItemValue::Int(v) = inp {
101 Ok(*v as u8)
102 } else {
103 Err(anyhow::anyhow!("Expected UInt8"))
104 }
105}
106
107pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
109 if let tlv::TlvItemValue::Int(v) = inp {
110 Ok(*v as u8)
111 } else {
112 Err(anyhow::anyhow!("Expected UInt8"))
113 }
114}
115
116pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
118 if let tlv::TlvItemValue::Int(v) = inp {
119 Ok(*v as u8)
120 } else {
121 Err(anyhow::anyhow!("Expected UInt8"))
122 }
123}
124
125
126pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
138 if cluster_id != 0x0051 {
140 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0051, got {}\"}}", cluster_id);
141 }
142
143 match attribute_id {
144 0x0000 => {
145 match decode_supported_modes(tlv_value) {
146 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
147 Err(e) => format!("{{\"error\": \"{}\"}}", e),
148 }
149 }
150 0x0001 => {
151 match decode_current_mode(tlv_value) {
152 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
153 Err(e) => format!("{{\"error\": \"{}\"}}", e),
154 }
155 }
156 0x0002 => {
157 match decode_start_up_mode(tlv_value) {
158 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
159 Err(e) => format!("{{\"error\": \"{}\"}}", e),
160 }
161 }
162 0x0003 => {
163 match decode_on_mode(tlv_value) {
164 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
165 Err(e) => format!("{{\"error\": \"{}\"}}", e),
166 }
167 }
168 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
169 }
170}
171
172pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
177 vec![
178 (0x0000, "SupportedModes"),
179 (0x0001, "CurrentMode"),
180 (0x0002, "StartUpMode"),
181 (0x0003, "OnMode"),
182 ]
183}
184
185pub async fn read_supported_modes(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
189 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_MODE, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_MODE_ATTR_ID_SUPPORTEDMODES).await?;
190 decode_supported_modes(&tlv)
191}
192
193pub async fn read_current_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
195 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_MODE, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_MODE_ATTR_ID_CURRENTMODE).await?;
196 decode_current_mode(&tlv)
197}
198
199pub async fn read_start_up_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
201 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_MODE, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_MODE_ATTR_ID_STARTUPMODE).await?;
202 decode_start_up_mode(&tlv)
203}
204
205pub async fn read_on_mode(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
207 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LAUNDRY_WASHER_MODE, crate::clusters::defs::CLUSTER_LAUNDRY_WASHER_MODE_ATTR_ID_ONMODE).await?;
208 decode_on_mode(&tlv)
209}
210