#![allow(clippy::too_many_arguments)]
use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ChangeIndication {
Ok = 0,
Warning = 1,
Critical = 2,
}
impl ChangeIndication {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(ChangeIndication::Ok),
1 => Some(ChangeIndication::Warning),
2 => Some(ChangeIndication::Critical),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<ChangeIndication> for u8 {
fn from(val: ChangeIndication) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum DegradationDirection {
Up = 0,
Down = 1,
}
impl DegradationDirection {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(DegradationDirection::Up),
1 => Some(DegradationDirection::Down),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<DegradationDirection> for u8 {
fn from(val: DegradationDirection) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ProductIdentifierType {
Upc = 0,
Gtin8 = 1,
Ean = 2,
Gtin14 = 3,
Oem = 4,
}
impl ProductIdentifierType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(ProductIdentifierType::Upc),
1 => Some(ProductIdentifierType::Gtin8),
2 => Some(ProductIdentifierType::Ean),
3 => Some(ProductIdentifierType::Gtin14),
4 => Some(ProductIdentifierType::Oem),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<ProductIdentifierType> for u8 {
fn from(val: ProductIdentifierType) -> Self {
val as u8
}
}
#[derive(Debug, serde::Serialize)]
pub struct ReplacementProduct {
pub product_identifier_type: Option<ProductIdentifierType>,
pub product_identifier_value: Option<String>,
}
pub fn decode_condition(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected UInt8"))
}
}
pub fn decode_degradation_direction(inp: &tlv::TlvItemValue) -> anyhow::Result<DegradationDirection> {
if let tlv::TlvItemValue::Int(v) = inp {
DegradationDirection::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_change_indication(inp: &tlv::TlvItemValue) -> anyhow::Result<ChangeIndication> {
if let tlv::TlvItemValue::Int(v) = inp {
ChangeIndication::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_in_place_indicator(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_last_changed_time(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v))
} else {
Ok(None)
}
}
pub fn decode_replacement_product_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ReplacementProduct>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
res.push(ReplacementProduct {
product_identifier_type: item.get_int(&[0]).and_then(|v| ProductIdentifierType::from_u8(v as u8)),
product_identifier_value: item.get_string_owned(&[1]),
});
}
}
Ok(res)
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0000 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0000, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_condition(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_degradation_direction(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_change_indication(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_in_place_indicator(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_last_changed_time(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0005 => {
match decode_replacement_product_list(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),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "Condition"),
(0x0001, "DegradationDirection"),
(0x0002, "ChangeIndication"),
(0x0003, "InPlaceIndicator"),
(0x0004, "LastChangedTime"),
(0x0005, "ReplacementProductList"),
]
}
pub fn get_command_list() -> Vec<(u32, &'static str)> {
vec![
(0x00, "ResetCondition"),
]
}
pub fn get_command_name(cmd_id: u32) -> Option<&'static str> {
match cmd_id {
0x00 => Some("ResetCondition"),
_ => None,
}
}
pub fn get_command_schema(cmd_id: u32) -> Option<Vec<crate::clusters::codec::CommandField>> {
match cmd_id {
0x00 => Some(vec![]),
_ => None,
}
}
pub fn encode_command_json(cmd_id: u32, _args: &serde_json::Value) -> anyhow::Result<Vec<u8>> {
match cmd_id {
0x00 => Ok(vec![]),
_ => Err(anyhow::anyhow!("unknown command ID: 0x{:02X}", cmd_id)),
}
}
pub async fn reset_condition(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<()> {
conn.invoke_request(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_CMD_ID_RESETCONDITION, &[]).await?;
Ok(())
}
pub async fn read_condition(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_CONDITION).await?;
decode_condition(&tlv)
}
pub async fn read_degradation_direction(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<DegradationDirection> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_DEGRADATIONDIRECTION).await?;
decode_degradation_direction(&tlv)
}
pub async fn read_change_indication(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<ChangeIndication> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_CHANGEINDICATION).await?;
decode_change_indication(&tlv)
}
pub async fn read_in_place_indicator(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_INPLACEINDICATOR).await?;
decode_in_place_indicator(&tlv)
}
pub async fn read_last_changed_time(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u64>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_LASTCHANGEDTIME).await?;
decode_last_changed_time(&tlv)
}
pub async fn read_replacement_product_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<ReplacementProduct>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_WATER_TANK_LEVEL_MONITORING, crate::clusters::defs::CLUSTER_WATER_TANK_LEVEL_MONITORING_ATTR_ID_REPLACEMENTPRODUCTLIST).await?;
decode_replacement_product_list(&tlv)
}