added get latest image function

This commit is contained in:
lkoester 2024-04-25 16:31:05 +02:00
parent df556acbf5
commit 2566050b3b
4 changed files with 75 additions and 36 deletions

View File

@ -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
);
}
}

View File

@ -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) = <PathBuf as Clone>::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) = <PathBuf as Clone>::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) = <PathBuf as Clone>::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) = <PathBuf as Clone>::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<PathBuf, std::io::Error> {
// TODO no idea if this works in any way shape or form
pub fn get_latest_image(index: usize) -> Result<PathBuf, std::io::Error> {
// 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::<Vec<std::fs::DirEntry>>();
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());
}
}
Err(std::io::Error::other("No latest image found"))
}

View File

@ -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<PathBuf> = 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!(

View File

@ -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;