Send signals using the correct context for each

This commit is contained in:
Luke D. Jones
2022-11-06 12:48:19 +13:00
parent 58ff566d65
commit 688e3a7358
6 changed files with 29 additions and 21 deletions

View File

@@ -52,13 +52,15 @@ impl CtrlPower {
err
})
.ok();
Self::notify_charge_control_end_threshold(&ctxt, limit).await?;
Self::notify_charge_control_end_threshold(&ctxt, limit)
.await
.ok();
Ok(())
}
fn charge_control_end_threshold(&self) -> u8 {
loop {
if let Some(config) = self.config.try_lock() {
if let Some(mut config) = self.config.try_lock() {
let limit = self
.power
.get_charge_control_end_threshold()
@@ -67,11 +69,10 @@ impl CtrlPower {
err
})
.unwrap_or(100);
if let Some(mut config) = self.config.try_lock() {
config.read();
config.bat_charge_limit = limit;
config.write();
}
config.read();
config.bat_charge_limit = limit;
config.write();
return config.bat_charge_limit;
}
@@ -207,7 +208,6 @@ impl CtrlTask for CtrlPower {
.await?;
let ctrl = self.clone();
dbg!("CtrlPower");
tokio::spawn(async move {
let mut online = 10;
loop {

View File

@@ -216,7 +216,6 @@ impl CtrlTask for ProfileZbus {
lock.write_profile_curve_to_platform().unwrap();
lock.save_config();
}
Self::notify_profile(&signal_ctxt.clone(), lock.config.active_profile)
.await
.ok();

View File

@@ -10,6 +10,7 @@ use daemon::ctrl_anime::CtrlAnime;
use log::LevelFilter;
use log::{error, info, warn};
use tokio::time::sleep;
use zbus::SignalContext;
use daemon::ctrl_anime::{config::AnimeConfig, trait_impls::CtrlAnimeZbus};
use daemon::ctrl_aura::{config::AuraConfig, controller::CtrlKbdLed, trait_impls::CtrlKbdLedZbus};
@@ -78,7 +79,8 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
match CtrlPlatform::new(config.clone()) {
Ok(ctrl) => {
start_tasks(ctrl, &mut connection).await?;
let sig_ctx = CtrlPlatform::signal_context(&connection)?;
start_tasks(ctrl, &mut connection, sig_ctx).await?;
}
Err(err) => {
error!("CtrlPlatform: {}", err);
@@ -87,7 +89,8 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
match CtrlPower::new(config.clone()) {
Ok(ctrl) => {
start_tasks(ctrl, &mut connection).await?;
let sig_ctx = CtrlPower::signal_context(&connection)?;
start_tasks(ctrl, &mut connection, sig_ctx).await?;
}
Err(err) => {
error!("CtrlPower: {}", err);
@@ -99,7 +102,8 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
match CtrlPlatformProfile::new(profile_config) {
Ok(ctrl) => {
let zbus = ProfileZbus(Arc::new(Mutex::new(ctrl)));
start_tasks(zbus, &mut connection).await?;
let sig_ctx = ProfileZbus::signal_context(&connection)?;
start_tasks(zbus, &mut connection, sig_ctx).await?;
}
Err(err) => {
error!("Profile control: {}", err);
@@ -112,7 +116,8 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
match CtrlAnime::new(AnimeConfig::load()) {
Ok(ctrl) => {
let zbus = CtrlAnimeZbus(Arc::new(Mutex::new(ctrl)));
start_tasks(zbus, &mut connection).await?;
let sig_ctx = CtrlAnimeZbus::signal_context(&connection)?;
start_tasks(zbus, &mut connection, sig_ctx).await?;
}
Err(err) => {
info!("AniMe control: {}", err);
@@ -124,7 +129,8 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
match CtrlKbdLed::new(laptop, aura_config) {
Ok(ctrl) => {
let zbus = CtrlKbdLedZbus(Arc::new(Mutex::new(ctrl)));
start_tasks(zbus, &mut connection).await?;
let sig_ctx = CtrlKbdLedZbus::signal_context(&connection)?;
start_tasks(zbus, &mut connection, sig_ctx).await?;
}
Err(err) => {
error!("Keyboard control: {}", err);
@@ -140,7 +146,11 @@ async fn start_daemon() -> Result<(), Box<dyn Error>> {
}
}
async fn start_tasks<T>(mut zbus: T, connection: &mut Connection) -> Result<(), Box<dyn Error>>
async fn start_tasks<T>(
mut zbus: T,
connection: &mut Connection,
signal_ctx: SignalContext<'static>,
) -> Result<(), Box<dyn Error>>
where
T: ZbusRun + Reloadable + CtrlTask + Clone,
{
@@ -151,8 +161,6 @@ where
.unwrap_or_else(|err| warn!("Controller error: {}", err));
zbus.add_to_server(connection).await;
task.create_tasks(CtrlKbdLedZbus::signal_context(connection)?)
.await
.ok();
task.create_tasks(signal_ctx).await.ok();
Ok(())
}

View File

@@ -64,8 +64,9 @@ macro_rules! task_watch_item {
let mut buffer = [0; 32];
watch.event_stream(&mut buffer).unwrap().for_each(|_| async {
let value = ctrl.$name();
dbg!(&value);
concat_idents::concat_idents!(notif_fn = notify_, $name {
Self::notif_fn(&signal_ctxt, value).await.unwrap();
Self::notif_fn(&signal_ctxt, value).await.ok();
});
}).await;
});

View File

@@ -70,5 +70,5 @@ trait Profile {
/// NotifyProfile signal
#[dbus_proxy(signal)]
fn notify_profile(&self, profile: Profile) -> zbus::Result<Profile>;
async fn notify_profile(&self, profile: Profile) -> zbus::Result<Profile>;
}

View File

@@ -23,7 +23,7 @@ macro_rules! watch_attr {
path.push($attr_name);
if let Some(path) = path.to_str() {
let mut inotify = inotify::Inotify::init()?;
inotify.add_watch(path, inotify::WatchMask::MODIFY)
inotify.add_watch(path, inotify::WatchMask::CLOSE_WRITE)
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
PlatformError::AttrNotFound(format!("{}", $attr_name))