initial file browser work

This commit is contained in:
2024-11-20 01:14:39 +01:00
parent 8218c4b9a6
commit 9f249af430
3 changed files with 145 additions and 16 deletions

View File

@@ -26,8 +26,10 @@ use plant_ctrl2::sipo::ShiftRegister40;
use anyhow::{anyhow, Context};
use anyhow::{bail, Ok, Result};
use serde::Serialize;
use std::ffi::CString;
use std::fs::File;
use std::fs::{self, DirEntry, File};
use std::io::{Read, Write};
use std::path::Path;
use chrono::{DateTime, Utc};
@@ -60,6 +62,7 @@ const REPEAT_MOIST_MEASURE: usize = 1;
const SPIFFS_PARTITION_NAME: &str = "storage";
const CONFIG_FILE: &str = "/spiffs/config.cfg";
const BASE_PATH: &str = "/spiffs";
const TANK_MULTI_SAMPLE: usize = 11;
@@ -165,7 +168,34 @@ pub struct PlantCtrlBoard<'a> {
>,
}
#[derive(Serialize, Debug)]
pub struct FileInfo{
filename:String,
size:usize
}
impl PlantCtrlBoard<'_> {
pub fn list_files(&self) -> Result<Vec<FileInfo>> {
let spiffs = Path::new(BASE_PATH);
let list = fs::read_dir(spiffs).unwrap().map(|dir| {
let file = dir.unwrap();
FileInfo{
filename: file.file_name().into_string().unwrap(),
size: file.metadata().unwrap().len() as usize
}
});
return Ok(list.collect());
}
pub fn get_file_handle(&self, filename:String, write:bool) -> Result<File> {
let filepath = Path::new(BASE_PATH).join(Path::new(&filename));
return Ok(if write {
File::create(filepath)?
} else {
File::open(filepath)?
})
}
pub fn is_day(&self) -> bool {
self.solar_is_day.get_level().into()
}