cleanup some builtin vars
This commit is contained in:
parent
c613275621
commit
25e50eda46
1 changed files with 68 additions and 48 deletions
|
@ -75,13 +75,18 @@ pub fn get_cpus() -> String {
|
||||||
let mut c = SYSTEM.lock().unwrap();
|
let mut c = SYSTEM.lock().unwrap();
|
||||||
c.refresh_cpu_specifics(sysinfo::CpuRefreshKind::everything());
|
c.refresh_cpu_specifics(sysinfo::CpuRefreshKind::everything());
|
||||||
let cpus = c.cpus();
|
let cpus = c.cpus();
|
||||||
format!(
|
let json = serde_json::json!({
|
||||||
r#"{{ "cores": [{}], "avg": {} }}"#,
|
"cores": cpus.iter()
|
||||||
cpus.iter()
|
.map(|a| {
|
||||||
.map(|a| format!(r#"{{"core": "{}", "freq": {}, "usage": {:.0}}}"#, a.name(), a.frequency(), a.cpu_usage()))
|
serde_json::json!({
|
||||||
.join(","),
|
"core": a.name(),
|
||||||
cpus.iter().map(|a| a.cpu_usage()).avg()
|
"freq": a.frequency(),
|
||||||
)
|
"usage": a.cpu_usage() as i64
|
||||||
|
})
|
||||||
|
}).collect::<Vec<_>>(),
|
||||||
|
"avg": cpus.iter().map(|a| a.cpu_usage()).avg()
|
||||||
|
});
|
||||||
|
serde_json::to_string(&json).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
@ -111,48 +116,64 @@ pub fn get_battery_capacity() -> Result<String> {
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub fn get_battery_capacity() -> Result<String> {
|
pub fn get_battery_capacity() -> Result<String> {
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::{collections::HashMap, sync::atomic::AtomicBool};
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct BatteryData {
|
||||||
|
capacity: i64,
|
||||||
|
status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct Data {
|
||||||
|
#[serde(flatten)]
|
||||||
|
batteries: HashMap<String, BatteryData>,
|
||||||
|
total_avg: f64,
|
||||||
|
}
|
||||||
|
|
||||||
let mut current = 0_f64;
|
let mut current = 0_f64;
|
||||||
let mut total = 0_f64;
|
let mut total = 0_f64;
|
||||||
let mut json = String::from('{');
|
let mut batteries = HashMap::new();
|
||||||
for i in
|
let power_supply_dir = std::path::Path::new("/sys/class/power_supply");
|
||||||
std::path::Path::new("/sys/class/power_supply").read_dir().context("Couldn't read /sys/class/power_supply directory")?
|
let power_supply_entries = power_supply_dir.read_dir().context("Couldn't read /sys/class/power_supply directory")?;
|
||||||
{
|
for entry in power_supply_entries {
|
||||||
let i = i?.path();
|
let entry = entry?.path();
|
||||||
if i.is_dir() {
|
if !entry.is_dir() {
|
||||||
// some ugly hack because if let Some(a) = a && Some(b) = b doesn't work yet
|
continue;
|
||||||
if let (Ok(o), Ok(s)) = (read_to_string(i.join("capacity")), read_to_string(i.join("status"))) {
|
}
|
||||||
json.push_str(&format!(
|
if let (Ok(capacity), Ok(status)) = (read_to_string(entry.join("capacity")), read_to_string(entry.join("status"))) {
|
||||||
r#"{:?}: {{ "status": "{}", "capacity": {} }},"#,
|
batteries.insert(
|
||||||
i.file_name().context("couldn't convert file name to rust string")?,
|
entry.file_name().context("Couldn't get filename")?.to_string_lossy().to_string(),
|
||||||
s.trim_end_matches(|c| c == '\n'),
|
BatteryData {
|
||||||
o.trim_end_matches(|c| c == '\n')
|
status: status.trim_end_matches('\n').to_string(),
|
||||||
));
|
capacity: capacity.trim_end_matches('\n').parse::<f64>()?.round() as i64,
|
||||||
if let (Ok(t), Ok(c), Ok(v)) = (
|
},
|
||||||
read_to_string(i.join("charge_full")),
|
);
|
||||||
read_to_string(i.join("charge_now")),
|
if let (Ok(charge_full), Ok(charge_now), Ok(voltage_now)) = (
|
||||||
read_to_string(i.join("voltage_now")),
|
read_to_string(entry.join("charge_full")),
|
||||||
) {
|
read_to_string(entry.join("charge_now")),
|
||||||
// (uAh / 1000000) * U = p and that / one million so that we have microwatt
|
read_to_string(entry.join("voltage_now")),
|
||||||
current += ((c.trim_end_matches(|c| c == '\n').parse::<f64>()? / 1000000_f64)
|
) {
|
||||||
* v.trim_end_matches(|c| c == '\n').parse::<f64>()?)
|
// (uAh / 1000000) * U = p and that / one million so that we have microwatt
|
||||||
/ 1000000_f64;
|
current += ((charge_now.trim_end_matches('\n').parse::<f64>()? / 1000000_f64)
|
||||||
total += ((t.trim_end_matches(|c| c == '\n').parse::<f64>()? / 1000000_f64)
|
* voltage_now.trim_end_matches('\n').parse::<f64>()?)
|
||||||
* v.trim_end_matches(|c| c == '\n').parse::<f64>()?)
|
/ 1000000_f64;
|
||||||
/ 1000000_f64;
|
total += ((charge_full.trim_end_matches('\n').parse::<f64>()? / 1000000_f64)
|
||||||
} else if let (Ok(t), Ok(c)) = (read_to_string(i.join("energy_full")), read_to_string(i.join("energy_now"))) {
|
* voltage_now.trim_end_matches('\n').parse::<f64>()?)
|
||||||
current += c.trim_end_matches(|c| c == '\n').parse::<f64>()?;
|
/ 1000000_f64;
|
||||||
total += t.trim_end_matches(|c| c == '\n').parse::<f64>()?;
|
} else if let (Ok(energy_full), Ok(energy_now)) =
|
||||||
} else {
|
(read_to_string(entry.join("energy_full")), read_to_string(entry.join("energy_now")))
|
||||||
static WARNED: AtomicBool = AtomicBool::new(false);
|
{
|
||||||
if !WARNED.load(std::sync::atomic::Ordering::Relaxed) {
|
current += energy_now.trim_end_matches('\n').parse::<f64>()?;
|
||||||
WARNED.store(true, std::sync::atomic::Ordering::Relaxed);
|
total += energy_full.trim_end_matches('\n').parse::<f64>()?;
|
||||||
log::warn!(
|
} else {
|
||||||
"Failed to get/calculate uWh: the total_avg value of the battery magic var will probably be a \
|
static WARNED: AtomicBool = AtomicBool::new(false);
|
||||||
garbage value that can not be trusted."
|
if !WARNED.load(std::sync::atomic::Ordering::Relaxed) {
|
||||||
);
|
WARNED.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||||
}
|
log::warn!(
|
||||||
|
"Failed to get/calculate uWh: the total_avg value of the battery magic var will probably be a garbage \
|
||||||
|
value that can not be trusted."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,8 +182,7 @@ pub fn get_battery_capacity() -> Result<String> {
|
||||||
return Ok(String::from(""));
|
return Ok(String::from(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
json.push_str(&format!(r#" "total_avg": {:.1}}}"#, (current / total) * 100_f64));
|
Ok(serde_json::to_string(&(Data { batteries, total_avg: (current / total) * 100_f64 })).unwrap())
|
||||||
Ok(json)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
|
Loading…
Add table
Reference in a new issue