matc/clusters/codec/
mode_device_energy_management.rs

1//! Matter TLV encoders and decoders for Device Energy Management Mode Cluster
2//! Cluster ID: 0x009F
3//!
4//! This file is automatically generated from Mode_DeviceEnergyManagement.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Enum definitions
12
13#[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    Nooptimization = 16384,
27    Deviceoptimization = 16385,
28    Localoptimization = 16386,
29    Gridoptimization = 16387,
30}
31
32impl ModeTag {
33    /// Convert from u8 value (promoted to u16)
34    pub fn from_u8(value: u8) -> Option<Self> {
35        Self::from_u16(value as u16)
36    }
37
38    /// Convert from u16 value
39    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::Nooptimization),
52            16385 => Some(ModeTag::Deviceoptimization),
53            16386 => Some(ModeTag::Localoptimization),
54            16387 => Some(ModeTag::Gridoptimization),
55            _ => None,
56        }
57    }
58
59    /// Convert to u8 value (truncated if value > 255)
60    pub fn to_u8(self) -> u8 {
61        self as u8
62    }
63
64    /// Convert to u16 value
65    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// Struct definitions
77
78#[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
85// Attribute decoders
86
87/// Decode SupportedModes attribute (0x0000)
88pub 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
96/// Decode CurrentMode attribute (0x0001)
97pub 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
105/// Decode StartUpMode attribute (0x0002)
106pub 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
114/// Decode OnMode attribute (0x0003)
115pub 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
124// JSON dispatcher function
125
126/// Decode attribute value and return as JSON string
127///
128/// # Parameters
129/// * `cluster_id` - The cluster identifier
130/// * `attribute_id` - The attribute identifier
131/// * `tlv_value` - The TLV value to decode
132///
133/// # Returns
134/// JSON string representation of the decoded value or error
135pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
136    // Verify this is the correct cluster
137    if cluster_id != 0x009F {
138        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x009F, 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
170/// Get list of all attributes supported by this cluster
171///
172/// # Returns
173/// Vector of tuples containing (attribute_id, attribute_name)
174pub 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