matc 0.1.3

Matter protocol library (controller side)
Documentation
//! Matter TLV encoders and decoders for Temperature Measurement Cluster
//! Cluster ID: 0x0402
//!
//! This file is automatically generated from TemperatureMeasurement.xml

#![allow(clippy::too_many_arguments)]

use crate::tlv;
use anyhow;
use serde_json;


// Attribute decoders

/// Decode MeasuredValue attribute (0x0000)
pub fn decode_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
    if let tlv::TlvItemValue::Int(v) = inp {
        Ok(Some(*v as i16))
    } else {
        Ok(None)
    }
}

/// Decode MinMeasuredValue attribute (0x0001)
pub fn decode_min_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
    if let tlv::TlvItemValue::Int(v) = inp {
        Ok(Some(*v as i16))
    } else {
        Ok(None)
    }
}

/// Decode MaxMeasuredValue attribute (0x0002)
pub fn decode_max_measured_value(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i16>> {
    if let tlv::TlvItemValue::Int(v) = inp {
        Ok(Some(*v as i16))
    } else {
        Ok(None)
    }
}

/// Decode Tolerance attribute (0x0003)
pub fn decode_tolerance(inp: &tlv::TlvItemValue) -> anyhow::Result<u16> {
    if let tlv::TlvItemValue::Int(v) = inp {
        Ok(*v as u16)
    } else {
        Err(anyhow::anyhow!("Expected UInt16"))
    }
}


// JSON dispatcher function

/// Decode attribute value and return as JSON string
///
/// # Parameters
/// * `cluster_id` - The cluster identifier
/// * `attribute_id` - The attribute identifier
/// * `tlv_value` - The TLV value to decode
///
/// # Returns
/// JSON string representation of the decoded value or error
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
    // Verify this is the correct cluster
    if cluster_id != 0x0402 {
        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0402, got {}\"}}", cluster_id);
    }

    match attribute_id {
        0x0000 => {
            match decode_measured_value(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0001 => {
            match decode_min_measured_value(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0002 => {
            match decode_max_measured_value(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        0x0003 => {
            match decode_tolerance(tlv_value) {
                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
                Err(e) => format!("{{\"error\": \"{}\"}}", e),
            }
        }
        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
    }
}

/// Get list of all attributes supported by this cluster
///
/// # Returns
/// Vector of tuples containing (attribute_id, attribute_name)
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
    vec![
        (0x0000, "MeasuredValue"),
        (0x0001, "MinMeasuredValue"),
        (0x0002, "MaxMeasuredValue"),
        (0x0003, "Tolerance"),
    ]
}

// Typed facade (invokes + reads)

/// Read `MeasuredValue` attribute from cluster `Temperature Measurement`.
pub async fn read_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TEMPERATURE_MEASUREMENT, crate::clusters::defs::CLUSTER_TEMPERATURE_MEASUREMENT_ATTR_ID_MEASUREDVALUE).await?;
    decode_measured_value(&tlv)
}

/// Read `MinMeasuredValue` attribute from cluster `Temperature Measurement`.
pub async fn read_min_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TEMPERATURE_MEASUREMENT, crate::clusters::defs::CLUSTER_TEMPERATURE_MEASUREMENT_ATTR_ID_MINMEASUREDVALUE).await?;
    decode_min_measured_value(&tlv)
}

/// Read `MaxMeasuredValue` attribute from cluster `Temperature Measurement`.
pub async fn read_max_measured_value(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<i16>> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TEMPERATURE_MEASUREMENT, crate::clusters::defs::CLUSTER_TEMPERATURE_MEASUREMENT_ATTR_ID_MAXMEASUREDVALUE).await?;
    decode_max_measured_value(&tlv)
}

/// Read `Tolerance` attribute from cluster `Temperature Measurement`.
pub async fn read_tolerance(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u16> {
    let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_TEMPERATURE_MEASUREMENT, crate::clusters::defs::CLUSTER_TEMPERATURE_MEASUREMENT_ATTR_ID_TOLERANCE).await?;
    decode_tolerance(&tlv)
}