matc/clusters/codec/
mode_evse.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 Manual = 16384,
27 Timeofuse = 16385,
28 Solarcharging = 16386,
29 V2x = 16387,
30}
31
32impl ModeTag {
33 pub fn from_u8(value: u8) -> Option<Self> {
35 Self::from_u16(value as u16)
36 }
37
38 pub fn from_u16(value: u16) -> Option<Self> {
40 match value {
41 0 => Some(ModeTag::Auto),
42 1 => Some(ModeTag::Quick),
43 2 => Some(ModeTag::Quiet),
44 3 => Some(ModeTag::Lownoise),
45 4 => Some(ModeTag::Lowenergy),
46 5 => Some(ModeTag::Vacation),
47 6 => Some(ModeTag::Min),
48 7 => Some(ModeTag::Max),
49 8 => Some(ModeTag::Night),
50 9 => Some(ModeTag::Day),
51 16384 => Some(ModeTag::Manual),
52 16385 => Some(ModeTag::Timeofuse),
53 16386 => Some(ModeTag::Solarcharging),
54 16387 => Some(ModeTag::V2x),
55 _ => None,
56 }
57 }
58
59 pub fn to_u8(self) -> u8 {
61 self as u8
62 }
63
64 pub fn to_u16(self) -> u16 {
66 self as u16
67 }
68}
69
70impl From<ModeTag> for u16 {
71 fn from(val: ModeTag) -> Self {
72 val as u16
73 }
74}
75
76#[derive(Debug, serde::Serialize)]
79pub struct ModeOption {
80 pub label: Option<u8>,
81 pub mode: Option<u8>,
82 pub mode_tags: Option<u8>,
83}
84
85pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
89 if let tlv::TlvItemValue::Int(v) = inp {
90 Ok(*v as u8)
91 } else {
92 Err(anyhow::anyhow!("Expected UInt8"))
93 }
94}
95
96pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
98 if let tlv::TlvItemValue::Int(v) = inp {
99 Ok(*v as u8)
100 } else {
101 Err(anyhow::anyhow!("Expected UInt8"))
102 }
103}
104
105pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
107 if let tlv::TlvItemValue::Int(v) = inp {
108 Ok(*v as u8)
109 } else {
110 Err(anyhow::anyhow!("Expected UInt8"))
111 }
112}
113
114pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
116 if let tlv::TlvItemValue::Int(v) = inp {
117 Ok(*v as u8)
118 } else {
119 Err(anyhow::anyhow!("Expected UInt8"))
120 }
121}
122
123
124pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
136 if cluster_id != 0x009D {
138 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009D, got {}\"}}", cluster_id);
139 }
140
141 match attribute_id {
142 0x0000 => {
143 match decode_supported_modes(tlv_value) {
144 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
145 Err(e) => format!("{{\"error\": \"{}\"}}", e),
146 }
147 }
148 0x0001 => {
149 match decode_current_mode(tlv_value) {
150 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
151 Err(e) => format!("{{\"error\": \"{}\"}}", e),
152 }
153 }
154 0x0002 => {
155 match decode_start_up_mode(tlv_value) {
156 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
157 Err(e) => format!("{{\"error\": \"{}\"}}", e),
158 }
159 }
160 0x0003 => {
161 match decode_on_mode(tlv_value) {
162 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
163 Err(e) => format!("{{\"error\": \"{}\"}}", e),
164 }
165 }
166 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
167 }
168}
169
170pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
175 vec![
176 (0x0000, "SupportedModes"),
177 (0x0001, "CurrentMode"),
178 (0x0002, "StartUpMode"),
179 (0x0003, "OnMode"),
180 ]
181}
182