|
| 1 | +use ratatui::{ |
| 2 | + Frame, |
| 3 | + layout::{Constraint, Margin, Rect}, |
| 4 | + style::Stylize, |
| 5 | + widgets::{Block, Cell, Padding, Row, Table}, |
| 6 | +}; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct Battery { |
| 10 | + location: String, |
| 11 | + manufacturer: String, |
| 12 | + manufacture_date: String, |
| 13 | + serial_number: String, |
| 14 | + device_name: String, |
| 15 | + device_chemistry: String, |
| 16 | + design_capacity: Option<u16>, |
| 17 | + design_voltage: Option<u16>, |
| 18 | + sbds_version: String, |
| 19 | + max_error_in_battery: Option<u8>, |
| 20 | + oem_specific: u32, |
| 21 | +} |
| 22 | + |
| 23 | +impl From<(Vec<u8>, Vec<String>)> for Battery { |
| 24 | + fn from((data, text): (Vec<u8>, Vec<String>)) -> Self { |
| 25 | + let manufacture_date = if data[2] == 0 { |
| 26 | + let value = u16::from_le_bytes(data[14..16].try_into().unwrap()); |
| 27 | + let day = (value & 0b11111) as u8; |
| 28 | + let month = ((value >> 5) & 0b1111) as u8; |
| 29 | + let year = (value >> 9) + 1980; |
| 30 | + |
| 31 | + format!("{}-{:02}-{:02}", year, month, day) |
| 32 | + } else { |
| 33 | + text[data[2].saturating_sub(1) as usize].clone() |
| 34 | + }; |
| 35 | + |
| 36 | + let serial_number = if data[3] == 0 { |
| 37 | + format!( |
| 38 | + "0x{:X}", |
| 39 | + u16::from_le_bytes(data[12..14].try_into().unwrap()) |
| 40 | + ) |
| 41 | + } else { |
| 42 | + text[data[3].saturating_sub(1) as usize].clone() |
| 43 | + }; |
| 44 | + |
| 45 | + let design_voltage = { |
| 46 | + let value = u16::from_le_bytes(data[8..10].try_into().unwrap()); |
| 47 | + if value == 0 { None } else { Some(value) } |
| 48 | + }; |
| 49 | + |
| 50 | + let device_chemistry = { |
| 51 | + if data[5] == 2 { |
| 52 | + text[data[16].saturating_sub(1) as usize].clone() |
| 53 | + } else { |
| 54 | + Chemistry::from(data[5]).to_string() |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + let design_capacity = { |
| 59 | + let value = u16::from_le_bytes(data[6..8].try_into().unwrap()); |
| 60 | + if value == 0 { |
| 61 | + None |
| 62 | + } else { |
| 63 | + Some(value * (data[17] as u16)) |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + let max_error_in_battery = { |
| 68 | + if data[11] == 0xFF { |
| 69 | + None |
| 70 | + } else { |
| 71 | + Some(data[11]) |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + Self { |
| 76 | + location: text[data[0].saturating_sub(1) as usize].clone(), |
| 77 | + manufacturer: text[data[1].saturating_sub(1) as usize].clone(), |
| 78 | + manufacture_date, |
| 79 | + serial_number, |
| 80 | + device_name: text[data[4].saturating_sub(1) as usize].clone(), |
| 81 | + device_chemistry, |
| 82 | + design_capacity, |
| 83 | + design_voltage, |
| 84 | + sbds_version: text[data[10].saturating_sub(1) as usize].clone(), |
| 85 | + max_error_in_battery, |
| 86 | + oem_specific: u32::from_le_bytes(data[18..22].try_into().unwrap()), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl Battery { |
| 92 | + pub fn render(&self, frame: &mut Frame, block: Rect) { |
| 93 | + let rows = vec![ |
| 94 | + Row::new(vec![ |
| 95 | + Cell::from("location").bold(), |
| 96 | + Cell::from(self.location.clone()), |
| 97 | + ]), |
| 98 | + Row::new(vec![ |
| 99 | + Cell::from("Manufacturer").bold(), |
| 100 | + Cell::from(self.manufacturer.clone()), |
| 101 | + ]), |
| 102 | + Row::new(vec![ |
| 103 | + Cell::from("Manufacture Date").bold(), |
| 104 | + Cell::from(self.manufacture_date.clone()), |
| 105 | + ]), |
| 106 | + Row::new(vec![ |
| 107 | + Cell::from("Serial number").bold(), |
| 108 | + Cell::from(self.serial_number.clone()), |
| 109 | + ]), |
| 110 | + Row::new(vec![ |
| 111 | + Cell::from("Name").bold(), |
| 112 | + Cell::from(self.device_name.clone()), |
| 113 | + ]), |
| 114 | + Row::new(vec![ |
| 115 | + Cell::from("Chemistry").bold(), |
| 116 | + Cell::from(self.device_chemistry.clone()), |
| 117 | + ]), |
| 118 | + Row::new(vec![ |
| 119 | + Cell::from("Design Voltage").bold(), |
| 120 | + Cell::from({ |
| 121 | + if let Some(v) = self.design_voltage { |
| 122 | + format!("{v} mWV") |
| 123 | + } else { |
| 124 | + "Unknown".to_string() |
| 125 | + } |
| 126 | + }), |
| 127 | + ]), |
| 128 | + Row::new(vec![ |
| 129 | + Cell::from("Design Capacity").bold(), |
| 130 | + Cell::from({ |
| 131 | + if let Some(v) = self.design_capacity { |
| 132 | + format!("{v} mWh") |
| 133 | + } else { |
| 134 | + "Unknown".to_string() |
| 135 | + } |
| 136 | + }), |
| 137 | + ]), |
| 138 | + Row::new(vec![ |
| 139 | + Cell::from("SBDS Version").bold(), |
| 140 | + Cell::from(self.sbds_version.clone()), |
| 141 | + ]), |
| 142 | + Row::new(vec![ |
| 143 | + Cell::from("Maximum Error").bold(), |
| 144 | + Cell::from({ |
| 145 | + if let Some(v) = self.max_error_in_battery { |
| 146 | + format!("{v}%") |
| 147 | + } else { |
| 148 | + "Unknown".to_string() |
| 149 | + } |
| 150 | + }), |
| 151 | + ]), |
| 152 | + Row::new(vec![ |
| 153 | + Cell::from("OEM specific").bold(), |
| 154 | + Cell::from(format!("0x{:X}", self.oem_specific)), |
| 155 | + ]), |
| 156 | + ]; |
| 157 | + |
| 158 | + let widths = [Constraint::Length(20), Constraint::Fill(1)]; |
| 159 | + let table = Table::new(rows, widths).block(Block::new().padding(Padding::uniform(2))); |
| 160 | + frame.render_widget(table, block.inner(Margin::new(2, 0))); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#[derive(Debug, strum::Display)] |
| 165 | +enum Chemistry { |
| 166 | + #[strum(to_string = "Other")] |
| 167 | + Other, |
| 168 | + #[strum(to_string = "Unknown")] |
| 169 | + Unknown, |
| 170 | + #[strum(to_string = "Lead Acid")] |
| 171 | + LeadAcid, |
| 172 | + #[strum(to_string = "Nickel Cadmium")] |
| 173 | + NickelCadmium, |
| 174 | + #[strum(to_string = "Nickel metal hydride")] |
| 175 | + NickelMetalHydride, |
| 176 | + #[strum(to_string = "Lithium-ion")] |
| 177 | + LithiumIon, |
| 178 | + #[strum(to_string = "Zinc air")] |
| 179 | + ZincAir, |
| 180 | + #[strum(to_string = "Lithium Polymer")] |
| 181 | + LithiumPolymer, |
| 182 | +} |
| 183 | + |
| 184 | +impl From<u8> for Chemistry { |
| 185 | + fn from(value: u8) -> Self { |
| 186 | + match value { |
| 187 | + 1 => Self::Other, |
| 188 | + 2 => Self::Unknown, |
| 189 | + 3 => Self::LeadAcid, |
| 190 | + 4 => Self::NickelCadmium, |
| 191 | + 5 => Self::NickelMetalHydride, |
| 192 | + 6 => Self::LithiumIon, |
| 193 | + 7 => Self::ZincAir, |
| 194 | + 8 => Self::LithiumPolymer, |
| 195 | + _ => unreachable!(), |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments