From 18aaa689baa754cd82adde5bc6f0185387905f53 Mon Sep 17 00:00:00 2001 From: Ryan Butler Date: Tue, 10 Dec 2024 18:00:31 -0600 Subject: [PATCH] feat(thermal-cam-ctrl): add cleanup command (#311) --- Cargo.lock | 4 ++- Cargo.toml | 3 +- seek-camera/wrapper/src/lib.rs | 2 +- thermal-cam-ctrl/Cargo.toml | 17 ++++----- thermal-cam-ctrl/src/calib.rs | 9 ++--- thermal-cam-ctrl/src/capture.rs | 3 +- thermal-cam-ctrl/src/cleanup.rs | 61 +++++++++++++++++++++++++++++++++ thermal-cam-ctrl/src/main.rs | 12 +++++-- thermal-cam-ctrl/src/pairing.rs | 7 ++-- 9 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 thermal-cam-ctrl/src/cleanup.rs diff --git a/Cargo.lock b/Cargo.lock index 20b5c4f0..b298e355 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4673,7 +4673,7 @@ dependencies = [ [[package]] name = "orb-thermal-cam-ctrl" -version = "0.0.6" +version = "0.0.7" dependencies = [ "bytemuck", "clap", @@ -4681,9 +4681,11 @@ dependencies = [ "eyre", "indicatif", "orb-build-info 0.0.0", + "orb-telemetry", "owo-colors", "png", "seek-camera", + "tracing", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 652fdf8c..788490f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,12 +88,13 @@ can-rs.path = "can" orb-attest-dbus.path = "attest/dbus" orb-build-info.path = "build-info" orb-const-concat.path = "const-concat" +orb-header-parsing.path = "header-parsing" orb-security-utils.path = "security-utils" orb-slot-ctrl.path = "slot-ctrl" orb-telemetry.path = "telemetry" orb-update-agent-core.path = "update-agent/core" orb-zbus-proxies.path = "zbus-proxies" -orb-header-parsing.path = "header-parsing" +seek-camera.path = "seek-camera/wrapper" [workspace.dependencies.orb-messages] git = "https://github.com/worldcoin/orb-messages" diff --git a/seek-camera/wrapper/src/lib.rs b/seek-camera/wrapper/src/lib.rs index b7a737bf..6fec54eb 100644 --- a/seek-camera/wrapper/src/lib.rs +++ b/seek-camera/wrapper/src/lib.rs @@ -50,7 +50,7 @@ impl Display for SerialNumber { pub struct ChipId(sys::chipid_t); impl ChipId { - fn as_str(&self) -> &str { + pub fn as_str(&self) -> &str { // Some platforms have c_char as i8 instead of u8. let chars: &[core::ffi::c_char] = &self.0; let chars: &[u8] = unsafe { std::mem::transmute(chars) }; diff --git a/thermal-cam-ctrl/Cargo.toml b/thermal-cam-ctrl/Cargo.toml index d9175b38..7ab463ee 100644 --- a/thermal-cam-ctrl/Cargo.toml +++ b/thermal-cam-ctrl/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "orb-thermal-cam-ctrl" -version = "0.0.6" +version = "0.0.7" description = "CLI util for the thermal camera on the orb" authors = ["Ryan Butler "] publish = false @@ -12,18 +12,19 @@ rust-version.workspace = true [dependencies] bytemuck = { version = "1.13.1", features = ["derive"] } -clap = { version = "4.3", features = ["derive"] } -color-eyre = "0.6.2" -eyre = "0.6" +clap = { workspace = true, features = ["derive"] } +color-eyre.workspace = true +eyre.workspace = true indicatif = "0.17" -orb-build-info.path = "../build-info" +orb-build-info.workspace = true +orb-telemetry.workspace = true owo-colors = "3" png = "0.17" -seek-camera.path = "../seek-camera/wrapper" +seek-camera.workspace = true +tracing.workspace = true [build-dependencies] -orb-build-info = { path = "../build-info", features = ["build-script"] } -color-eyre = "0.6.2" +orb-build-info = { workspace = true, features = ["build-script"] } [package.metadata.orb] unsupported_targets = [ diff --git a/thermal-cam-ctrl/src/calib.rs b/thermal-cam-ctrl/src/calib.rs index dc192bd3..86dfd1be 100644 --- a/thermal-cam-ctrl/src/calib.rs +++ b/thermal-cam-ctrl/src/calib.rs @@ -10,6 +10,7 @@ use seek_camera::{ manager::{CameraHandle, Manager}, }; use std::{sync::OnceLock, time::Duration}; +use tracing::info; use crate::{start_manager, Flow}; @@ -73,7 +74,7 @@ fn delete_fsc(mngr: &mut Manager, cam_h: CameraHandle) -> Result { .set_position(pct as u64) }), )?; - println!("Completed deletion!"); + info!("Completed deletion!"); Ok(Flow::Finish) } @@ -93,13 +94,13 @@ fn new_fsc( } cam.capture_session_start(FrameFormat::Grayscale)?; - println!("Warming camera up for {} seconds.", warmup_time.as_secs()); + info!("Warming camera up for {} seconds.", warmup_time.as_secs()); std::thread::sleep(warmup_time); // This static is necessary because we don't support closures for the progress // callback. static BAR: OnceLock = OnceLock::new(); - println!("Beginning flat scene calibration."); + info!("Beginning flat scene calibration."); cam.store_flat_scene_correction( FlatSceneCorrectionId::_0, Some(|pct| { @@ -108,6 +109,6 @@ fn new_fsc( }), )?; cam.capture_session_stop()?; - println!("Completed calibration!"); + info!("Completed calibration!"); Ok(Flow::Finish) } diff --git a/thermal-cam-ctrl/src/capture.rs b/thermal-cam-ctrl/src/capture.rs index b9f90b86..623c35b4 100644 --- a/thermal-cam-ctrl/src/capture.rs +++ b/thermal-cam-ctrl/src/capture.rs @@ -17,6 +17,7 @@ use std::{ path::{Path, PathBuf}, sync::mpsc::SyncSender, }; +use tracing::warn; use crate::{start_manager, Flow}; @@ -57,7 +58,7 @@ impl Save { bail!("Please provide a valid directory that exists for `save_dir`"); } if self.save_dir.read_dir()?.next().is_some() { - eprintln!("{}", "Warning: `save_dir` is not empty".yellow()); + warn!("{}", "Warning: `save_dir` is not empty".yellow()); } start_manager(Box::new(move |mngr, cam_h, _evt, _err| { on_cam(mngr, cam_h, self.num_frames, &self.save_dir, self.no_fsc) diff --git a/thermal-cam-ctrl/src/cleanup.rs b/thermal-cam-ctrl/src/cleanup.rs new file mode 100644 index 00000000..d9b5f145 --- /dev/null +++ b/thermal-cam-ctrl/src/cleanup.rs @@ -0,0 +1,61 @@ +use clap::Parser; +use color_eyre::Result; +use eyre::{OptionExt as _, WrapErr as _}; +use seek_camera::{ + manager::{CameraHandle, Event, Manager}, + ChipId, ErrorCode, +}; + +use crate::{start_manager, Flow}; + +/// Manages pairing of the camera +#[derive(Debug, Parser)] +pub struct Cleanup {} + +impl Cleanup { + pub fn run(self) -> Result<()> { + start_manager(Box::new(on_cam)) + } +} + +fn on_cam( + mngr: &mut Manager, + cam_h: CameraHandle, + evt: Event, + _err_code: Option, +) -> Result { + match evt { + Event::Connect | Event::ReadyToPair => (), + _ => return Ok(Flow::Finish), + } + let cid = mngr + .cameras() + .wrap_err("failed to get cameras")? + .get_mut(&cam_h) + .ok_or_eyre("failed to access camera from handle")? + .chip_id() + .wrap_err("failed to get camera chip_id")?; + delete_other_cams(&cid)?; + Ok(Flow::Finish) +} + +fn delete_other_cams(cid_to_keep: &ChipId) -> Result<()> { + let calib_dir = crate::get_seek_dir().join("cal"); + for entry in calib_dir + .read_dir() + .wrap_err("failed to access SEEKTHERMAL_ROOT/.seekthermal/cal")? + { + let entry = entry?; + if cid_to_keep.as_str() == entry.file_name() { + continue; // skips matching dir + } + tracing::info!("removing {}", entry.path().display()); + std::fs::remove_dir_all(entry.path()).wrap_err_with(|| { + format!( + "failed to delete directory contents at {}", + entry.path().display() + ) + })? + } + Ok(()) +} diff --git a/thermal-cam-ctrl/src/main.rs b/thermal-cam-ctrl/src/main.rs index 63ee31e2..f11bbdc8 100644 --- a/thermal-cam-ctrl/src/main.rs +++ b/thermal-cam-ctrl/src/main.rs @@ -2,6 +2,7 @@ mod calib; mod capture; +mod cleanup; mod log; mod pairing; @@ -24,10 +25,12 @@ use seek_camera::{ manager::{CameraHandle, Event, Manager}, ErrorCode, }; +use tracing::warn; static SEEK_DIR: OnceLock = OnceLock::new(); const BUILD_INFO: BuildInfo = make_build_info!(); +const SYSLOG_IDENTIFIER: &str = "worldcoin-thermal-cam-ctrl"; fn make_clap_v3_styles() -> Styles { Styles::styled() @@ -50,6 +53,7 @@ enum Commands { Capture(crate::capture::Capture), Log(crate::log::Log), Pairing(crate::pairing::Pairing), + Cleanup(crate::cleanup::Cleanup), } fn get_seek_dir() -> &'static Path { @@ -106,6 +110,10 @@ enum Flow { fn main() -> Result<()> { color_eyre::install()?; + orb_telemetry::TelemetryConfig::new() + .with_journald(SYSLOG_IDENTIFIER) + .init(); + let args = Cli::parse(); if std::env::var("SEEKTHERMAL_ROOT").unwrap_or_default() == "" { return Err(eyre!("`SEEKTHERMAL_ROOT` env var must be explicitly set!")) @@ -119,18 +127,18 @@ fn main() -> Result<()> { #[cfg(unix)] const USER_ENV_VAR: &str = "USER"; if std::env::var(USER_ENV_VAR).unwrap_or_default() == "root" { - eprintln!( + warn!( "{}", "warning: running as root. This may mess up file permissions." .color(AnsiColors::Red) ); } - #[cfg(unix)] match args.commands { Commands::Calibration(c) => c.run(), Commands::Capture(c) => c.run(), Commands::Log(c) => c.run(), Commands::Pairing(c) => c.run(), + Commands::Cleanup(c) => c.run(), } } diff --git a/thermal-cam-ctrl/src/pairing.rs b/thermal-cam-ctrl/src/pairing.rs index 2c0a9abe..80d75d9f 100644 --- a/thermal-cam-ctrl/src/pairing.rs +++ b/thermal-cam-ctrl/src/pairing.rs @@ -12,6 +12,7 @@ use color_eyre::{ }; use indicatif::ProgressBar; use seek_camera::manager::{CameraHandle, Event, Manager}; +use tracing::info; use crate::{start_manager, Flow}; @@ -143,15 +144,15 @@ fn helper( } else { "unpaired".color(AnsiColors::Red) }; - println!("Found {paired} camera with cid: {cid}, serial: {serial}"); + info!("Found {paired} camera with cid: {cid}, serial: {serial}"); if pairing_behavior == PairingBehavior::ForcePair || pairing_behavior == PairingBehavior::Pair && !is_paired { - println!("Pairing camera (cid {cid})..."); + info!("Pairing camera (cid {cid})..."); cam.store_calibration_data(from_dir, Some(pair_progress_cb)) .wrap_err("Error while pairing camera")?; - println!("{} camera (cid {cid})", "Paired".green()); + info!("{} camera (cid {cid})", "Paired".green()); } if continue_running {