Skip to content

Commit

Permalink
procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
andtsa committed Jul 6, 2024
1 parent 4c799a2 commit e9c4103
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
6 changes: 6 additions & 0 deletions config/procedures/example.procedure
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is an example procedure:
People: Kiko, Kiril
Equipment: Andreas

I refuse to elaborate.

48 changes: 46 additions & 2 deletions gs/station/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;
use crate::api::Message;
use crate::Command;
use std::path::PathBuf;
use tokio::task::AbortHandle;

// /// Any frontend that interfaces with this backend needs to comply to this trait
Expand Down Expand Up @@ -52,7 +52,10 @@ impl Backend {
message_receiver,
command_transmitter,
command_receiver,
log: Log { messages: vec![], commands: vec![] },
log: Log {
messages: vec![],
commands: vec![],
},
}
}

Expand Down Expand Up @@ -146,4 +149,45 @@ impl Backend {
pub fn log_cmd(&mut self, cmd: &Command) {
self.log.commands.push(*cmd);
}

pub fn load_procedures(folder: PathBuf) -> anyhow::Result<Vec<[String; 5]>> {
let mut r = vec![];

for entry in std::fs::read_dir(folder)? {
let f = entry?;

if f.file_name().to_str()?.ends_with(".procedure") {
let contents = std::fs::read_to_string(f.path())?;
let mut lines = contents.lines();
let name = f
.file_name()
.to_str()?
.trim_end_matches(".procedure")
.to_string();
let title = lines.next()?.trim_start_matches([' ', '#']).to_string();
let people = lines
.next()?
.trim_start()
.trim_start_matches("people")
.trim_start_matches("People")
.trim_start_matches("PEOPLE")
.trim_start_matches(":")
.trim_start()
.to_string();
let equipment = lines
.next()?
.trim_start()
.trim_start_matches("equipment")
.trim_start_matches("Equipment")
.trim_start_matches("EQUIPMENT")
.trim_start_matches(":")
.trim_start()
.to_string();
let content = lines.collect::<Vec<&str>>().join("\n").trim().to_string();
r.push([name, title, people, equipment, content]);
}
}

Ok(r)
}
}
25 changes: 25 additions & 0 deletions gs/station/src/frontend/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::frontend::{BackendState, BACKEND};
use crate::Datatype;
use crate::{Command, ERROR_CHANNEL, INFO_CHANNEL, STATUS_CHANNEL, WARNING_CHANNEL};
use rand::Rng;
use std::path::PathBuf;
use std::sync::Mutex;
use tauri::{Manager, State};

Expand Down Expand Up @@ -182,3 +183,27 @@ pub fn quit_server() {
backend_mutex.get_mut().unwrap().quit_server();
}
}

#[allow(unused)]
#[tauri::command]
pub fn procedures() -> Vec<[String; 5]> {
let res =
Backend::load_procedures(PathBuf::from_string("../../../../config/procedures/").unwrap());
if let Some(backend_mutex) = unsafe { BACKEND.as_mut() } {
if let Ok(x) = res {
backend_mutex
.get_mut()
.unwrap()
.log_msg(Message::Info("Loading procedures".into()));
x
} else {
backend_mutex
.get_mut()
.unwrap()
.log_msg(Message::Error("Failed to load procedures".into()));
Vec::new()
}
} else {
res.unwrap()
}
}

0 comments on commit e9c4103

Please sign in to comment.