implement wait feature in ssh-cli
All checks were successful
Build Container / Build Container (push) Successful in 4m16s

This commit is contained in:
lilly 2026-07-19 20:47:27 +02:00
commit 7c64d57eb5
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
4 changed files with 82 additions and 45 deletions

View file

@ -13,13 +13,13 @@ mod api_client;
mod api_models;
mod cli;
use std::process::exit;
use std::{process::exit, time::Duration};
use eyre::eyre;
use crate::{
api_client::ApiClient,
api_models::{ApiLock, DesiredLockState},
api_models::{ApiLock, DesiredLockState, LockState},
};
#[tokio::main(flavor = "current_thread")]
@ -71,9 +71,10 @@ async fn main() -> eyre::Result<()> {
api.operate_lock(&lock.id, DesiredLockState::CLOSED).await?;
if wait_until_done {
tracing::warn!(
"wait feature is not yet implemented. lock should be turning now >~<"
);
wait_for_lock_state(&api, &lock_name, LockState::LOCKED).await?;
println!("Lock is now closed. Enjoy <3");
} else {
println!("Lock is now closing. Enjoy <3");
}
exit(0);
@ -89,9 +90,10 @@ async fn main() -> eyre::Result<()> {
api.operate_lock(&lock.id, DesiredLockState::OPEN).await?;
if wait_until_done {
tracing::warn!(
"wait feature is not yet implemented. lock should be turning now >~<"
);
wait_for_lock_state(&api, &lock_name, LockState::UNLOCKED).await?;
println!("Lock is now opened. Enjoy <3");
} else {
println!("Lock is now opening. Enjoy <3");
}
exit(0);
@ -111,9 +113,9 @@ async fn main() -> eyre::Result<()> {
if i_lock.status.is_error_jammed {
println!(" !!! Lock is jammed !!!");
}
println!(" State: {}", i_lock.status.lock_state);
println!(" Desired State: {}", i_lock.status.lock_target_level);
println!(" Acitivty: {}", i_lock.status.activity_state);
println!(" State: {:?}", i_lock.status.lock_state);
println!(" Desired State: {:?}", i_lock.status.lock_target_level);
println!(" Acitivty: {:?}", i_lock.status.activity_state);
println!();
}
@ -124,13 +126,37 @@ async fn main() -> eyre::Result<()> {
async fn find_lock(api: &ApiClient, name: &str) -> eyre::Result<ApiLock> {
let locks = api.fetch_locks().await?;
tracing::debug!("Comparing locks from API for one with name {name:?}");
tracing::trace!("Comparing locks from API for one with name {name:?}");
let lock = locks
.into_iter()
.find(|i_lock| i_lock.name.eq_ignore_ascii_case(&name))
.ok_or_else(|| {
eyre!("No lock named {name:?} exists. See \"list-locks\" for a list of valid locks.")
})?;
tracing::debug!(lock = ?lock,"Identified lock with name matching {name:?}");
tracing::trace!(lock = ?lock, "Identified lock with name matching {name:?}");
Ok(lock)
}
async fn wait_for_lock_state(
api: &ApiClient,
lock_name: &str,
desired_state: LockState,
) -> eyre::Result<()> {
tokio::time::sleep(Duration::from_millis(200)).await;
let lock = find_lock(&api, &lock_name).await?;
let mut interval = tokio::time::interval(Duration::from_millis(500));
let mut last_status = lock.status;
while last_status.lock_state != desired_state {
interval.tick().await;
let lock = find_lock(&api, &lock_name).await?;
if lock.status != last_status {
println!(
"Lock state changed to {:?} (activity={:?})",
lock.status.lock_state, lock.status.activity_state
);
last_status = lock.status;
}
}
Ok(())
}