added get latest image function
This commit is contained in:
+48
-29
@@ -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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user