matc/clusters/codec/
localization_configuration.rs1#![allow(clippy::too_many_arguments)]
7
8use crate::tlv;
9use anyhow;
10use serde_json;
11
12
13pub fn decode_active_locale(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
17 if let tlv::TlvItemValue::String(v) = inp {
18 Ok(v.clone())
19 } else {
20 Err(anyhow::anyhow!("Expected String"))
21 }
22}
23
24pub fn decode_supported_locales(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<String>> {
26 let mut res = Vec::new();
27 if let tlv::TlvItemValue::List(v) = inp {
28 for item in v {
29 if let tlv::TlvItemValue::String(s) = &item.value {
30 res.push(s.clone());
31 }
32 }
33 }
34 Ok(res)
35}
36
37
38pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
50 if cluster_id != 0x002B {
52 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002B, got {}\"}}", cluster_id);
53 }
54
55 match attribute_id {
56 0x0000 => {
57 match decode_active_locale(tlv_value) {
58 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
59 Err(e) => format!("{{\"error\": \"{}\"}}", e),
60 }
61 }
62 0x0001 => {
63 match decode_supported_locales(tlv_value) {
64 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
65 Err(e) => format!("{{\"error\": \"{}\"}}", e),
66 }
67 }
68 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
69 }
70}
71
72pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
77 vec![
78 (0x0000, "ActiveLocale"),
79 (0x0001, "SupportedLocales"),
80 ]
81}
82
83pub async fn read_active_locale(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
87 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LOCALIZATION_CONFIGURATION, crate::clusters::defs::CLUSTER_LOCALIZATION_CONFIGURATION_ATTR_ID_ACTIVELOCALE).await?;
88 decode_active_locale(&tlv)
89}
90
91pub async fn read_supported_locales(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<String>> {
93 let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_LOCALIZATION_CONFIGURATION, crate::clusters::defs::CLUSTER_LOCALIZATION_CONFIGURATION_ATTR_ID_SUPPORTEDLOCALES).await?;
94 decode_supported_locales(&tlv)
95}
96