battery: refactor to do less in callback
This commit is contained in:
parent
fb76f49325
commit
4893221973
|
@ -17,7 +17,7 @@ use num_traits::FromPrimitive;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{convert::Infallible, time::Duration};
|
use std::{convert::Infallible, time::Duration};
|
||||||
use tokio::sync::watch;
|
use tokio::sync::mpsc;
|
||||||
use tokio::try_join;
|
use tokio::try_join;
|
||||||
|
|
||||||
#[derive(FromPrimitive, Debug, PartialEq, Eq)]
|
#[derive(FromPrimitive, Debug, PartialEq, Eq)]
|
||||||
|
@ -60,7 +60,7 @@ pub async fn battery(
|
||||||
dbus_conn.as_ref(),
|
dbus_conn.as_ref(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let init_info = {
|
let mut info = {
|
||||||
let (percentage, raw_state, time_to_empty, time_to_full) = try_join!(
|
let (percentage, raw_state, time_to_empty, time_to_full) = try_join!(
|
||||||
proxy.percentage(),
|
proxy.percentage(),
|
||||||
proxy.state(),
|
proxy.state(),
|
||||||
|
@ -75,9 +75,7 @@ pub async fn battery(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
eprintln!("{:#?}", init_info);
|
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
let (tx, mut rx) = watch::channel(init_info);
|
|
||||||
|
|
||||||
let _reciever = dbus_conn
|
let _reciever = dbus_conn
|
||||||
.add_match(PropertiesPropertiesChanged::match_rule(
|
.add_match(PropertiesPropertiesChanged::match_rule(
|
||||||
|
@ -85,40 +83,10 @@ pub async fn battery(
|
||||||
Some(path),
|
Some(path),
|
||||||
))
|
))
|
||||||
.await?
|
.await?
|
||||||
.cb(move |_, changed: PropertiesPropertiesChanged| {
|
.cb(move |_, changed: PropertiesPropertiesChanged| tx.send(changed).is_ok());
|
||||||
tx.send_modify(|info| {
|
|
||||||
let props = changed.changed_properties;
|
|
||||||
props
|
|
||||||
.get("Percentage")
|
|
||||||
.and_then(Variant::as_f64)
|
|
||||||
.map(|val| info.percentage = val);
|
|
||||||
|
|
||||||
props
|
|
||||||
.get("State")
|
|
||||||
.and_then(Variant::as_u64)
|
|
||||||
.and_then(DeviceState::from_u64)
|
|
||||||
.map(|val| info.state = val);
|
|
||||||
|
|
||||||
props
|
|
||||||
.get("TimeToEmpty")
|
|
||||||
.and_then(Variant::as_i64)
|
|
||||||
.map(|val| val.clamp(0, i64::MAX).try_into().unwrap())
|
|
||||||
.map(Duration::from_secs)
|
|
||||||
.map(|val| info.time_to_empty = val);
|
|
||||||
|
|
||||||
props
|
|
||||||
.get("TimeToFull")
|
|
||||||
.and_then(Variant::as_i64)
|
|
||||||
.map(|val| val.clamp(0, i64::MAX).try_into().unwrap())
|
|
||||||
.map(Duration::from_secs)
|
|
||||||
.map(|val| info.time_to_full = val);
|
|
||||||
});
|
|
||||||
true
|
|
||||||
});
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let block = {
|
let block = {
|
||||||
let info = rx.borrow();
|
|
||||||
let time_str = match info.state {
|
let time_str = match info.state {
|
||||||
DeviceState::Discharging => format_duration(info.time_to_empty),
|
DeviceState::Discharging => format_duration(info.time_to_empty),
|
||||||
DeviceState::Charging => format_duration(info.time_to_full),
|
DeviceState::Charging => format_duration(info.time_to_full),
|
||||||
|
@ -151,6 +119,32 @@ pub async fn battery(
|
||||||
};
|
};
|
||||||
output.send(block).await?;
|
output.send(block).await?;
|
||||||
|
|
||||||
rx.changed().await?;
|
let msg = rx.recv().await.ok_or_eyre("Battery stream closed")?;
|
||||||
|
let props = msg.changed_properties;
|
||||||
|
|
||||||
|
props
|
||||||
|
.get("Percentage")
|
||||||
|
.and_then(Variant::as_f64)
|
||||||
|
.map(|val| info.percentage = val);
|
||||||
|
|
||||||
|
props
|
||||||
|
.get("State")
|
||||||
|
.and_then(Variant::as_u64)
|
||||||
|
.and_then(DeviceState::from_u64)
|
||||||
|
.map(|val| info.state = val);
|
||||||
|
|
||||||
|
props
|
||||||
|
.get("TimeToEmpty")
|
||||||
|
.and_then(Variant::as_i64)
|
||||||
|
.map(|val| val.clamp(0, i64::MAX).try_into().unwrap())
|
||||||
|
.map(Duration::from_secs)
|
||||||
|
.map(|val| info.time_to_empty = val);
|
||||||
|
|
||||||
|
props
|
||||||
|
.get("TimeToFull")
|
||||||
|
.and_then(Variant::as_i64)
|
||||||
|
.map(|val| val.clamp(0, i64::MAX).try_into().unwrap())
|
||||||
|
.map(Duration::from_secs)
|
||||||
|
.map(|val| info.time_to_full = val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue