#![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 BatApprovedChemistry {
Unspecified = 0,
Alkaline = 1,
Lithiumcarbonfluoride = 2,
Lithiumchromiumoxide = 3,
Lithiumcopperoxide = 4,
Lithiumirondisulfide = 5,
Lithiummanganesedioxide = 6,
Lithiumthionylchloride = 7,
Magnesium = 8,
Mercuryoxide = 9,
Nickeloxyhydride = 10,
Silveroxide = 11,
Zincair = 12,
Zinccarbon = 13,
Zincchloride = 14,
Zincmanganesedioxide = 15,
Leadacid = 16,
Lithiumcobaltoxide = 17,
Lithiumion = 18,
Lithiumionpolymer = 19,
Lithiumironphosphate = 20,
Lithiumsulfur = 21,
Lithiumtitanate = 22,
Nickelcadmium = 23,
Nickelhydrogen = 24,
Nickeliron = 25,
Nickelmetalhydride = 26,
Nickelzinc = 27,
Silverzinc = 28,
Sodiumion = 29,
Sodiumsulfur = 30,
Zincbromide = 31,
Zinccerium = 32,
}
impl BatApprovedChemistry {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatApprovedChemistry::Unspecified),
1 => Some(BatApprovedChemistry::Alkaline),
2 => Some(BatApprovedChemistry::Lithiumcarbonfluoride),
3 => Some(BatApprovedChemistry::Lithiumchromiumoxide),
4 => Some(BatApprovedChemistry::Lithiumcopperoxide),
5 => Some(BatApprovedChemistry::Lithiumirondisulfide),
6 => Some(BatApprovedChemistry::Lithiummanganesedioxide),
7 => Some(BatApprovedChemistry::Lithiumthionylchloride),
8 => Some(BatApprovedChemistry::Magnesium),
9 => Some(BatApprovedChemistry::Mercuryoxide),
10 => Some(BatApprovedChemistry::Nickeloxyhydride),
11 => Some(BatApprovedChemistry::Silveroxide),
12 => Some(BatApprovedChemistry::Zincair),
13 => Some(BatApprovedChemistry::Zinccarbon),
14 => Some(BatApprovedChemistry::Zincchloride),
15 => Some(BatApprovedChemistry::Zincmanganesedioxide),
16 => Some(BatApprovedChemistry::Leadacid),
17 => Some(BatApprovedChemistry::Lithiumcobaltoxide),
18 => Some(BatApprovedChemistry::Lithiumion),
19 => Some(BatApprovedChemistry::Lithiumionpolymer),
20 => Some(BatApprovedChemistry::Lithiumironphosphate),
21 => Some(BatApprovedChemistry::Lithiumsulfur),
22 => Some(BatApprovedChemistry::Lithiumtitanate),
23 => Some(BatApprovedChemistry::Nickelcadmium),
24 => Some(BatApprovedChemistry::Nickelhydrogen),
25 => Some(BatApprovedChemistry::Nickeliron),
26 => Some(BatApprovedChemistry::Nickelmetalhydride),
27 => Some(BatApprovedChemistry::Nickelzinc),
28 => Some(BatApprovedChemistry::Silverzinc),
29 => Some(BatApprovedChemistry::Sodiumion),
30 => Some(BatApprovedChemistry::Sodiumsulfur),
31 => Some(BatApprovedChemistry::Zincbromide),
32 => Some(BatApprovedChemistry::Zinccerium),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatApprovedChemistry> for u8 {
fn from(val: BatApprovedChemistry) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatChargeFault {
Unspecified = 0,
Ambienttoohot = 1,
Ambienttoocold = 2,
Batterytoohot = 3,
Batterytoocold = 4,
Batteryabsent = 5,
Batteryovervoltage = 6,
Batteryundervoltage = 7,
Chargerovervoltage = 8,
Chargerundervoltage = 9,
Safetytimeout = 10,
}
impl BatChargeFault {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatChargeFault::Unspecified),
1 => Some(BatChargeFault::Ambienttoohot),
2 => Some(BatChargeFault::Ambienttoocold),
3 => Some(BatChargeFault::Batterytoohot),
4 => Some(BatChargeFault::Batterytoocold),
5 => Some(BatChargeFault::Batteryabsent),
6 => Some(BatChargeFault::Batteryovervoltage),
7 => Some(BatChargeFault::Batteryundervoltage),
8 => Some(BatChargeFault::Chargerovervoltage),
9 => Some(BatChargeFault::Chargerundervoltage),
10 => Some(BatChargeFault::Safetytimeout),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatChargeFault> for u8 {
fn from(val: BatChargeFault) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatChargeLevel {
Ok = 0,
Warning = 1,
Critical = 2,
}
impl BatChargeLevel {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatChargeLevel::Ok),
1 => Some(BatChargeLevel::Warning),
2 => Some(BatChargeLevel::Critical),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatChargeLevel> for u8 {
fn from(val: BatChargeLevel) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatChargeState {
Unknown = 0,
Ischarging = 1,
Isatfullcharge = 2,
Isnotcharging = 3,
}
impl BatChargeState {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatChargeState::Unknown),
1 => Some(BatChargeState::Ischarging),
2 => Some(BatChargeState::Isatfullcharge),
3 => Some(BatChargeState::Isnotcharging),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatChargeState> for u8 {
fn from(val: BatChargeState) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatCommonDesignation {
Unspecified = 0,
Aaa = 1,
Aa = 2,
C = 3,
D = 4,
_4v5 = 5,
_6v0 = 6,
_9v0 = 7,
_12aa = 8,
Aaaa = 9,
A = 10,
B = 11,
F = 12,
N = 13,
No6 = 14,
Subc = 15,
A23 = 16,
A27 = 17,
Ba5800 = 18,
Duplex = 19,
_4sr44 = 20,
_523 = 21,
_531 = 22,
_15v0 = 23,
_22v5 = 24,
_30v0 = 25,
_45v0 = 26,
_67v5 = 27,
J = 28,
Cr123a = 29,
Cr2 = 30,
_2cr5 = 31,
CrP2 = 32,
CrV3 = 33,
Sr41 = 34,
Sr43 = 35,
Sr44 = 36,
Sr45 = 37,
Sr48 = 38,
Sr54 = 39,
Sr55 = 40,
Sr57 = 41,
Sr58 = 42,
Sr59 = 43,
Sr60 = 44,
Sr63 = 45,
Sr64 = 46,
Sr65 = 47,
Sr66 = 48,
Sr67 = 49,
Sr68 = 50,
Sr69 = 51,
Sr516 = 52,
Sr731 = 53,
Sr712 = 54,
Lr932 = 55,
A5 = 56,
A10 = 57,
A13 = 58,
A312 = 59,
A675 = 60,
Ac41e = 61,
_10180 = 62,
_10280 = 63,
_10440 = 64,
_14250 = 65,
_14430 = 66,
_14500 = 67,
_14650 = 68,
_15270 = 69,
_16340 = 70,
Rcr123a = 71,
_17500 = 72,
_17670 = 73,
_18350 = 74,
_18500 = 75,
_18650 = 76,
_19670 = 77,
_25500 = 78,
_26650 = 79,
_32600 = 80,
}
impl BatCommonDesignation {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatCommonDesignation::Unspecified),
1 => Some(BatCommonDesignation::Aaa),
2 => Some(BatCommonDesignation::Aa),
3 => Some(BatCommonDesignation::C),
4 => Some(BatCommonDesignation::D),
5 => Some(BatCommonDesignation::_4v5),
6 => Some(BatCommonDesignation::_6v0),
7 => Some(BatCommonDesignation::_9v0),
8 => Some(BatCommonDesignation::_12aa),
9 => Some(BatCommonDesignation::Aaaa),
10 => Some(BatCommonDesignation::A),
11 => Some(BatCommonDesignation::B),
12 => Some(BatCommonDesignation::F),
13 => Some(BatCommonDesignation::N),
14 => Some(BatCommonDesignation::No6),
15 => Some(BatCommonDesignation::Subc),
16 => Some(BatCommonDesignation::A23),
17 => Some(BatCommonDesignation::A27),
18 => Some(BatCommonDesignation::Ba5800),
19 => Some(BatCommonDesignation::Duplex),
20 => Some(BatCommonDesignation::_4sr44),
21 => Some(BatCommonDesignation::_523),
22 => Some(BatCommonDesignation::_531),
23 => Some(BatCommonDesignation::_15v0),
24 => Some(BatCommonDesignation::_22v5),
25 => Some(BatCommonDesignation::_30v0),
26 => Some(BatCommonDesignation::_45v0),
27 => Some(BatCommonDesignation::_67v5),
28 => Some(BatCommonDesignation::J),
29 => Some(BatCommonDesignation::Cr123a),
30 => Some(BatCommonDesignation::Cr2),
31 => Some(BatCommonDesignation::_2cr5),
32 => Some(BatCommonDesignation::CrP2),
33 => Some(BatCommonDesignation::CrV3),
34 => Some(BatCommonDesignation::Sr41),
35 => Some(BatCommonDesignation::Sr43),
36 => Some(BatCommonDesignation::Sr44),
37 => Some(BatCommonDesignation::Sr45),
38 => Some(BatCommonDesignation::Sr48),
39 => Some(BatCommonDesignation::Sr54),
40 => Some(BatCommonDesignation::Sr55),
41 => Some(BatCommonDesignation::Sr57),
42 => Some(BatCommonDesignation::Sr58),
43 => Some(BatCommonDesignation::Sr59),
44 => Some(BatCommonDesignation::Sr60),
45 => Some(BatCommonDesignation::Sr63),
46 => Some(BatCommonDesignation::Sr64),
47 => Some(BatCommonDesignation::Sr65),
48 => Some(BatCommonDesignation::Sr66),
49 => Some(BatCommonDesignation::Sr67),
50 => Some(BatCommonDesignation::Sr68),
51 => Some(BatCommonDesignation::Sr69),
52 => Some(BatCommonDesignation::Sr516),
53 => Some(BatCommonDesignation::Sr731),
54 => Some(BatCommonDesignation::Sr712),
55 => Some(BatCommonDesignation::Lr932),
56 => Some(BatCommonDesignation::A5),
57 => Some(BatCommonDesignation::A10),
58 => Some(BatCommonDesignation::A13),
59 => Some(BatCommonDesignation::A312),
60 => Some(BatCommonDesignation::A675),
61 => Some(BatCommonDesignation::Ac41e),
62 => Some(BatCommonDesignation::_10180),
63 => Some(BatCommonDesignation::_10280),
64 => Some(BatCommonDesignation::_10440),
65 => Some(BatCommonDesignation::_14250),
66 => Some(BatCommonDesignation::_14430),
67 => Some(BatCommonDesignation::_14500),
68 => Some(BatCommonDesignation::_14650),
69 => Some(BatCommonDesignation::_15270),
70 => Some(BatCommonDesignation::_16340),
71 => Some(BatCommonDesignation::Rcr123a),
72 => Some(BatCommonDesignation::_17500),
73 => Some(BatCommonDesignation::_17670),
74 => Some(BatCommonDesignation::_18350),
75 => Some(BatCommonDesignation::_18500),
76 => Some(BatCommonDesignation::_18650),
77 => Some(BatCommonDesignation::_19670),
78 => Some(BatCommonDesignation::_25500),
79 => Some(BatCommonDesignation::_26650),
80 => Some(BatCommonDesignation::_32600),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatCommonDesignation> for u8 {
fn from(val: BatCommonDesignation) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatFault {
Unspecified = 0,
Overtemp = 1,
Undertemp = 2,
}
impl BatFault {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatFault::Unspecified),
1 => Some(BatFault::Overtemp),
2 => Some(BatFault::Undertemp),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatFault> for u8 {
fn from(val: BatFault) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum BatReplaceability {
Unspecified = 0,
Notreplaceable = 1,
Userreplaceable = 2,
Factoryreplaceable = 3,
}
impl BatReplaceability {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(BatReplaceability::Unspecified),
1 => Some(BatReplaceability::Notreplaceable),
2 => Some(BatReplaceability::Userreplaceable),
3 => Some(BatReplaceability::Factoryreplaceable),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<BatReplaceability> for u8 {
fn from(val: BatReplaceability) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum PowerSourceStatus {
Unspecified = 0,
Active = 1,
Standby = 2,
Unavailable = 3,
}
impl PowerSourceStatus {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(PowerSourceStatus::Unspecified),
1 => Some(PowerSourceStatus::Active),
2 => Some(PowerSourceStatus::Standby),
3 => Some(PowerSourceStatus::Unavailable),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<PowerSourceStatus> for u8 {
fn from(val: PowerSourceStatus) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum WiredCurrentType {
Ac = 0,
Dc = 1,
}
impl WiredCurrentType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(WiredCurrentType::Ac),
1 => Some(WiredCurrentType::Dc),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<WiredCurrentType> for u8 {
fn from(val: WiredCurrentType) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum WiredFault {
Unspecified = 0,
Overvoltage = 1,
Undervoltage = 2,
}
impl WiredFault {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(WiredFault::Unspecified),
1 => Some(WiredFault::Overvoltage),
2 => Some(WiredFault::Undervoltage),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<WiredFault> for u8 {
fn from(val: WiredFault) -> Self {
val as u8
}
}
pub fn decode_status(inp: &tlv::TlvItemValue) -> anyhow::Result<PowerSourceStatus> {
if let tlv::TlvItemValue::Int(v) = inp {
PowerSourceStatus::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_order(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_description(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
if let tlv::TlvItemValue::String(v) = inp {
Ok(v.clone())
} else {
Err(anyhow::anyhow!("Expected String"))
}
}
pub fn decode_wired_assessed_input_voltage(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_wired_assessed_input_frequency(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u16))
} else {
Ok(None)
}
}
pub fn decode_wired_current_type(inp: &tlv::TlvItemValue) -> anyhow::Result<WiredCurrentType> {
if let tlv::TlvItemValue::Int(v) = inp {
WiredCurrentType::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_wired_assessed_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_wired_nominal_voltage(inp: &tlv::TlvItemValue) -> anyhow::Result<u32> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u32)
} else {
Err(anyhow::anyhow!("Expected UInt32"))
}
}
pub fn decode_wired_maximum_current(inp: &tlv::TlvItemValue) -> anyhow::Result<u32> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u32)
} else {
Err(anyhow::anyhow!("Expected UInt32"))
}
}
pub fn decode_wired_present(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_active_wired_faults(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<WiredFault>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
if let Some(enum_val) = WiredFault::from_u8(*i as u8) {
res.push(enum_val);
}
}
}
}
Ok(res)
}
pub fn decode_bat_voltage(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_bat_percent_remaining(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_bat_time_remaining(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_bat_charge_level(inp: &tlv::TlvItemValue) -> anyhow::Result<BatChargeLevel> {
if let tlv::TlvItemValue::Int(v) = inp {
BatChargeLevel::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_bat_replacement_needed(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_bat_replaceability(inp: &tlv::TlvItemValue) -> anyhow::Result<BatReplaceability> {
if let tlv::TlvItemValue::Int(v) = inp {
BatReplaceability::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_bat_present(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_active_bat_faults(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<BatFault>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
if let Some(enum_val) = BatFault::from_u8(*i as u8) {
res.push(enum_val);
}
}
}
}
Ok(res)
}
pub fn decode_bat_replacement_description(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
if let tlv::TlvItemValue::String(v) = inp {
Ok(v.clone())
} else {
Err(anyhow::anyhow!("Expected String"))
}
}
pub fn decode_bat_common_designation(inp: &tlv::TlvItemValue) -> anyhow::Result<BatCommonDesignation> {
if let tlv::TlvItemValue::Int(v) = inp {
BatCommonDesignation::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_bat_ansi_designation(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
if let tlv::TlvItemValue::String(v) = inp {
Ok(v.clone())
} else {
Err(anyhow::anyhow!("Expected String"))
}
}
pub fn decode_bat_iec_designation(inp: &tlv::TlvItemValue) -> anyhow::Result<String> {
if let tlv::TlvItemValue::String(v) = inp {
Ok(v.clone())
} else {
Err(anyhow::anyhow!("Expected String"))
}
}
pub fn decode_bat_approved_chemistry(inp: &tlv::TlvItemValue) -> anyhow::Result<BatApprovedChemistry> {
if let tlv::TlvItemValue::Int(v) = inp {
BatApprovedChemistry::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_bat_capacity(inp: &tlv::TlvItemValue) -> anyhow::Result<u32> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u32)
} else {
Err(anyhow::anyhow!("Expected UInt32"))
}
}
pub fn decode_bat_quantity(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_bat_charge_state(inp: &tlv::TlvItemValue) -> anyhow::Result<BatChargeState> {
if let tlv::TlvItemValue::Int(v) = inp {
BatChargeState::from_u8(*v as u8).ok_or_else(|| anyhow::anyhow!("Invalid enum value"))
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_bat_time_to_full_charge(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_bat_functional_while_charging(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
if let tlv::TlvItemValue::Bool(v) = inp {
Ok(*v)
} else {
Err(anyhow::anyhow!("Expected Bool"))
}
}
pub fn decode_bat_charging_current(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_active_bat_charge_faults(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<BatChargeFault>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
if let Some(enum_val) = BatChargeFault::from_u8(*i as u8) {
res.push(enum_val);
}
}
}
}
Ok(res)
}
pub fn decode_endpoint_list(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<u16>> {
let mut res = Vec::new();
if let tlv::TlvItemValue::List(v) = inp {
for item in v {
if let tlv::TlvItemValue::Int(i) = &item.value {
res.push(*i as u16);
}
}
}
Ok(res)
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x002F {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002F, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_status(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_order(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_description(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_wired_assessed_input_voltage(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_wired_assessed_input_frequency(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0005 => {
match decode_wired_current_type(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0006 => {
match decode_wired_assessed_current(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0007 => {
match decode_wired_nominal_voltage(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0008 => {
match decode_wired_maximum_current(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0009 => {
match decode_wired_present(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000A => {
match decode_active_wired_faults(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000B => {
match decode_bat_voltage(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000C => {
match decode_bat_percent_remaining(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000D => {
match decode_bat_time_remaining(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000E => {
match decode_bat_charge_level(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000F => {
match decode_bat_replacement_needed(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0010 => {
match decode_bat_replaceability(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0011 => {
match decode_bat_present(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0012 => {
match decode_active_bat_faults(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0013 => {
match decode_bat_replacement_description(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0014 => {
match decode_bat_common_designation(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0015 => {
match decode_bat_ansi_designation(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0016 => {
match decode_bat_iec_designation(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0017 => {
match decode_bat_approved_chemistry(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0018 => {
match decode_bat_capacity(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0019 => {
match decode_bat_quantity(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001A => {
match decode_bat_charge_state(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001B => {
match decode_bat_time_to_full_charge(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001C => {
match decode_bat_functional_while_charging(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001D => {
match decode_bat_charging_current(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001E => {
match decode_active_bat_charge_faults(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x001F => {
match decode_endpoint_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, "Status"),
(0x0001, "Order"),
(0x0002, "Description"),
(0x0003, "WiredAssessedInputVoltage"),
(0x0004, "WiredAssessedInputFrequency"),
(0x0005, "WiredCurrentType"),
(0x0006, "WiredAssessedCurrent"),
(0x0007, "WiredNominalVoltage"),
(0x0008, "WiredMaximumCurrent"),
(0x0009, "WiredPresent"),
(0x000A, "ActiveWiredFaults"),
(0x000B, "BatVoltage"),
(0x000C, "BatPercentRemaining"),
(0x000D, "BatTimeRemaining"),
(0x000E, "BatChargeLevel"),
(0x000F, "BatReplacementNeeded"),
(0x0010, "BatReplaceability"),
(0x0011, "BatPresent"),
(0x0012, "ActiveBatFaults"),
(0x0013, "BatReplacementDescription"),
(0x0014, "BatCommonDesignation"),
(0x0015, "BatANSIDesignation"),
(0x0016, "BatIECDesignation"),
(0x0017, "BatApprovedChemistry"),
(0x0018, "BatCapacity"),
(0x0019, "BatQuantity"),
(0x001A, "BatChargeState"),
(0x001B, "BatTimeToFullCharge"),
(0x001C, "BatFunctionalWhileCharging"),
(0x001D, "BatChargingCurrent"),
(0x001E, "ActiveBatChargeFaults"),
(0x001F, "EndpointList"),
]
}
pub async fn read_status(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<PowerSourceStatus> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_STATUS).await?;
decode_status(&tlv)
}
pub async fn read_order(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_ORDER).await?;
decode_order(&tlv)
}
pub async fn read_description(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_DESCRIPTION).await?;
decode_description(&tlv)
}
pub async fn read_wired_assessed_input_voltage(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDASSESSEDINPUTVOLTAGE).await?;
decode_wired_assessed_input_voltage(&tlv)
}
pub async fn read_wired_assessed_input_frequency(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDASSESSEDINPUTFREQUENCY).await?;
decode_wired_assessed_input_frequency(&tlv)
}
pub async fn read_wired_current_type(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<WiredCurrentType> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDCURRENTTYPE).await?;
decode_wired_current_type(&tlv)
}
pub async fn read_wired_assessed_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDASSESSEDCURRENT).await?;
decode_wired_assessed_current(&tlv)
}
pub async fn read_wired_nominal_voltage(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u32> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDNOMINALVOLTAGE).await?;
decode_wired_nominal_voltage(&tlv)
}
pub async fn read_wired_maximum_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u32> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDMAXIMUMCURRENT).await?;
decode_wired_maximum_current(&tlv)
}
pub async fn read_wired_present(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_WIREDPRESENT).await?;
decode_wired_present(&tlv)
}
pub async fn read_active_wired_faults(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<WiredFault>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_ACTIVEWIREDFAULTS).await?;
decode_active_wired_faults(&tlv)
}
pub async fn read_bat_voltage(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATVOLTAGE).await?;
decode_bat_voltage(&tlv)
}
pub async fn read_bat_percent_remaining(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u8>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATPERCENTREMAINING).await?;
decode_bat_percent_remaining(&tlv)
}
pub async fn read_bat_time_remaining(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATTIMEREMAINING).await?;
decode_bat_time_remaining(&tlv)
}
pub async fn read_bat_charge_level(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<BatChargeLevel> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATCHARGELEVEL).await?;
decode_bat_charge_level(&tlv)
}
pub async fn read_bat_replacement_needed(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATREPLACEMENTNEEDED).await?;
decode_bat_replacement_needed(&tlv)
}
pub async fn read_bat_replaceability(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<BatReplaceability> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATREPLACEABILITY).await?;
decode_bat_replaceability(&tlv)
}
pub async fn read_bat_present(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATPRESENT).await?;
decode_bat_present(&tlv)
}
pub async fn read_active_bat_faults(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<BatFault>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_ACTIVEBATFAULTS).await?;
decode_active_bat_faults(&tlv)
}
pub async fn read_bat_replacement_description(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATREPLACEMENTDESCRIPTION).await?;
decode_bat_replacement_description(&tlv)
}
pub async fn read_bat_common_designation(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<BatCommonDesignation> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATCOMMONDESIGNATION).await?;
decode_bat_common_designation(&tlv)
}
pub async fn read_bat_ansi_designation(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATANSIDESIGNATION).await?;
decode_bat_ansi_designation(&tlv)
}
pub async fn read_bat_iec_designation(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<String> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATIECDESIGNATION).await?;
decode_bat_iec_designation(&tlv)
}
pub async fn read_bat_approved_chemistry(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<BatApprovedChemistry> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATAPPROVEDCHEMISTRY).await?;
decode_bat_approved_chemistry(&tlv)
}
pub async fn read_bat_capacity(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u32> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATCAPACITY).await?;
decode_bat_capacity(&tlv)
}
pub async fn read_bat_quantity(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<u8> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATQUANTITY).await?;
decode_bat_quantity(&tlv)
}
pub async fn read_bat_charge_state(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<BatChargeState> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATCHARGESTATE).await?;
decode_bat_charge_state(&tlv)
}
pub async fn read_bat_time_to_full_charge(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATTIMETOFULLCHARGE).await?;
decode_bat_time_to_full_charge(&tlv)
}
pub async fn read_bat_functional_while_charging(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<bool> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATFUNCTIONALWHILECHARGING).await?;
decode_bat_functional_while_charging(&tlv)
}
pub async fn read_bat_charging_current(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Option<u32>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_BATCHARGINGCURRENT).await?;
decode_bat_charging_current(&tlv)
}
pub async fn read_active_bat_charge_faults(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<BatChargeFault>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_ACTIVEBATCHARGEFAULTS).await?;
decode_active_bat_charge_faults(&tlv)
}
pub async fn read_endpoint_list(conn: &crate::controller::Connection, endpoint: u16) -> anyhow::Result<Vec<u16>> {
let tlv = conn.read_request2(endpoint, crate::clusters::defs::CLUSTER_ID_POWER_SOURCE, crate::clusters::defs::CLUSTER_POWER_SOURCE_ATTR_ID_ENDPOINTLIST).await?;
decode_endpoint_list(&tlv)
}
#[derive(Debug, serde::Serialize)]
pub struct WiredFaultChangeEvent {
pub current: Option<Vec<WiredFault>>,
pub previous: Option<Vec<WiredFault>>,
}
#[derive(Debug, serde::Serialize)]
pub struct BatFaultChangeEvent {
pub current: Option<Vec<BatFault>>,
pub previous: Option<Vec<BatFault>>,
}
#[derive(Debug, serde::Serialize)]
pub struct BatChargeFaultChangeEvent {
pub current: Option<Vec<BatChargeFault>>,
pub previous: Option<Vec<BatChargeFault>>,
}
pub fn decode_wired_fault_change_event(inp: &tlv::TlvItemValue) -> anyhow::Result<WiredFaultChangeEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(WiredFaultChangeEvent {
current: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[0]) {
let items: Vec<WiredFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { WiredFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
previous: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[1]) {
let items: Vec<WiredFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { WiredFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_bat_fault_change_event(inp: &tlv::TlvItemValue) -> anyhow::Result<BatFaultChangeEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(BatFaultChangeEvent {
current: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[0]) {
let items: Vec<BatFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { BatFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
previous: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[1]) {
let items: Vec<BatFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { BatFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_bat_charge_fault_change_event(inp: &tlv::TlvItemValue) -> anyhow::Result<BatChargeFaultChangeEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(BatChargeFaultChangeEvent {
current: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[0]) {
let items: Vec<BatChargeFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { BatChargeFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
previous: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[1]) {
let items: Vec<BatChargeFault> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { BatChargeFault::from_u8(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}