Fix battery detection

This commit is contained in:
Moritz Ruth 2023-03-09 23:35:49 +01:00
parent 9143d30de3
commit a6e721009e
Signed by: moritzruth
GPG key ID: C9BBAB79405EE56D
4 changed files with 47 additions and 31 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target/ /target/
.idea/

44
Cargo.lock generated
View file

@ -677,20 +677,20 @@ dependencies = [
"image", "image",
"json", "json",
"lazy_static", "lazy_static",
"log", "log",
"notify-rust", "notify-rust",
"rand", "rand",
"regex", "regex",
"rumqttc", "rumqttc",
"serde", "serde",
"serde_json", "serde_json",
"sysinfo", "sysinfo",
"tokio", "tokio",
"toml", "toml",
"users", "users",
"validator", "validator",
"void", "void",
"zbus", "zbus",
] ]
[[package]] [[package]]
@ -1978,9 +1978,9 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"
dependencies = [ dependencies = [
"form_urlencoded", "form_urlencoded",
"idna 0.3.0", "idna 0.3.0",
"percent-encoding", "percent-encoding",
] ]
[[package]] [[package]]
@ -1989,8 +1989,8 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
] ]
[[package]] [[package]]
@ -1999,9 +1999,9 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32ad5bf234c7d3ad1042e5252b7eddb2c4669ee23f32c7dd0e9b7705f07ef591" checksum = "32ad5bf234c7d3ad1042e5252b7eddb2c4669ee23f32c7dd0e9b7705f07ef591"
dependencies = [ dependencies = [
"idna 0.2.3", "idna 0.2.3",
"lazy_static", "lazy_static",
"regex", "regex",
"serde", "serde",
"serde_derive", "serde_derive",
"serde_json", "serde_json",

View file

@ -142,4 +142,4 @@ When a notification is dismissed, `closed` is sent into the topic.
## License ## License
Hassliebe is licensed under the [Blue Oak Model License 1.0.0](/LICENSE.md). Hassliebe is licensed under the [Blue Oak Model License 1.0.0](./LICENSE.md).

View file

@ -1,10 +1,11 @@
use std::fs; use std::fs::ReadDir;
use std::time::Duration; use std::time::Duration;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use rumqttc::QoS; use rumqttc::QoS;
use serde::Deserialize; use serde::Deserialize;
use sysinfo::{CpuExt, System, SystemExt}; use sysinfo::{CpuExt, System, SystemExt};
use tokio::task::spawn_blocking;
use tokio::time::MissedTickBehavior; use tokio::time::MissedTickBehavior;
use validator::Validate; use validator::Validate;
@ -77,12 +78,26 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> {
} }
if config.battery != 0 { if config.battery != 0 {
let battery_dirs = fs::read_dir("/sys/class/power_supply")?.filter_map(|d| d.ok()).collect::<Vec<_>>(); let battery_dirs = spawn_blocking(|| {
std::fs::read_dir("/sys/class/power_supply")
// TODO: Filter battery_dirs by the existence of "capacity" .map(|dirs| {
dirs.filter_map(|d| d.ok())
.filter(|d| std::fs::read_to_string(d.path().join("capacity")).is_ok())
.collect::<Vec<_>>()
})
.unwrap_or_default()
})
.await
.unwrap();
if let Some(dir) = battery_dirs.first() { 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 path = dir.path();
let capacity_path = path.clone().join("capacity"); let capacity_path = path.clone().join("capacity");
let status_path = path.clone().join("status"); let status_path = path.clone().join("status");
@ -97,7 +112,7 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> {
}, },
context, context,
config.battery, config.battery,
move || Ok(fs::read_to_string(&capacity_path)?), move || Ok(std::fs::read_to_string(&capacity_path)?),
) )
.await?; .await?;
@ -111,7 +126,7 @@ pub async fn init(context: &mut InitializationContext) -> Result<()> {
}, },
context, context,
config.battery, config.battery,
move || Ok(fs::read_to_string(&status_path)?), move || Ok(std::fs::read_to_string(&status_path)?),
) )
.await?; .await?;
} else { } else {