mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-01-22 17:33:19 +01:00
Merge branch 'fluke/async-tasks' into 'main'
Async tasks See merge request asus-linux/asusctl!106
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod zbus;
|
pub mod zbus;
|
||||||
|
|
||||||
use ::zbus::blocking::Connection;
|
use ::zbus::Connection;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use logind_zbus::manager::ManagerProxyBlocking;
|
use logind_zbus::manager::ManagerProxy;
|
||||||
use rog_anime::{
|
use rog_anime::{
|
||||||
error::AnimeError,
|
error::AnimeError,
|
||||||
usb::{
|
usb::{
|
||||||
@@ -15,6 +15,7 @@ use rog_anime::{
|
|||||||
};
|
};
|
||||||
use rog_supported::AnimeSupportedFunctions;
|
use rog_supported::AnimeSupportedFunctions;
|
||||||
use rusb::{Device, DeviceHandle};
|
use rusb::{Device, DeviceHandle};
|
||||||
|
use smol::{stream::StreamExt, Executor};
|
||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
error::Error,
|
error::Error,
|
||||||
@@ -298,33 +299,63 @@ impl CtrlAnime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CtrlAnimeTask<'a> {
|
pub struct CtrlAnimeTask {
|
||||||
_inner: Arc<Mutex<CtrlAnime>>,
|
_inner: Arc<Mutex<CtrlAnime>>,
|
||||||
_c: Connection,
|
|
||||||
manager: ManagerProxyBlocking<'a>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CtrlAnimeTask<'a> {
|
impl CtrlAnimeTask {
|
||||||
pub fn new(inner: Arc<Mutex<CtrlAnime>>) -> Self {
|
pub async fn new(inner: Arc<Mutex<CtrlAnime>>) -> CtrlAnimeTask {
|
||||||
let connection =
|
Self { _inner: inner }
|
||||||
Connection::system().expect("CtrlAnimeTask could not create dbus connection");
|
|
||||||
|
|
||||||
let manager = ManagerProxyBlocking::new(&connection)
|
|
||||||
.expect("CtrlAnimeTask could not create ManagerProxy");
|
|
||||||
|
|
||||||
Self {
|
|
||||||
_inner: inner,
|
|
||||||
_c: connection,
|
|
||||||
manager,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'a> crate::CtrlTask for CtrlAnimeTask<'a> {
|
impl crate::CtrlTask for CtrlAnimeTask {
|
||||||
async fn do_task(&self) -> Result<(), RogError> {
|
async fn create_task(&self, executor: &mut Executor) -> Result<(), RogError> {
|
||||||
self.manager.receive_prepare_for_shutdown()?.next();
|
let connection = Connection::system()
|
||||||
self.manager.receive_prepare_for_sleep()?.next();
|
.await
|
||||||
|
.expect("CtrlAnimeTask could not create dbus connection");
|
||||||
|
|
||||||
|
let manager = ManagerProxy::new(&connection)
|
||||||
|
.await
|
||||||
|
.expect("CtrlAnimeTask could not create ManagerProxy");
|
||||||
|
|
||||||
|
let x = self._inner.clone();
|
||||||
|
executor
|
||||||
|
.spawn(async move {
|
||||||
|
if let Ok(p) = manager.receive_prepare_for_sleep().await {
|
||||||
|
p.for_each(|_| {
|
||||||
|
if let Ok(_lock) = x.clone().try_lock() {
|
||||||
|
info!("AniMe received sleep event (this feature is not yet complete)");
|
||||||
|
// lock.config.system
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
let manager = ManagerProxy::new(&connection)
|
||||||
|
.await
|
||||||
|
.expect("CtrlAnimeTask could not create ManagerProxy");
|
||||||
|
|
||||||
|
let x = self._inner.clone();
|
||||||
|
executor
|
||||||
|
.spawn(async move {
|
||||||
|
if let Ok(p) = manager.receive_prepare_for_shutdown().await {
|
||||||
|
p.for_each(|_| {
|
||||||
|
if let Ok(_lock) = x.clone().try_lock() {
|
||||||
|
info!(
|
||||||
|
"AniMe received shutdown event (this feature is not yet complete)"
|
||||||
|
);
|
||||||
|
// lock.config.system
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use logind_zbus::manager::ManagerProxyBlocking;
|
use logind_zbus::manager::ManagerProxy;
|
||||||
use rog_aura::{
|
use rog_aura::{
|
||||||
usb::{
|
usb::{
|
||||||
LED_APPLY, LED_AWAKE_OFF_SLEEP_OFF, LED_AWAKE_OFF_SLEEP_ON, LED_AWAKE_ON_SLEEP_OFF,
|
LED_APPLY, LED_AWAKE_OFF_SLEEP_OFF, LED_AWAKE_OFF_SLEEP_ON, LED_AWAKE_ON_SLEEP_OFF,
|
||||||
@@ -17,12 +17,13 @@ use rog_aura::{
|
|||||||
AuraEffect, LedBrightness, LED_MSG_LEN,
|
AuraEffect, LedBrightness, LED_MSG_LEN,
|
||||||
};
|
};
|
||||||
use rog_supported::LedSupportedFunctions;
|
use rog_supported::LedSupportedFunctions;
|
||||||
|
use smol::{stream::StreamExt, Executor};
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use zbus::blocking::Connection;
|
use zbus::Connection;
|
||||||
|
|
||||||
use crate::GetSupported;
|
use crate::GetSupported;
|
||||||
|
|
||||||
@@ -55,50 +56,13 @@ pub struct CtrlKbdLed {
|
|||||||
pub config: AuraConfig,
|
pub config: AuraConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CtrlKbdLedTask<'a> {
|
pub struct CtrlKbdLedTask {
|
||||||
inner: Arc<Mutex<CtrlKbdLed>>,
|
inner: Arc<Mutex<CtrlKbdLed>>,
|
||||||
_c: Connection,
|
|
||||||
_manager: ManagerProxyBlocking<'a>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CtrlKbdLedTask<'a> {
|
impl CtrlKbdLedTask {
|
||||||
pub fn new(inner: Arc<Mutex<CtrlKbdLed>>) -> Self {
|
pub fn new(inner: Arc<Mutex<CtrlKbdLed>>) -> Self {
|
||||||
let connection =
|
Self { inner }
|
||||||
Connection::system().expect("CtrlKbdLedTask could not create dbus connection");
|
|
||||||
|
|
||||||
let manager = ManagerProxyBlocking::new(&connection)
|
|
||||||
.expect("CtrlKbdLedTask could not create ManagerProxy");
|
|
||||||
|
|
||||||
// let c1 = inner.clone();
|
|
||||||
// // Run this action when the system wakes up from sleep
|
|
||||||
// manager
|
|
||||||
// .connect_prepare_for_sleep(move |sleep| {
|
|
||||||
// if !sleep {
|
|
||||||
// let c1 = c1.clone();
|
|
||||||
// spawn(move || {
|
|
||||||
// // wait a fraction for things to wake up properly
|
|
||||||
// //std::thread::sleep(Duration::from_millis(100));
|
|
||||||
// loop {
|
|
||||||
// if let Ok(ref mut lock) = c1.try_lock() {
|
|
||||||
// lock.set_brightness(lock.config.brightness).ok();
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// Ok(())
|
|
||||||
// })
|
|
||||||
// .map_err(|err| {
|
|
||||||
// warn!("CtrlAnimeTask: new() {}", err);
|
|
||||||
// err
|
|
||||||
// })
|
|
||||||
// .ok();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
inner,
|
|
||||||
_c: connection,
|
|
||||||
_manager: manager,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_config(lock: &mut CtrlKbdLed) -> Result<(), RogError> {
|
fn update_config(lock: &mut CtrlKbdLed) -> Result<(), RogError> {
|
||||||
@@ -127,13 +91,38 @@ impl<'a> CtrlKbdLedTask<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'a> CtrlTask for CtrlKbdLedTask<'a> {
|
impl CtrlTask for CtrlKbdLedTask {
|
||||||
async fn do_task(&self) -> Result<(), RogError> {
|
async fn create_task(&self, executor: &mut Executor) -> Result<(), RogError> {
|
||||||
self._manager.receive_prepare_for_sleep()?.next();
|
let connection = Connection::system()
|
||||||
|
.await
|
||||||
|
.expect("CtrlKbdLedTask could not create dbus connection");
|
||||||
|
|
||||||
if let Ok(ref mut lock) = self.inner.try_lock() {
|
let manager = ManagerProxy::new(&connection)
|
||||||
return Self::update_config(lock);
|
.await
|
||||||
}
|
.expect("CtrlKbdLedTask could not create ManagerProxy");
|
||||||
|
|
||||||
|
let inner = self.inner.clone();
|
||||||
|
executor
|
||||||
|
.spawn(async move {
|
||||||
|
if let Ok(p) = manager.receive_prepare_for_sleep().await {
|
||||||
|
p.for_each(|_| {
|
||||||
|
if let Ok(_lock) = inner.clone().try_lock() {
|
||||||
|
info!("CtrlKbdLedTask received sleep event (this feature is not yet complete)");
|
||||||
|
// lock.config.system
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
let inner = self.inner.clone();
|
||||||
|
self.repeating_task(500, executor, move || {
|
||||||
|
if let Ok(ref mut lock) = inner.try_lock() {
|
||||||
|
Self::update_config(lock).unwrap();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use log::{info, warn};
|
|||||||
use rog_profiles::error::ProfileError;
|
use rog_profiles::error::ProfileError;
|
||||||
use rog_profiles::{FanCurveProfiles, Profile};
|
use rog_profiles::{FanCurveProfiles, Profile};
|
||||||
use rog_supported::PlatformProfileFunctions;
|
use rog_supported::PlatformProfileFunctions;
|
||||||
|
use smol::Executor;
|
||||||
|
|
||||||
use super::config::ProfileConfig;
|
use super::config::ProfileConfig;
|
||||||
|
|
||||||
@@ -138,15 +139,19 @@ impl CtrlProfileTask {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CtrlTask for CtrlProfileTask {
|
impl CtrlTask for CtrlProfileTask {
|
||||||
async fn do_task(&self) -> Result<(), RogError> {
|
async fn create_task(&self, executor: &mut Executor) -> Result<(), RogError> {
|
||||||
if let Ok(ref mut lock) = self.ctrl.try_lock() {
|
let ctrl = self.ctrl.clone();
|
||||||
let new_profile = Profile::get_active_profile().unwrap();
|
self.repeating_task(666, executor, move || {
|
||||||
if new_profile != lock.config.active_profile {
|
if let Ok(ref mut lock) = ctrl.try_lock() {
|
||||||
lock.config.active_profile = new_profile;
|
let new_profile = Profile::get_active_profile().unwrap();
|
||||||
lock.write_profile_curve_to_platform()?;
|
if new_profile != lock.config.active_profile {
|
||||||
lock.save_config();
|
lock.config.active_profile = new_profile;
|
||||||
|
lock.write_profile_curve_to_platform().unwrap();
|
||||||
|
lock.save_config();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use log::warn;
|
|||||||
use rog_profiles::fan_curve_set::CurveData;
|
use rog_profiles::fan_curve_set::CurveData;
|
||||||
use rog_profiles::fan_curve_set::FanCurveSet;
|
use rog_profiles::fan_curve_set::FanCurveSet;
|
||||||
use rog_profiles::Profile;
|
use rog_profiles::Profile;
|
||||||
|
use smol::Executor;
|
||||||
use zbus::Connection;
|
use zbus::Connection;
|
||||||
use zbus::SignalContext;
|
use zbus::SignalContext;
|
||||||
|
|
||||||
@@ -203,15 +204,19 @@ impl crate::ZbusAdd for ProfileZbus {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CtrlTask for ProfileZbus {
|
impl CtrlTask for ProfileZbus {
|
||||||
async fn do_task(&self) -> Result<(), RogError> {
|
async fn create_task(&self, executor: &mut Executor) -> Result<(), RogError> {
|
||||||
if let Ok(ref mut lock) = self.inner.try_lock() {
|
let inner = self.inner.clone();
|
||||||
let new_profile = Profile::get_active_profile().unwrap();
|
self.repeating_task(500, executor, move || {
|
||||||
if new_profile != lock.config.active_profile {
|
if let Ok(ref mut lock) = inner.try_lock() {
|
||||||
lock.config.active_profile = new_profile;
|
let new_profile = Profile::get_active_profile().unwrap();
|
||||||
lock.write_profile_curve_to_platform()?;
|
if new_profile != lock.config.active_profile {
|
||||||
lock.save_config();
|
lock.config.active_profile = new_profile;
|
||||||
|
lock.write_profile_curve_to_platform().unwrap();
|
||||||
|
lock.save_config();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ use std::env;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use ::zbus::Connection;
|
use ::zbus::Connection;
|
||||||
|
use daemon::ctrl_profiles::controller::CtrlProfileTask;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use smol::Executor;
|
use smol::Executor;
|
||||||
@@ -115,9 +115,13 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box<dyn Error>>
|
|||||||
.unwrap_or_else(|err| warn!("Profile control: {}", err));
|
.unwrap_or_else(|err| warn!("Profile control: {}", err));
|
||||||
|
|
||||||
let tmp = Arc::new(Mutex::new(ctrl));
|
let tmp = Arc::new(Mutex::new(ctrl));
|
||||||
ProfileZbus::new(tmp.clone())
|
let task = CtrlProfileTask::new(tmp.clone());
|
||||||
.add_to_server(&mut connection)
|
task.create_task(executor).await.ok();
|
||||||
.await;
|
|
||||||
|
let task = ProfileZbus::new(tmp.clone());
|
||||||
|
task.create_task(executor).await.ok();
|
||||||
|
|
||||||
|
task.add_to_server(&mut connection).await;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Profile control: {}", err);
|
error!("Profile control: {}", err);
|
||||||
@@ -139,12 +143,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box<dyn Error>>
|
|||||||
let zbus = CtrlAnimeZbus(inner.clone());
|
let zbus = CtrlAnimeZbus(inner.clone());
|
||||||
zbus.add_to_server(&mut connection).await;
|
zbus.add_to_server(&mut connection).await;
|
||||||
|
|
||||||
let task = CtrlAnimeTask::new(inner);
|
let task = CtrlAnimeTask::new(inner).await;
|
||||||
executor
|
task.create_task(executor).await.ok();
|
||||||
.spawn(async move {
|
|
||||||
task.do_task().await.ok();
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("AniMe control: {}", err);
|
error!("AniMe control: {}", err);
|
||||||
@@ -167,11 +167,7 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box<dyn Error>>
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let task = CtrlKbdLedTask::new(inner);
|
let task = CtrlKbdLedTask::new(inner);
|
||||||
executor
|
task.create_task(executor).await.ok();
|
||||||
.spawn(async move {
|
|
||||||
task.do_task().await.ok();
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Keyboard control: {}", err);
|
error!("Keyboard control: {}", err);
|
||||||
@@ -180,10 +176,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box<dyn Error>>
|
|||||||
|
|
||||||
// Request dbus name after finishing initalizing all functions
|
// Request dbus name after finishing initalizing all functions
|
||||||
connection.request_name(DBUS_NAME).await?;
|
connection.request_name(DBUS_NAME).await?;
|
||||||
|
dbg!();
|
||||||
// Loop to check errors and iterate zbus server
|
|
||||||
loop {
|
loop {
|
||||||
// Nothing to do here really
|
smol::block_on(executor.tick());
|
||||||
std::thread::sleep(Duration::from_millis(1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,9 +29,12 @@ pub mod ctrl_supported;
|
|||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::error::RogError;
|
use crate::error::RogError;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
use smol::{stream::StreamExt, Executor, Timer};
|
||||||
use zbus::Connection;
|
use zbus::Connection;
|
||||||
|
|
||||||
pub static VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
@@ -45,9 +48,25 @@ pub trait ZbusAdd {
|
|||||||
async fn add_to_server(self, server: &mut Connection);
|
async fn add_to_server(self, server: &mut Connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set up a task to run on the async executor
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CtrlTask {
|
pub trait CtrlTask {
|
||||||
async fn do_task(&self) -> Result<(), RogError>;
|
async fn create_task(&self, executor: &mut Executor) -> Result<(), RogError>;
|
||||||
|
|
||||||
|
/// Create a timed repeating task
|
||||||
|
async fn repeating_task(
|
||||||
|
&self,
|
||||||
|
millis: u64,
|
||||||
|
executor: &mut Executor,
|
||||||
|
mut task: impl FnMut() + Send + 'static,
|
||||||
|
) {
|
||||||
|
let timer = Timer::interval(Duration::from_millis(millis));
|
||||||
|
executor
|
||||||
|
.spawn(async move {
|
||||||
|
timer.for_each(|_| task()).await;
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CtrlTaskComplex {
|
pub trait CtrlTaskComplex {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ The task trait:
|
|||||||
pub struct CtrlAnimeTask(Arc<Mutex<CtrlAnime>>);
|
pub struct CtrlAnimeTask(Arc<Mutex<CtrlAnime>>);
|
||||||
|
|
||||||
impl crate::CtrlTask for CtrlAnimeTask {
|
impl crate::CtrlTask for CtrlAnimeTask {
|
||||||
fn do_task(&self) -> Result<(), RogError> {
|
fn create_task(&self, executor: &mut Executor) -> Result<(), RogError> {
|
||||||
if let Ok(lock) = self.inner.try_lock() {
|
if let Ok(lock) = self.inner.try_lock() {
|
||||||
<some action>
|
<some action>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user