From 869c754d215f5726ff00dfdcc93894fd6d2a1e41 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 10 Oct 2023 18:30:02 +0000 Subject: [PATCH] fallback to charge_full and charge_now --- src/config.rs | 4 ++-- src/tiles/battery.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7b13000..4cda069 100644 --- a/src/config.rs +++ b/src/config.rs @@ -128,9 +128,9 @@ pub async fn read_config() -> Result> { Ok(toml::from_slice(&config_contents)?) } -pub fn process_tile<'a>( +pub fn process_tile( tile: &TileConfig, - connection: &'a Arc, + connection: &Arc, ) -> BoxStream<'static, TileResult> { let five_secs = Duration::from_secs(5); match &tile.config_type { diff --git a/src/tiles/battery.rs b/src/tiles/battery.rs index 7d44bcd..ba3f0cf 100644 --- a/src/tiles/battery.rs +++ b/src/tiles/battery.rs @@ -4,16 +4,24 @@ use futures::future::try_join3; use futures_async_stream::try_stream; use std::error::Error; use std::path::Path; -use tokio::fs::File; +use tokio::fs::{try_exists, File}; use tokio::io::AsyncReadExt; #[try_stream(ok = Block, error = Box)] pub async fn battery_stream(config: BatteryConfig) { - let base_path = Path::new("/sys/class/power_supply").join(&*config.battery); + let base_dir = Path::new("/sys/class/power_supply").join(&*config.battery); + let file_prefix = if try_exists(base_dir.join("energy_now")).await? { + "energy_" + } else { + "charge_" + }; + let now_path = base_dir.join(file_prefix.to_owned() + "now"); + let full_path = base_dir.join(file_prefix.to_owned() + "full"); + loop { let charge_now = async { let mut raw = String::new(); - File::open(base_path.join("energy_now")) + File::open(&now_path) .await? .read_to_string(&mut raw) .await?; @@ -22,7 +30,7 @@ pub async fn battery_stream(config: BatteryConfig) { }; let charge_total = async { let mut raw = String::new(); - File::open(base_path.join("energy_full")) + File::open(&full_path) .await? .read_to_string(&mut raw) .await?; @@ -31,7 +39,7 @@ pub async fn battery_stream(config: BatteryConfig) { }; let status = async { let mut raw = String::new(); - File::open(base_path.join("status")) + File::open(base_dir.join("status")) .await? .read_to_string(&mut raw) .await?;