matc/clusters/codec/
chime.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13#[derive(Debug, serde::Serialize)]
16pub struct ChimeSound {
17 pub chime_id: Option<u8>,
18 pub name: Option<String>,
19}
20
21pub fn decode_installed_chime_sounds(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ChimeSound>> {
27 let mut res = Vec::new();
28 if let tlv::TlvItemValue::List(v) = inp {
29 for item in v {
30 res.push(ChimeSound {
31 chime_id: item.get_int(&[0]).map(|v| v as u8),
32 name: item.get_string_owned(&[1]),
33 });
34 }
35 }
36 Ok(res)
37}
38
39pub fn decode_selected_chime(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
41 if let tlv::TlvItemValue::Int(v) = inp {
42 Ok(*v as u8)
43 } else {
44 Err(anyhow::anyhow!("Expected UInt8"))
45 }
46}
47
48pub fn decode_enabled(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
50 if let tlv::TlvItemValue::Bool(v) = inp {
51 Ok(*v)
52 } else {
53 Err(anyhow::anyhow!("Expected Bool"))
54 }
55}
56
57
58pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
70 if cluster_id != 0x0556 {
72 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0556, got {}\"}}", cluster_id);
73 }
74
75 match attribute_id {
76 0x0000 => {
77 match decode_installed_chime_sounds(tlv_value) {
78 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
79 Err(e) => format!("{{\"error\": \"{}\"}}", e),
80 }
81 }
82 0x0001 => {
83 match decode_selected_chime(tlv_value) {
84 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
85 Err(e) => format!("{{\"error\": \"{}\"}}", e),
86 }
87 }
88 0x0002 => {
89 match decode_enabled(tlv_value) {
90 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
91 Err(e) => format!("{{\"error\": \"{}\"}}", e),
92 }
93 }
94 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
95 }
96}
97
98pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
103 vec![
104 (0x0000, "InstalledChimeSounds"),
105 (0x0001, "SelectedChime"),
106 (0x0002, "Enabled"),
107 ]
108}
109
110pub fn get_command_list() -> Vec<(u32, &'static str)> {
113 vec![
114 (0x00, "PlayChimeSound"),
115 ]
116}
117
118pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
119 match cmd_id {
120 0x00 => Some("PlayChimeSound"),
121 _ => None,
122 }
123}
124
125pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
126 match cmd_id {
127 0x00 => Some(vec![]),
128 _ => None,
129 }
130}
131
132pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
133 match cmd_id {
134 0x00 => Ok(vec![]),
135 _ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
136 }
137}
138
139pub async fn play_chime_sound(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
143 conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_CMD_ID_PLAYCHIMESOUND, &[]).await?;
144 Ok(())
145}
146
147pub async fn read_installed_chime_sounds(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<ChimeSound>> {
149 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_INSTALLEDCHIMESOUNDS).await?;
150 decode_installed_chime_sounds(&tlv)
151}
152
153pub async fn read_selected_chime(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
155 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_SELECTEDCHIME).await?;
156 decode_selected_chime(&tlv)
157}
158
159pub async fn read_enabled(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
161 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_CHIME, crate::clusters::defs::CLUSTER_CHIME_ATTR_ID_ENABLED).await?;
162 decode_enabled(&tlv)
163}
164