Add support for batteries not named BAT0

This commit is contained in:
Artemis Tosini 2021-08-27 18:37:40 +00:00
parent 3f69c3d2eb
commit 892b43c83f
Signed by: artemist
GPG key ID: ADFFE553DCBB831E
3 changed files with 34 additions and 16 deletions

View file

@ -54,7 +54,7 @@ pub struct TileConfig {
#[derive(Deserialize, Clone, Debug)] #[derive(Deserialize, Clone, Debug)]
#[serde(tag = "type", rename_all = "snake_case")] #[serde(tag = "type", rename_all = "snake_case")]
pub enum TileConfigType { pub enum TileConfigType {
Battery, Battery(BatteryConfig),
Memory, Memory,
Load, Load,
Hostname, Hostname,
@ -62,6 +62,13 @@ pub enum TileConfigType {
Iwd(IwdConfig), Iwd(IwdConfig),
} }
#[derive(SmartDefault, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct BatteryConfig {
#[default("BAT0")]
pub battery: Box<str>,
}
#[derive(SmartDefault, Deserialize, Clone, Debug)] #[derive(SmartDefault, Deserialize, Clone, Debug)]
#[serde(default)] #[serde(default)]
pub struct IwdConfig { pub struct IwdConfig {
@ -127,7 +134,10 @@ pub fn process_tile<'a>(
) -> BoxStream<'static, TileResult> { ) -> BoxStream<'static, TileResult> {
let five_secs = Duration::from_secs(5); let five_secs = Duration::from_secs(5);
match &tile.config_type { 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::Hostname => wrap(tiles::hostname_stream(connection.as_ref()), tile.update),
TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))), TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))), TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))),

View file

@ -18,16 +18,21 @@ where
let mut blocks = Vec::new(); let mut blocks = Vec::new();
blocks.resize_with(num_tiles, Default::default); blocks.resize_with(num_tiles, Default::default);
loop { loop {
let message = receiver.next().await.unwrap(); match receiver.next().await.unwrap() {
let message = message.unwrap(); Ok(message) => {
if message.sender_id < num_tiles { if message.sender_id < num_tiles {
blocks[message.sender_id] = Some(message.block); blocks[message.sender_id] = Some(message.block);
} else { } else {
eprintln!("Invalid message with sender id {}", message.sender_id); eprintln!("Invalid message with sender id {}", message.sender_id);
continue; 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?;
} }
} }

View file

@ -1,16 +1,19 @@
use crate::config::BatteryConfig;
use crate::tile::Block; use crate::tile::Block;
use futures::future::try_join3; use futures::future::try_join3;
use futures_async_stream::try_stream; use futures_async_stream::try_stream;
use std::error::Error; use std::error::Error;
use std::path::Path;
use tokio::fs::File; use tokio::fs::File;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
#[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)] #[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)]
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 { loop {
let charge_now = async { let charge_now = async {
let mut raw = String::new(); let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/charge_now") File::open(base_path.join("charge_now"))
.await? .await?
.read_to_string(&mut raw) .read_to_string(&mut raw)
.await?; .await?;
@ -19,7 +22,7 @@ pub async fn battery_stream() {
}; };
let charge_total = async { let charge_total = async {
let mut raw = String::new(); let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/charge_full") File::open(base_path.join("charge_full"))
.await? .await?
.read_to_string(&mut raw) .read_to_string(&mut raw)
.await?; .await?;
@ -28,7 +31,7 @@ pub async fn battery_stream() {
}; };
let status = async { let status = async {
let mut raw = String::new(); let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/status") File::open(base_path.join("status"))
.await? .await?
.read_to_string(&mut raw) .read_to_string(&mut raw)
.await?; .await?;