@@ -201,6 +201,26 @@ pub async fn list_devices(when: When) -> Vec<String> {
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn wait_for_idle() {
|
||||
loop {
|
||||
let mut waited = false;
|
||||
let keys: Vec<String> = DEVICES.read().unwrap().keys().map(Into::into).collect();
|
||||
for key in keys {
|
||||
let Some(device) = DEVICES.read().unwrap().get(&key).cloned() else {
|
||||
continue;
|
||||
};
|
||||
if device.lock.try_lock().is_ok() {
|
||||
continue;
|
||||
}
|
||||
waited = true;
|
||||
_ = device.lock.lock().await;
|
||||
}
|
||||
if !waited {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dev_root() -> PathBuf {
|
||||
std::env::var("LXDEVICED_DEV_ROOT")
|
||||
.as_deref()
|
||||
|
||||
@@ -34,6 +34,7 @@ async fn handle_connection(mut connection: Connection) -> std::io::Result<()> {
|
||||
Request::Trigger(set, action) => handler(trigger(set, action)),
|
||||
Request::ReloadRules => handler(reload_rules()),
|
||||
Request::ListDevices(when) => handler(list_devices(when)),
|
||||
Request::WaitForIdle => handler(wait_for_idle()),
|
||||
};
|
||||
let reply = handler.await;
|
||||
connection.send(reply).await?;
|
||||
@@ -57,6 +58,11 @@ async fn list_devices(when: When) -> Result<Vec<String>, Error> {
|
||||
Ok(crate::device::list_devices(when).await)
|
||||
}
|
||||
|
||||
async fn wait_for_idle() -> Result<(), Error> {
|
||||
crate::device::wait_for_idle().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handler<R: Serialize>(
|
||||
fut: impl Future<Output = Result<R, Error>> + Send + 'static,
|
||||
) -> Pin<Box<dyn Future<Output = Result<serde_json::Value, Error>> + Send>> {
|
||||
|
||||
Reference in New Issue
Block a user