DS made DsFile a trait

This commit is contained in:
Jacob Weaver
2024-12-13 14:32:54 -06:00
parent 808c218646
commit 19a0cc1a8e
7 changed files with 107 additions and 114 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ edition = "2021"
path = "src/ds.rs" path = "src/ds.rs"
[features] [features]
default = ["std"] default = []
std = [] std = []
[dependencies] [dependencies]
+9 -15
View File
@@ -1,15 +1,9 @@
#![no_std] #![no_std]
#[cfg(feature = "std")]
mod ds_std;
use bincode::encode_to_vec; use bincode::encode_to_vec;
#[cfg(feature = "std")]
pub use ds_std::DsFile;
#[cfg(not(feature = "std"))] mod file;
mod ds_stub; pub use file::*;
#[cfg(not(feature = "std"))]
pub use ds_stub::DsFile;
extern crate alloc; extern crate alloc;
@@ -20,10 +14,10 @@ use msg::{DsCmd, DsHk, DsOutData, DsTlmSet, Instance, Msg, TlmSetId};
use rfe::*; use rfe::*;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct DsData { pub struct DsData<F: DsFile> {
hk: DsHk, hk: DsHk,
out_data: DsOutData, out_data: DsOutData,
file_list: HashMap<TlmSetId, DsFile>, file_list: HashMap<TlmSetId, F>,
enabled: bool, enabled: bool,
} }
@@ -33,13 +27,13 @@ pub struct DsFileSettings {
pub enabled: bool, pub enabled: bool,
} }
pub struct Ds { pub struct Ds<F: DsFile> {
data: DsData, data: DsData<F>,
tlm_sets: HashMap<TlmSetId, DsTlmSet>, tlm_sets: HashMap<TlmSetId, DsTlmSet>,
start_enabled: bool, start_enabled: bool,
} }
impl Ds { impl<F: DsFile> Ds<F> {
pub fn new(tlm_sets: HashMap<TlmSetId, DsTlmSet>, start_enabled: bool) -> Self { pub fn new(tlm_sets: HashMap<TlmSetId, DsTlmSet>, start_enabled: bool) -> Self {
Self { Self {
data: Default::default(), data: Default::default(),
@@ -61,7 +55,7 @@ impl Ds {
} }
} }
impl App for Ds { impl<F: DsFile> App for Ds<F> {
fn init(&mut self, rfe: &mut rfe::Rfe) -> Result<()> { fn init(&mut self, rfe: &mut rfe::Rfe) -> Result<()> {
self.data = Default::default(); self.data = Default::default();
self.data.enabled = self.start_enabled; self.data.enabled = self.start_enabled;
@@ -152,7 +146,7 @@ impl App for Ds {
if let Some(f) = self.data.file_list.get_mut(&tlm_set.id) { if let Some(f) = self.data.file_list.get_mut(&tlm_set.id) {
f f
} else { } else {
let f = DsFile::new(tlm_set.path.clone()); let f = F::new(tlm_set.path.clone());
self.data.file_list.insert(tlm_set.id, f); self.data.file_list.insert(tlm_set.id, f);
self.data.file_list.get_mut(&tlm_set.id).unwrap() self.data.file_list.get_mut(&tlm_set.id).unwrap()
}; };
-72
View File
@@ -1,72 +0,0 @@
extern crate std;
use alloc::{
format,
string::{String, ToString},
};
use chrono::Utc;
use log::*;
use std::{fs::File, io::Write, path::Path};
#[derive(Debug)]
pub struct DsFile {
pub dir: String,
pub file: Option<File>,
prefix: String,
}
impl DsFile {
pub fn new(dir: String) -> Self {
Self {
file: None,
prefix: dir
.clone()
.split("/")
.last()
.unwrap_or("unnamed")
.to_string(),
dir,
}
}
pub fn close(&mut self) {
self.file = None;
}
pub fn open(&mut self) {
let time = Self::get_time(&self.prefix);
let file_path = Path::new(&self.dir).join(time);
self.file = match File::create(&file_path) {
Ok(f) => Some(f),
Err(e) => {
error!("failed to create file at {:?} {}", file_path, e);
None
}
};
}
fn get_time(prefix: &str) -> String {
let date = Utc::now();
format!("{}_{}", prefix, date.format("%Y-%m-%d_%H-%M-%S.dat"))
}
pub fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.file.is_none() {
self.open();
}
if let Some(f) = &mut self.file {
return f.write(buf);
} else {
return Ok(0);
}
}
pub fn flush(&mut self) -> std::io::Result<()> {
if let Some(f) = &mut self.file {
return f.flush();
} else {
return Ok(());
}
}
}
-25
View File
@@ -1,25 +0,0 @@
use alloc::string::String;
use anyhow::Result;
#[derive(Debug)]
pub struct DsFile {
pub dir: String,
}
impl DsFile {
pub fn new(dir: String) -> Self {
Self { dir }
}
pub fn close(&mut self) {}
pub fn open(&mut self) {}
pub fn write(&mut self, _buf: &[u8]) -> Result<usize> {
return Ok(0);
}
pub fn flush(&mut self) -> Result<()> {
return Ok(());
}
}
+95
View File
@@ -0,0 +1,95 @@
extern crate alloc;
use alloc::string::String;
use anyhow::Result;
pub trait DsFile: Default {
fn new(dir: String) -> Self;
fn close(&mut self);
fn open(&mut self);
fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn flush(&mut self) -> Result<()>;
}
#[cfg(feature = "std")]
mod file_std {
extern crate std;
use alloc::{
format,
string::{String, ToString},
};
use anyhow::Result;
use chrono::Utc;
use log::*;
use std::{fs::File, io::Write, path::Path};
use super::DsFile;
#[derive(Debug, Default)]
pub struct StdDsFile {
pub dir: String,
pub file: Option<File>,
prefix: String,
}
impl StdDsFile {
fn get_time(prefix: &str) -> String {
let date = Utc::now();
format!("{}_{}", prefix, date.format("%Y-%m-%d_%H-%M-%S.dat"))
}
}
impl DsFile for StdDsFile {
fn new(dir: String) -> Self {
Self {
file: None,
prefix: dir
.clone()
.split("/")
.last()
.unwrap_or("unnamed")
.to_string(),
dir,
}
}
fn close(&mut self) {
self.file = None;
}
fn open(&mut self) {
let time = Self::get_time(&self.prefix);
let file_path = Path::new(&self.dir).join(time);
self.file = match File::create(&file_path) {
Ok(f) => Some(f),
Err(e) => {
error!("failed to create file at {:?} {}", file_path, e);
None
}
};
}
fn write(&mut self, buf: &[u8]) -> Result<usize> {
if self.file.is_none() {
self.open();
}
if let Some(f) = &mut self.file {
return Ok(f.write(buf)?);
} else {
return Ok(0);
}
}
fn flush(&mut self) -> Result<()> {
if let Some(f) = &mut self.file {
return Ok(f.flush()?);
} else {
return Ok(());
}
}
}
}
#[cfg(feature = "std")]
pub use file_std::*;
+1
View File
@@ -9,6 +9,7 @@ rfe.path = "../../rfe"
rfe.features = ["std"] rfe.features = ["std"]
example.path = "../../apps/example" example.path = "../../apps/example"
ds.path = "../../apps/ds" ds.path = "../../apps/ds"
ds.features = ["std"]
hs.path = "../../apps/hs" hs.path = "../../apps/hs"
hs.features = ["std"] hs.features = ["std"]
to.path = "../../apps/to" to.path = "../../apps/to"
+1 -1
View File
@@ -95,7 +95,7 @@ fn main() -> Result<()> {
); );
let mut example = Example::new(); let mut example = Example::new();
let mut ds = Ds::new(record, false); let mut ds = Ds::<StdDsFile>::new(record, false);
// let mut wd = LinuxWatchdog::new().unwrap(); // let mut wd = LinuxWatchdog::new().unwrap();
let mut grabber = StdSystemInfoGrabber::new(); let mut grabber = StdSystemInfoGrabber::new();
let mut hs = Hs::new( let mut hs = Hs::new(