diff --git a/src/config.rs b/src/config.rs index ec9912b..c4ccfe9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,7 +9,7 @@ use std::path::{Path, PathBuf}; pub const STOP_FILE_NAME: &str = "stop-experiment"; pub const CONFIG_FILE_NAME: &str = "exp278.toml"; -pub const HOME_FOLDER_EXPERIMENT: &str = "/home/exp278"; // also where IMS-100 images are placed +pub const HOME_FOLDER_EXPERIMENT: &str = "/home/exp278"; // also where IMS-100 images are placed pub const TO_GROUND_FOLDER_EXPERIMENT: &str = "/home/exp278/toGround"; pub const TO_GROUND_LP_FOLDER_EXPERIMENT: &str = "/home/exp278/toGroundLP"; pub const LOG_FOLDER: &str = "logs"; @@ -296,7 +296,12 @@ pub mod tasks { pub fn create_low_priority_ground_dir() { log::debug!("Creating low priority to ground directory"); - if !Path::new(TO_GROUND_LP_FOLDER_EXPERIMENT).exists() && std::fs::create_dir_all(TO_GROUND_LP_FOLDER_EXPERIMENT).is_err() { - log::error!("Failed to create low priority to ground directory '{}'", TO_GROUND_LP_FOLDER_EXPERIMENT); + if !Path::new(TO_GROUND_LP_FOLDER_EXPERIMENT).exists() + && std::fs::create_dir_all(TO_GROUND_LP_FOLDER_EXPERIMENT).is_err() + { + log::error!( + "Failed to create low priority to ground directory '{}'", + TO_GROUND_LP_FOLDER_EXPERIMENT + ); } } diff --git a/src/controller.rs b/src/controller.rs index 1ff00bc..67f4cf9 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,4 +1,10 @@ +use crate::logger::LOGFILE_PATH; use num_enum::TryFromPrimitive; +use ops_sat_rs::config::{ + action_err::INVALID_ACTION_ID, HOME_FOLDER_EXPERIMENT, HOME_PATH, STOP_FILE_NAME, + TO_GROUND_FOLDER_EXPERIMENT, +}; +use satrs::action::ActionRequestVariant; use satrs::{ action::ActionRequest, pus::action::{ActionReplyPus, ActionReplyVariant}, @@ -9,8 +15,6 @@ use std::{ path::{Path, PathBuf}, sync::{atomic::AtomicBool, mpsc, Arc}, }; -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; @@ -103,9 +107,14 @@ 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() { - if std::fs::copy(logfile_path.as_str(), TO_GROUND_FOLDER_EXPERIMENT).is_err() { - log::error!("Copying logfile into downlink path failed") + if let Ok(logfile_path) = ::clone(logfile_path) + .into_os_string() + .into_string() + { + if std::fs::copy(logfile_path.as_str(), TO_GROUND_FOLDER_EXPERIMENT) + .is_err() + { + log::error!("Copying logfile into downlink path failed") } } } else { @@ -115,8 +124,17 @@ impl ExperimentController { // 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 let Ok(image_path) = match action_req.variant { + ActionRequestVariant::VecData(data) => { + let index = data[0]; + get_latest_image(index as usize) + } + _ => get_latest_image(0), + } { + 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") } @@ -151,33 +169,34 @@ impl ExperimentController { } } -// TODO this may very well cause everything to crash -pub fn get_latest_image() -> Result { +// TODO no idea if this works in any way shape or form +pub fn get_latest_image(index: usize) -> Result { // Get the most recently modified file - if let Some(last_modified_file) = std::fs::read_dir(HOME_FOLDER_EXPERIMENT)? + let mut png_files = std::fs::read_dir(HOME_FOLDER_EXPERIMENT)? .flatten() .filter(|f| match f.metadata() { - Ok(metadata) => {metadata.is_file()} - Err(_) => {false} + Ok(metadata) => metadata.is_file(), + Err(_) => false, }) - .filter(|f| match f.file_name().into_string(){ - Ok(name) => {name.ends_with(".png")} - 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(_) => { + .collect::>(); + png_files.sort_by_key(|x| match x.metadata() { + Ok(metadata) => { + if let Ok(time) = metadata.modified() { + time + } else { std::time::SystemTime::UNIX_EPOCH } - }) { - Ok(last_modified_file.path()) - } else { - Err(std::io::Error::other("No latest image found")) + } + Err(_) => std::time::SystemTime::UNIX_EPOCH, + }); + + png_files.reverse(); + if let Some(png) = png_files.into_iter().nth(index) { + return Ok(png.path()); } -} \ No newline at end of file + Err(std::io::Error::other("No latest image found")) +} diff --git a/src/logger.rs b/src/logger.rs index fe5914b..f502ac1 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,6 +1,6 @@ -use std::path::{Path, PathBuf}; use once_cell::sync::OnceCell; use ops_sat_rs::config::LOG_FOLDER; +use std::path::{Path, PathBuf}; pub static LOGFILE_PATH: OnceCell = OnceCell::new(); @@ -9,9 +9,17 @@ pub fn setup_logger() -> Result<(), fern::InitError> { eprintln!("Failed to create log folder '{}'", LOG_FOLDER); } let mut path_buf = PathBuf::from(LOG_FOLDER); - path_buf.push(format!("output_{}.log", humantime::format_rfc3339_seconds(std::time::SystemTime::now()).to_string()).replace(":", "_")); + path_buf.push( + format!( + "output_{}.log", + humantime::format_rfc3339_seconds(std::time::SystemTime::now()).to_string() + ) + .replace(":", "_"), + ); println!("Creating logfile {:?}", path_buf); - LOGFILE_PATH.set(path_buf.clone()).expect("Error setting global logfile path"); + LOGFILE_PATH + .set(path_buf.clone()) + .expect("Error setting global logfile path"); fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( diff --git a/src/main.rs b/src/main.rs index 0308b76..f3218ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,14 @@ use std::{ }; use log::{debug, info}; -use ops_sat_rs::config::{cfg_file::create_app_config, components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER}, create_low_priority_ground_dir, pool::create_sched_tc_pool, tasks::{FREQ_MS_CAMERA_HANDLING, FREQ_MS_CTRL, FREQ_MS_PUS_STACK, STOP_CHECK_FREQUENCY}, VALID_PACKET_ID_LIST, VERSION}; +use ops_sat_rs::config::{ + cfg_file::create_app_config, + components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER}, + create_low_priority_ground_dir, + pool::create_sched_tc_pool, + tasks::{FREQ_MS_CAMERA_HANDLING, FREQ_MS_CTRL, FREQ_MS_PUS_STACK, STOP_CHECK_FREQUENCY}, + VALID_PACKET_ID_LIST, VERSION, +}; use ops_sat_rs::config::{components::CAMERA_HANDLER, tasks::FREQ_MS_EVENT_HANDLING}; use ops_sat_rs::config::{tasks::FREQ_MS_UDP_TMTC, OBSW_SERVER_ADDR, SERVER_PORT}; use ops_sat_rs::TimeStampHelper;