This commit is contained in:
2026-03-15 19:42:18 +00:00
commit bb39568f01
9 changed files with 535 additions and 0 deletions

47
src/battery.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::{f32, fs};
use crate::data::{BatteryMutable, BatteryStatic};
pub struct Battery {
charge: i64,
total_capacity: i64,
}
#[derive(Debug)]
pub enum Battery_Error {
NoBattery,
}
impl Battery {
pub fn new() -> Result<Battery, Battery_Error> {
let battery_check = fs::read_dir("/sys/class/power_supply/BAT0");
match battery_check {
Ok(_) => {}
Err(_) => return Err(Battery_Error::NoBattery),
}
let energy_full: String =
fs::read_to_string("/sys/class/power_supply/BAT0/energy_full").unwrap();
Ok(Battery {
charge: 0,
total_capacity: energy_full.parse().unwrap(),
})
}
fn update(&mut self) -> Result<(), Battery_Error> {
let charge = fs::read_to_string("/sys/class/power_supply/BAT0/energy_now").unwrap();
self.charge = charge.parse().unwrap();
Ok(())
}
fn as_data(&self) -> (BatteryStatic, BatteryMutable) {
let batt_static = BatteryStatic {
total_capacity: self.total_capacity,
};
let batt_muteable = BatteryMutable {
charge: self.charge,
};
(batt_static, batt_muteable)
}
}

25
src/data.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::time::{SystemTime, UNIX_EPOCH};
use rkyv::{Archive, Deserialize, Serialize};
#[derive(Serialize, Deserialize, Archive)]
pub struct Data {
pub data_chunks: Vec<DataChunk>,
pub data_static: BatteryStatic,
}
#[derive(Serialize, Deserialize, Archive)]
pub struct DataChunk {
pub timestamp: i32,
pub battery: BatteryMutable,
}
#[derive(Serialize, Deserialize, Archive)]
pub struct BatteryMutable {
pub charge: i64,
}
#[derive(Serialize, Deserialize, Archive)]
pub struct BatteryStatic {
pub total_capacity: i64,
}

7
src/main.rs Normal file
View File

@@ -0,0 +1,7 @@
mod battery;
mod data;
fn main() {
let battery = battery::Battery::new();
battery.unwrap();
}