From df556acbf50e669903fe6936d29db4df95116e7b Mon Sep 17 00:00:00 2001 From: lkoester Date: Thu, 25 Apr 2024 16:12:22 +0200 Subject: [PATCH] added moving images into downlink lp folder --- src/controller.rs | 49 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index c1e5ae4..1ff00bc 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -9,8 +9,7 @@ use std::{ path::{Path, PathBuf}, sync::{atomic::AtomicBool, mpsc, Arc}, }; - -use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, HOME_PATH, STOP_FILE_NAME, TO_GROUND_FOLDER_EXPERIMENT}; +use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, HOME_FOLDER_EXPERIMENT, HOME_PATH, STOP_FILE_NAME, TO_GROUND_FOLDER_EXPERIMENT}; use crate::logger::LOGFILE_PATH; use crate::requests::CompositeRequest; @@ -20,6 +19,7 @@ use crate::requests::CompositeRequest; pub enum ActionId { StopExperiment = 1, DownlinkLogfile = 2, + DownlinkImages = 3, } pub struct ExperimentController { @@ -104,8 +104,7 @@ impl ExperimentController { ActionId::DownlinkLogfile => { if let Some(logfile_path) = LOGFILE_PATH.get() { if let Ok(logfile_path) = ::clone(logfile_path).into_os_string().into_string() { - let ground_folder_path = TO_GROUND_FOLDER_EXPERIMENT.to_string(); - if std::fs::copy(logfile_path.as_str(), ground_folder_path.as_str()).is_err() { + if std::fs::copy(logfile_path.as_str(), TO_GROUND_FOLDER_EXPERIMENT).is_err() { log::error!("Copying logfile into downlink path failed") } } @@ -113,6 +112,17 @@ impl ExperimentController { log::error!("Downlink path emtpy") } } + + // downlink images, default will be the last image, otherwise specified counting down (2 = second to last image, etc.) + ActionId::DownlinkImages => { + if let Ok(image_path) = get_latest_image() { + if let Ok(image_path) = ::clone(&image_path).into_os_string().into_string() { + if std::fs::copy(image_path, TO_GROUND_FOLDER_EXPERIMENT).is_err() { + log::error!("Copying logfile into downlink path failed") + } + } + } + } } } @@ -140,3 +150,34 @@ impl ExperimentController { check_at_path(self.home_path_stop_file.as_path()); } } + +// TODO this may very well cause everything to crash +pub fn get_latest_image() -> Result { + // Get the most recently modified file + if let Some(last_modified_file) = std::fs::read_dir(HOME_FOLDER_EXPERIMENT)? + .flatten() + .filter(|f| match f.metadata() { + Ok(metadata) => {metadata.is_file()} + Err(_) => {false} + }) + .filter(|f| match f.file_name().into_string(){ + Ok(name) => {name.ends_with(".png")} + Err(_) => {false} + }) + .max_by_key(|x| match x.metadata() { + Ok(metadata) => { + if let Ok(time) = metadata.modified() { + time + } else { + std::time::SystemTime::UNIX_EPOCH + } + } + Err(_) => { + std::time::SystemTime::UNIX_EPOCH + } + }) { + Ok(last_modified_file.path()) + } else { + Err(std::io::Error::other("No latest image found")) + } +} \ No newline at end of file