some more fixes and improvements

This commit is contained in:
2024-04-10 17:03:56 +02:00
parent 5cc561cbad
commit d0835f9393
5 changed files with 62 additions and 15 deletions
+41 -11
View File
@@ -1,11 +1,14 @@
use derive_new::new;
use num_enum::TryFromPrimitive;
use satrs::{
action::ActionRequest,
pus::action::{ActionReplyVariant, PusActionReply},
request::{GenericMessage, MessageMetadata},
};
use std::sync::{atomic::AtomicBool, mpsc, Arc};
use std::{
env::temp_dir,
path::{Path, PathBuf},
sync::{atomic::AtomicBool, mpsc, Arc},
};
use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, STOP_FILE_NAME};
@@ -17,11 +20,34 @@ pub enum ActionId {
StopExperiment = 1,
}
#[derive(new)]
pub struct ExperimentController {
pub composite_request_rx: mpsc::Receiver<GenericMessage<CompositeRequest>>,
pub action_reply_tx: mpsc::Sender<GenericMessage<PusActionReply>>,
pub stop_signal: Arc<AtomicBool>,
home_path_stop_file: PathBuf,
tmp_path_stop_file: PathBuf,
}
impl ExperimentController {
pub fn new(
home_dir: &Path,
composite_request_rx: mpsc::Receiver<GenericMessage<CompositeRequest>>,
action_reply_tx: mpsc::Sender<GenericMessage<PusActionReply>>,
stop_signal: Arc<AtomicBool>,
) -> Self {
let mut home_path_stop_file = PathBuf::new();
home_path_stop_file.push(home_dir);
home_path_stop_file.push(STOP_FILE_NAME);
let mut tmp_path_stop_file = temp_dir();
tmp_path_stop_file.push(STOP_FILE_NAME);
Self {
composite_request_rx,
action_reply_tx,
stop_signal,
home_path_stop_file,
tmp_path_stop_file,
}
}
}
impl ExperimentController {
@@ -78,13 +104,17 @@ impl ExperimentController {
}
pub fn check_stop_file(&self) {
if std::path::Path::new(STOP_FILE_NAME).exists() {
log::warn!(
"Detected stop file name at {}. Initiating experiment shutdown",
STOP_FILE_NAME
);
self.stop_signal
.store(true, std::sync::atomic::Ordering::Relaxed);
}
let check_at_path = |path: &Path| {
if path.exists() {
log::warn!(
"Detected stop file name at {}. Initiating experiment shutdown",
STOP_FILE_NAME
);
self.stop_signal
.store(true, std::sync::atomic::Ordering::Relaxed);
}
};
check_at_path(self.tmp_path_stop_file.as_path());
check_at_path(self.home_path_stop_file.as_path());
}
}