ops-sat-rs/src/controller.rs

354 lines
13 KiB
Rust
Raw Normal View History

2024-04-25 16:31:05 +02:00
use crate::logger::LOGFILE_PATH;
2024-04-10 15:37:24 +02:00
use num_enum::TryFromPrimitive;
2024-04-25 16:31:05 +02:00
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;
2024-04-10 15:37:24 +02:00
use satrs::{
action::ActionRequest,
2024-04-25 01:20:54 +02:00
params::Params,
2024-04-15 12:16:01 +02:00
pus::action::{ActionReplyPus, ActionReplyVariant},
2024-04-10 15:37:24 +02:00
request::{GenericMessage, MessageMetadata},
2024-04-25 01:20:54 +02:00
res_code::ResultU16,
2024-04-10 15:37:24 +02:00
};
2024-04-25 16:45:00 +02:00
use serde::{Deserialize, Serialize};
2024-04-10 17:03:56 +02:00
use std::{
env::temp_dir,
path::{Path, PathBuf},
2024-04-25 01:20:54 +02:00
process::Command,
2024-04-10 17:03:56 +02:00
sync::{atomic::AtomicBool, mpsc, Arc},
};
2024-04-10 15:37:24 +02:00
use ops_sat_rs::config::ctrl_err::{
SHELL_CMD_EXECUTION_FAILURE, SHELL_CMD_INVALID_FORMAT, SHELL_CMD_IO_ERROR,
2024-04-25 01:20:54 +02:00
};
2024-04-10 15:37:24 +02:00
use crate::requests::CompositeRequest;
2024-04-25 16:45:00 +02:00
#[derive(Serialize, Deserialize, Debug)]
2024-04-25 17:11:52 +02:00
pub struct ShellCmd<'a> {
cmd: &'a str,
args: Vec<&'a str>,
2024-04-25 16:45:00 +02:00
}
2024-04-10 15:37:24 +02:00
#[derive(Debug, Clone, Copy, TryFromPrimitive)]
#[repr(u32)]
pub enum ActionId {
StopExperiment = 1,
DownlinkLogfile = 2,
DownlinkImages = 3,
ExecuteShellCommandBlocking = 4,
2024-04-10 15:37:24 +02:00
}
pub struct ExperimentController {
pub composite_request_rx: mpsc::Receiver<GenericMessage<CompositeRequest>>,
2024-04-15 12:16:01 +02:00
pub action_reply_tx: mpsc::Sender<GenericMessage<ActionReplyPus>>,
2024-04-10 15:37:24 +02:00
pub stop_signal: Arc<AtomicBool>,
2024-04-10 17:03:56 +02:00
home_path_stop_file: PathBuf,
tmp_path_stop_file: PathBuf,
}
impl ExperimentController {
pub fn new(
composite_request_rx: mpsc::Receiver<GenericMessage<CompositeRequest>>,
2024-04-15 12:16:01 +02:00
action_reply_tx: mpsc::Sender<GenericMessage<ActionReplyPus>>,
2024-04-10 17:03:56 +02:00
stop_signal: Arc<AtomicBool>,
) -> Self {
let mut home_path_stop_file = PathBuf::new();
2024-04-10 17:13:29 +02:00
home_path_stop_file.push(HOME_PATH.as_path());
2024-04-10 17:03:56 +02:00
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,
}
}
2024-04-10 15:37:24 +02:00
}
impl ExperimentController {
pub fn perform_operation(&mut self) {
match self.composite_request_rx.try_recv() {
Ok(msg) => match msg.message {
2024-04-10 15:44:39 +02:00
CompositeRequest::Hk(_) => {
log::warn!("hk request handling unimplemented")
}
2024-04-10 15:37:24 +02:00
CompositeRequest::Action(action_req) => {
self.handle_action_request(msg.requestor_info, action_req);
}
},
2024-04-10 15:44:39 +02:00
Err(e) => {
if e != mpsc::TryRecvError::Empty {
log::error!("composite request rx error: {:?}", e);
}
}
2024-04-10 15:37:24 +02:00
}
self.check_stop_file();
}
pub fn handle_action_request(&mut self, requestor: MessageMetadata, action_req: ActionRequest) {
2024-04-25 01:20:54 +02:00
let send_completion_failure = |error_code: ResultU16, params: Option<Params>| {
2024-04-10 15:37:24 +02:00
let result = self.action_reply_tx.send(GenericMessage::new_action_reply(
requestor,
action_req.action_id,
2024-04-25 01:20:54 +02:00
ActionReplyVariant::CompletionFailed { error_code, params },
2024-04-10 15:37:24 +02:00
));
if result.is_err() {
log::error!("sending action reply failed");
}
2024-04-25 01:20:54 +02:00
};
let action_id = ActionId::try_from(action_req.action_id);
if action_id.is_err() {
send_completion_failure(INVALID_ACTION_ID, None);
2024-04-10 15:37:24 +02:00
return;
}
2024-04-25 01:20:54 +02:00
match action_id.unwrap() {
2024-04-10 15:37:24 +02:00
ActionId::StopExperiment => {
self.stop_signal
.store(true, std::sync::atomic::Ordering::Relaxed);
2024-04-25 01:20:54 +02:00
self.send_completion_success(&requestor, &action_req);
}
ActionId::ExecuteShellCommandBlocking => {
self.handle_shell_command_execution(&requestor, &action_req);
}
ActionId::DownlinkLogfile => self.handle_downlink_logfile(&requestor, &action_req),
// downlink images, default will be the last image, otherwise specified counting down (2 = second to last image, etc.)
ActionId::DownlinkImages => {
log::info!("Copying images into low priority downlink folder");
2024-04-25 16:31:05 +02:00
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")
}
}
}
}
2024-04-10 15:37:24 +02:00
}
}
pub fn handle_downlink_logfile(&self, requestor: &MessageMetadata, action_req: &ActionRequest) {
log::info!("copying logfile into downlink folder");
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::warn!("copying logfile into downlink path failed")
}
self.send_completion_success(requestor, action_req)
}
} else {
log::warn!("downlink path emtpy")
}
}
pub fn send_completion_success(&self, requestor: &MessageMetadata, action_req: &ActionRequest) {
let result = self.action_reply_tx.send(GenericMessage::new_action_reply(
*requestor,
action_req.action_id,
ActionReplyVariant::Completed,
));
if result.is_err() {
log::error!("sending action reply failed");
}
}
pub fn send_completion_failure(
&self,
requestor: &MessageMetadata,
action_req: &ActionRequest,
error_code: ResultU16,
params: Option<Params>,
) {
let result = self.action_reply_tx.send(GenericMessage::new_action_reply(
*requestor,
action_req.action_id,
ActionReplyVariant::CompletionFailed { error_code, params },
));
if result.is_err() {
log::error!("sending action reply failed");
2024-04-25 01:20:54 +02:00
}
}
pub fn handle_shell_command_execution(
&self,
requestor: &MessageMetadata,
action_req: &ActionRequest,
) {
if let ActionRequestVariant::VecData(data) = &action_req.variant {
2024-04-25 16:45:00 +02:00
let shell_cmd_result: serde_json::Result<ShellCmd> = serde_json::from_slice(data);
match shell_cmd_result {
Ok(shell_cmd) => {
log::info!("executing shell cmd {:?}", shell_cmd);
match Command::new(shell_cmd.cmd).args(shell_cmd.args).status() {
2024-04-25 01:20:54 +02:00
Ok(status) => {
if status.success() {
self.send_completion_success(requestor, action_req);
} else {
log::warn!("execution of command failed: {}", status);
self.send_completion_failure(
requestor,
action_req,
SHELL_CMD_EXECUTION_FAILURE,
Some(status.to_string().into()),
);
}
}
Err(e) => {
log::warn!("execution of command failed with IO error: {}", e);
self.send_completion_failure(
requestor,
action_req,
SHELL_CMD_IO_ERROR,
Some(e.to_string().into()),
);
}
}
}
Err(e) => {
2024-04-25 16:45:00 +02:00
log::warn!("failed to deserialize shell command: {}", e);
let result = self.action_reply_tx.send(GenericMessage::new_action_reply(
*requestor,
action_req.action_id,
ActionReplyVariant::Completed,
));
if result.is_err() {
log::error!("Sending action reply failed");
}
2024-04-10 15:37:24 +02:00
}
}
2024-04-25 16:45:00 +02:00
} else {
log::warn!("no shell command was supplied for shell command action command");
self.send_completion_failure(requestor, action_req, SHELL_CMD_INVALID_FORMAT, None);
2024-04-10 15:37:24 +02:00
}
}
pub fn check_stop_file(&self) {
2024-04-10 17:03:56 +02:00
let check_at_path = |path: &Path| {
if path.exists() {
log::warn!(
2024-04-10 17:13:29 +02:00
"Detected stop file name at {:?}. Initiating experiment shutdown",
path
2024-04-10 17:03:56 +02:00
);
2024-04-10 17:13:29 +02:00
// By default, clear the stop file.
let result = std::fs::remove_file(path);
if result.is_err() {
log::error!(
"failed to remove stop file at {:?}: {}",
path,
result.unwrap_err()
);
}
2024-04-10 17:03:56 +02:00
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());
2024-04-10 15:37:24 +02:00
}
}
2024-04-25 16:45:00 +02:00
2024-04-25 16:31:05 +02:00
// 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
2024-04-25 16:31:05 +02:00
let mut png_files = std::fs::read_dir(HOME_FOLDER_EXPERIMENT)?
.flatten()
.filter(|f| match f.metadata() {
2024-04-25 16:31:05 +02:00
Ok(metadata) => metadata.is_file(),
Err(_) => false,
})
2024-04-25 16:31:05 +02:00
.filter(|f| match f.file_name().into_string() {
Ok(name) => name.ends_with(".png"),
Err(_) => false,
})
2024-04-25 16:31:05 +02:00
.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
}
2024-04-25 16:31:05 +02:00
}
Err(_) => std::time::SystemTime::UNIX_EPOCH,
});
png_files.reverse();
if let Some(png) = png_files.into_iter().nth(index) {
return Ok(png.path());
}
2024-04-25 16:31:05 +02:00
Err(std::io::Error::other("No latest image found"))
}
2024-04-25 16:45:00 +02:00
#[cfg(test)]
mod tests {
use std::sync::{mpsc, Arc};
use tempfile::NamedTempFile;
use super::*;
fn init() {
env_logger::builder().is_test(true).init();
}
#[test]
fn test_shell_cmd_exection() {
init();
let (composite_req_tx, composite_req_rx) = mpsc::channel();
let (action_reply_tx, action_reply_rx) = mpsc::channel();
let stop_signal = Arc::default();
let mut exp_ctrl =
ExperimentController::new(composite_req_rx, action_reply_tx, stop_signal);
let named_temp_file = NamedTempFile::new().expect("creating temp file failed");
2024-04-25 17:11:52 +02:00
let args = vec![named_temp_file
.path()
.to_str()
.expect("converting path to str failed")];
let cmd = ShellCmd { cmd: "rm", args };
2024-04-25 16:45:00 +02:00
let cmd_serialized = serde_json::to_string(&cmd).expect("serialization failed");
let action_req = satrs::action::ActionRequest {
action_id: ActionId::ExecuteShellCommandBlocking as u32,
variant: satrs::action::ActionRequestVariant::VecData(cmd_serialized.into_bytes()),
};
composite_req_tx
.send(GenericMessage::new(
MessageMetadata::new(1, 2),
CompositeRequest::Action(action_req),
))
.expect("sending action request failed");
exp_ctrl.perform_operation();
assert!(!named_temp_file.path().exists());
let action_reply = action_reply_rx
.try_recv()
.expect("receiving action reply failed");
assert_eq!(
action_reply.message.action_id,
ActionId::ExecuteShellCommandBlocking as u32
);
match action_reply.message.variant {
ActionReplyVariant::Completed => (),
_ => {
panic!(
"unexecpted action reply variant {:?}",
action_reply.message.variant
)
}
}
}
}