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"),
]
}