mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-10-13 23:16:37 +00:00
implemented missing load & save mapping functions
This commit is contained in:
parent
6b2c56f3e5
commit
75130e2d20
@ -1,12 +1,10 @@
|
|||||||
use alloc::{format, vec::Vec};
|
use alloc::vec::Vec;
|
||||||
use embassy_time::Delay;
|
use embassy_time::Delay;
|
||||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||||
use embedded_sdmmc::{
|
use embedded_sdmmc::{SdCard, ShortFileName, TimeSource, Timestamp, VolumeIdx, VolumeManager};
|
||||||
Directory, SdCard, ShortFileName, TimeSource, Timestamp, VolumeIdx, VolumeManager,
|
|
||||||
};
|
|
||||||
use esp_hal::{Blocking, gpio::Output, spi::master::Spi};
|
use esp_hal::{Blocking, gpio::Output, spi::master::Spi};
|
||||||
|
|
||||||
use crate::store::{AttendanceDay, day::Day, persistence::Persistence};
|
use crate::store::{AttendanceDay, IDMapping, day::Day, persistence::Persistence};
|
||||||
|
|
||||||
pub struct DummyTimesource;
|
pub struct DummyTimesource;
|
||||||
|
|
||||||
@ -41,6 +39,8 @@ pub struct SDCardPersistence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SDCardPersistence {
|
impl SDCardPersistence {
|
||||||
|
const MAPPING_FILENAME: &'static str = "MAPPING.JS";
|
||||||
|
|
||||||
fn generate_filename(day: Day) -> ShortFileName {
|
fn generate_filename(day: Day) -> ShortFileName {
|
||||||
let basename = day.to_string();
|
let basename = day.to_string();
|
||||||
let mut filename: heapless::String<11> = heapless::String::new();
|
let mut filename: heapless::String<11> = heapless::String::new();
|
||||||
@ -83,6 +83,7 @@ impl Persistence for SDCardPersistence {
|
|||||||
let mut file = root_dir
|
let mut file = root_dir
|
||||||
.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadWriteCreateOrTruncate)
|
.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadWriteCreateOrTruncate)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
file.write(&serde_json::to_vec(data).unwrap()).unwrap();
|
file.write(&serde_json::to_vec(data).unwrap()).unwrap();
|
||||||
|
|
||||||
file.flush().unwrap();
|
file.flush().unwrap();
|
||||||
@ -90,11 +91,42 @@ impl Persistence for SDCardPersistence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn load_mapping(&mut self) -> Option<crate::store::IDMapping> {
|
async fn load_mapping(&mut self) -> Option<crate::store::IDMapping> {
|
||||||
todo!()
|
let mut vol_0 = self.vol_mgr.open_volume(VolumeIdx(0)).unwrap();
|
||||||
|
let mut root_dir = vol_0.open_root_dir().unwrap();
|
||||||
|
|
||||||
|
let file =
|
||||||
|
root_dir.open_file_in_dir(Self::MAPPING_FILENAME, embedded_sdmmc::Mode::ReadOnly);
|
||||||
|
|
||||||
|
if file.is_err() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut open_file = file.unwrap();
|
||||||
|
|
||||||
|
let mut read_buffer: [u8; 1024] = [0; 1024];
|
||||||
|
let read = open_file.read(&mut read_buffer).unwrap();
|
||||||
|
open_file.close().unwrap();
|
||||||
|
|
||||||
|
let mapping: IDMapping = serde_json::from_slice(&read_buffer[..read]).unwrap();
|
||||||
|
|
||||||
|
Some(mapping)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_mapping(&mut self, data: &crate::store::IDMapping) {
|
async fn save_mapping(&mut self, data: &crate::store::IDMapping) {
|
||||||
todo!()
|
let mut vol_0 = self.vol_mgr.open_volume(VolumeIdx(0)).unwrap();
|
||||||
|
let mut root_dir = vol_0.open_root_dir().unwrap();
|
||||||
|
|
||||||
|
let mut file = root_dir
|
||||||
|
.open_file_in_dir(
|
||||||
|
Self::MAPPING_FILENAME,
|
||||||
|
embedded_sdmmc::Mode::ReadWriteCreateOrTruncate,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
file.write(&serde_json::to_vec(data).unwrap()).unwrap();
|
||||||
|
|
||||||
|
file.flush().unwrap();
|
||||||
|
file.close().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_days(&mut self) -> Vec<Day> {
|
async fn list_days(&mut self) -> Vec<Day> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user