From 892b43c83f141805370e9e254fdab1a4ae0aa81a Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Fri, 27 Aug 2021 18:37:40 +0000 Subject: [PATCH] Add support for batteries not named BAT0 --- src/config.rs | 14 ++++++++++++-- src/output.rs | 25 +++++++++++++++---------- src/tiles/battery.rs | 11 +++++++---- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/config.rs b/src/config.rs index 44576f4..7b13000 100644 --- a/src/config.rs +++ b/src/config.rs @@ -54,7 +54,7 @@ pub struct TileConfig { #[derive(Deserialize, Clone, Debug)] #[serde(tag = "type", rename_all = "snake_case")] pub enum TileConfigType { - Battery, + Battery(BatteryConfig), Memory, Load, Hostname, @@ -62,6 +62,13 @@ pub enum TileConfigType { Iwd(IwdConfig), } +#[derive(SmartDefault, Deserialize, Clone, Debug)] +#[serde(default)] +pub struct BatteryConfig { + #[default("BAT0")] + pub battery: Box, +} + #[derive(SmartDefault, Deserialize, Clone, Debug)] #[serde(default)] pub struct IwdConfig { @@ -127,7 +134,10 @@ pub fn process_tile<'a>( ) -> BoxStream<'static, TileResult> { let five_secs = Duration::from_secs(5); match &tile.config_type { - TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))), + TileConfigType::Battery(c) => wrap( + tiles::battery_stream(c.clone()), + tile.update.or(Some(five_secs)), + ), TileConfigType::Hostname => wrap(tiles::hostname_stream(connection.as_ref()), tile.update), TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))), TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))), diff --git a/src/output.rs b/src/output.rs index 260fa8b..1657ee6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -18,16 +18,21 @@ where let mut blocks = Vec::new(); blocks.resize_with(num_tiles, Default::default); loop { - let message = receiver.next().await.unwrap(); - let message = message.unwrap(); - if message.sender_id < num_tiles { - blocks[message.sender_id] = Some(message.block); - } else { - eprintln!("Invalid message with sender id {}", message.sender_id); - continue; + match receiver.next().await.unwrap() { + Ok(message) => { + if message.sender_id < num_tiles { + blocks[message.sender_id] = Some(message.block); + } else { + eprintln!("Invalid message with sender id {}", message.sender_id); + continue; + } + let mut serialized = serde_json::to_vec(&blocks).unwrap(); + serialized.extend_from_slice(b",\n"); + stdout.write_all(&serialized).await?; + } + Err(err) => { + eprintln!("Error in tile: {:?}", err); + } } - let mut serialized = serde_json::to_vec(&blocks).unwrap(); - serialized.extend_from_slice(b",\n"); - stdout.write_all(&serialized).await?; } } diff --git a/src/tiles/battery.rs b/src/tiles/battery.rs index 50168d2..c0e9e0e 100644 --- a/src/tiles/battery.rs +++ b/src/tiles/battery.rs @@ -1,16 +1,19 @@ +use crate::config::BatteryConfig; use crate::tile::Block; 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::io::AsyncReadExt; #[try_stream(ok = Block, error = Box)] -pub async fn battery_stream() { +pub async fn battery_stream(config: BatteryConfig) { + let base_path = Path::new("/sys/class/power_supply").join(&*config.battery); loop { let charge_now = async { let mut raw = String::new(); - File::open("/sys/class/power_supply/BAT0/charge_now") + File::open(base_path.join("charge_now")) .await? .read_to_string(&mut raw) .await?; @@ -19,7 +22,7 @@ pub async fn battery_stream() { }; let charge_total = async { let mut raw = String::new(); - File::open("/sys/class/power_supply/BAT0/charge_full") + File::open(base_path.join("charge_full")) .await? .read_to_string(&mut raw) .await?; @@ -28,7 +31,7 @@ pub async fn battery_stream() { }; let status = async { let mut raw = String::new(); - File::open("/sys/class/power_supply/BAT0/status") + File::open(base_path.join("status")) .await? .read_to_string(&mut raw) .await?;