matc/clusters/codec/
mode_microwave_oven.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u16)]
15pub enum ModeTag {
16 Auto = 0,
17 Quick = 1,
18 Quiet = 2,
19 Lownoise = 3,
20 Lowenergy = 4,
21 Vacation = 5,
22 Min = 6,
23 Max = 7,
24 Night = 8,
25 Day = 9,
26 Normal = 16384,
27 Defrost = 16385,
28}
29
30impl ModeTag {
31 pub fn from_u8(value: u8) -> Option<Self> {
33 Self::from_u16(value as u16)
34 }
35
36 pub fn from_u16(value: u16) -> Option<Self> {
38 match value {
39 0 => Some(ModeTag::Auto),
40 1 => Some(ModeTag::Quick),
41 2 => Some(ModeTag::Quiet),
42 3 => Some(ModeTag::Lownoise),
43 4 => Some(ModeTag::Lowenergy),
44 5 => Some(ModeTag::Vacation),
45 6 => Some(ModeTag::Min),
46 7 => Some(ModeTag::Max),
47 8 => Some(ModeTag::Night),
48 9 => Some(ModeTag::Day),
49 16384 => Some(ModeTag::Normal),
50 16385 => Some(ModeTag::Defrost),
51 _ => None,
52 }
53 }
54
55 pub fn to_u8(self) -> u8 {
57 self as u8
58 }
59
60 pub fn to_u16(self) -> u16 {
62 self as u16
63 }
64}
65
66impl From<ModeTag> for u16 {
67 fn from(val: ModeTag) -> Self {
68 val as u16
69 }
70}
71
72pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
78 if let tlv::TlvItemValue::Int(v) = inp {
79 Ok(*v as u8)
80 } else {
81 Err(anyhow::anyhow!("Expected UInt8"))
82 }
83}
84
85pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
87 if let tlv::TlvItemValue::Int(v) = inp {
88 Ok(*v as u8)
89 } else {
90 Err(anyhow::anyhow!("Expected UInt8"))
91 }
92}
93
94pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
96 if let tlv::TlvItemValue::Int(v) = inp {
97 Ok(*v as u8)
98 } else {
99 Err(anyhow::anyhow!("Expected UInt8"))
100 }
101}
102
103pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
105 if let tlv::TlvItemValue::Int(v) = inp {
106 Ok(*v as u8)
107 } else {
108 Err(anyhow::anyhow!("Expected UInt8"))
109 }
110}
111
112
113pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
125 if cluster_id != 0x005E {
127 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x005E, got {}\"}}", cluster_id);
128 }
129
130 match attribute_id {
131 0x0000 => {
132 match decode_supported_modes(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 0x0001 => {
138 match decode_current_mode(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 0x0002 => {
144 match decode_start_up_mode(tlv_value) {
145 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
146 Err(e) => format!("{{\"error\": \"{}\"}}", e),
147 }
148 }
149 0x0003 => {
150 match decode_on_mode(tlv_value) {
151 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
152 Err(e) => format!("{{\"error\": \"{}\"}}", e),
153 }
154 }
155 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
156 }
157}
158
159pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
164 vec![
165 (0x0000, "SupportedModes"),
166 (0x0001, "CurrentMode"),
167 (0x0002, "StartUpMode"),
168 (0x0003, "OnMode"),
169 ]
170}
171