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)]
#[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<str>,
}
#[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))),

View file

@ -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?;
}
}

View file

@ -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<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 {
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?;