Skip to content

Commit

Permalink
Daemon battery info
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Oct 4, 2024
1 parent 4b70700 commit db20512
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 16 deletions.
25 changes: 10 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion platforms/unix/hardwareinfo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
netdev = "0.29.0"
sysinfo = "0.30.12"
starship-battery = "0.8.3"
starship-battery = "0.10.0"
indexmap = "2.2.6"
nvml-wrapper = "0.10.0"
serde = { version = "1", features = ["derive"] }
Expand Down
89 changes: 89 additions & 0 deletions platforms/unix/hardwareinfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use netdev::{get_default_interface, ip::Ipv4Net, mac::MacAddr, NetworkDevice};
use nvml_wrapper::enum_wrappers::device::{Clock, TemperatureSensor};
use nvml_wrapper::struct_wrappers::device::{MemoryInfo, Utilization};
use serde::{Deserialize, Serialize};
use starship_battery::units::energy::milliwatt_hour;
use starship_battery::units::ratio::percent;
use std::time::SystemTime;
use std::{
env,
Expand Down Expand Up @@ -174,6 +176,15 @@ pub struct CoresMotherboard {
pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CoresBattery {
pub cycle_count: String,
pub level: Vec<CoresSensor>,
pub remaining_time: CoresSensor,
pub capacity: Vec<CoresSensor>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CoresBIOS {
pub vendor: String,
Expand All @@ -199,6 +210,7 @@ pub struct CoresSystem {
pub bios: CoresBIOS,
pub superIO: CoresSuperIO,
pub monitor: CoresMonitor,
pub battery: CoresBattery,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -268,6 +280,12 @@ impl HardwareInfo {
monitor: CoresMonitor {
monitors: Vec::new(),
},
battery: CoresBattery {
cycle_count: "N/A".to_string(),
level: Vec::new(),
remaining_time: CoresSensor::default(),
capacity: Vec::new(),
},
},
}
}
Expand Down Expand Up @@ -665,6 +683,77 @@ pub fn refresh_hardware_info(data: &mut Data) {
}
}

// Battery
if data.first_run {
let manager = starship_battery::Manager::new();

match manager {
Ok(manager) => {
if let Ok(batteries) = manager.batteries() {
for battery in batteries {
if let Ok(battery) = battery {
let cycle_count = battery.cycle_count().unwrap_or(0);
let charge_level = battery.state_of_charge().get::<percent>();
let design_capacity =
battery.energy_full_design().get::<milliwatt_hour>();
let full_charge_capacity =
battery.energy_full().get::<milliwatt_hour>();
let remaining_capacity = battery.energy().get::<milliwatt_hour>();
let health = battery.state_of_health().get::<percent>();

data.hw_info.system.battery = CoresBattery {
cycle_count: cycle_count.to_string(),
level: Vec::new(),
remaining_time: CoresSensor::default(),
capacity: Vec::new(),
};

data.hw_info.system.battery.level.push(CoresSensor {
name: "Charge level".to_string(),
value: charge_level as f64,
min: charge_level as f64,
max: charge_level as f64,
});

data.hw_info.system.battery.level.push(CoresSensor {
name: "Health".to_string(),
value: 100.0 - health as f64,
min: 100.0 - health as f64,
max: 100.0 - health as f64,
});

data.hw_info.system.battery.capacity.push(CoresSensor {
name: "Design capacity".to_string(),
value: design_capacity as f64,
min: design_capacity as f64,
max: design_capacity as f64,
});

data.hw_info.system.battery.capacity.push(CoresSensor {
name: "Full charge capacity".to_string(),
value: full_charge_capacity as f64,
min: full_charge_capacity as f64,
max: full_charge_capacity as f64,
});

data.hw_info.system.battery.capacity.push(CoresSensor {
name: "Remaining capacity".to_string(),
value: remaining_capacity as f64,
min: remaining_capacity as f64,
max: remaining_capacity as f64,
});
} else {
error!("Error getting specific battery info");
}
}
}
}
Err(_err) => {
error!("Error getting battery info");
}
};
}

// END
data.first_run = false;
}

0 comments on commit db20512

Please sign in to comment.