From a6e721009e217446621060630ff7596e39876bc9 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Thu, 9 Mar 2023 23:35:49 +0100 Subject: [PATCH] Fix battery detection --- .gitignore | 3 ++- Cargo.lock | 44 ++++++++++++++++++++++---------------------- README.md | 2 +- src/modules/info.rs | 29 ++++++++++++++++++++++------- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index a6f89c2..bf11a4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target/ \ No newline at end of file +/target/ +.idea/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 2ec2d75..4c1263d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -677,20 +677,20 @@ dependencies = [ "image", "json", "lazy_static", - "log", - "notify-rust", - "rand", - "regex", - "rumqttc", - "serde", - "serde_json", - "sysinfo", - "tokio", - "toml", - "users", - "validator", - "void", - "zbus", + "log", + "notify-rust", + "rand", + "regex", + "rumqttc", + "serde", + "serde_json", + "sysinfo", + "tokio", + "toml", + "users", + "validator", + "void", + "zbus", ] [[package]] @@ -1978,9 +1978,9 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" dependencies = [ - "form_urlencoded", - "idna 0.3.0", - "percent-encoding", + "form_urlencoded", + "idna 0.3.0", + "percent-encoding", ] [[package]] @@ -1989,8 +1989,8 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" dependencies = [ - "libc", - "log", + "libc", + "log", ] [[package]] @@ -1999,9 +1999,9 @@ version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32ad5bf234c7d3ad1042e5252b7eddb2c4669ee23f32c7dd0e9b7705f07ef591" dependencies = [ - "idna 0.2.3", - "lazy_static", - "regex", + "idna 0.2.3", + "lazy_static", + "regex", "serde", "serde_derive", "serde_json", diff --git a/README.md b/README.md index 74bac2b..46210d2 100644 --- a/README.md +++ b/README.md @@ -142,4 +142,4 @@ When a notification is dismissed, `closed` is sent into the topic. ## License -Hassliebe is licensed under the [Blue Oak Model License 1.0.0](/LICENSE.md). \ No newline at end of file +Hassliebe is licensed under the [Blue Oak Model License 1.0.0](./LICENSE.md). \ No newline at end of file diff --git a/src/modules/info.rs b/src/modules/info.rs index 33a912d..07cbf10 100644 --- a/src/modules/info.rs +++ b/src/modules/info.rs @@ -1,10 +1,11 @@ -use std::fs; +use std::fs::ReadDir; use std::time::Duration; use anyhow::{anyhow, Result}; use rumqttc::QoS; use serde::Deserialize; use sysinfo::{CpuExt, System, SystemExt}; +use tokio::task::spawn_blocking; use tokio::time::MissedTickBehavior; use validator::Validate; @@ -77,12 +78,26 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> { } if config.battery != 0 { - let battery_dirs = fs::read_dir("/sys/class/power_supply")?.filter_map(|d| d.ok()).collect::>(); - - // TODO: Filter battery_dirs by the existence of "capacity" + let battery_dirs = spawn_blocking(|| { + std::fs::read_dir("/sys/class/power_supply") + .map(|dirs| { + dirs.filter_map(|d| d.ok()) + .filter(|d| std::fs::read_to_string(d.path().join("capacity")).is_ok()) + .collect::>() + }) + .unwrap_or_default() + }) + .await + .unwrap(); if let Some(dir) = battery_dirs.first() { - log::debug!("Found {} batteries, using {}", battery_dirs.len(), dir.file_name().to_string_lossy()); + log::debug!( + "Found {} {}, using {}", + battery_dirs.len(), + if battery_dirs.len() == 1 { "battery" } else { "batteries" }, + dir.file_name().to_string_lossy() + ); + let path = dir.path(); let capacity_path = path.clone().join("capacity"); let status_path = path.clone().join("status"); @@ -97,7 +112,7 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> { }, context, config.battery, - move || Ok(fs::read_to_string(&capacity_path)?), + move || Ok(std::fs::read_to_string(&capacity_path)?), ) .await?; @@ -111,7 +126,7 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> { }, context, config.battery, - move || Ok(fs::read_to_string(&status_path)?), + move || Ok(std::fs::read_to_string(&status_path)?), ) .await?; } else {