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

3
.gitignore vendored
View file

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

44
Cargo.lock generated
View file

@ -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",

View file

@ -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).
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 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::<Vec<_>>();
// 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::<Vec<_>>()
})
.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 {