1
Fork 0

storage manager

This commit is contained in:
Andy Killorin 2025-03-22 14:28:57 -04:00
parent 9c61d88fe4
commit d90011d921
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
5 changed files with 40 additions and 10 deletions

1
auto/Cargo.lock generated
View file

@ -1100,6 +1100,7 @@ dependencies = [
"lock_api",
"once_cell",
"parking_lot_core",
"serde",
]
[[package]]

1
interface/Cargo.lock generated
View file

@ -1092,6 +1092,7 @@ dependencies = [
"lock_api",
"once_cell",
"parking_lot_core",
"serde",
]
[[package]]

View file

@ -23,4 +23,4 @@ chrono = "0.4.40"
image = "0.25.5"
libloading = "0.8.6"
emath = "0.31.1"
dashmap = "6.1.0"
dashmap = { version = "6.1.0", features = ["serde"] }

View file

@ -6,6 +6,8 @@ use eframe::Storage;
use libloading::{Library, Symbol};
use tokio::sync::{self, broadcast, mpsc, watch};
use crate::storage::StorageManager;
pub struct AutoConfig {
turn_gain: f32,
auto_fire_distance: f32,
@ -15,7 +17,7 @@ pub struct AutoConfig {
pub struct AutoInterface {
command_sender: Option<mpsc::Sender<ControlPacket>>,
data_receiver: watch::Receiver<TelemetryPacket>,
config: &'static dyn Storage,
config: StorageManager,
enabled: bool,
}
@ -45,9 +47,7 @@ impl AutoInterface {
}
pub fn enabled(&self) -> bool { self.enabled}
pub fn conf(&self, key: &'static Configurable) -> f32 {
self.config.get_string(&key.name)
.map(|s| s.parse().ok())
.flatten().unwrap_or(key.default)
self.config.load(key)
}
fn send_message(&self, message: String) {unimplemented!()}
}

View file

@ -1,14 +1,42 @@
use std::{cell::RefCell, collections::HashMap, path::PathBuf, sync::Arc};
use std::{cell::RefCell, collections::HashMap, fs::File, io::{Read, Write}, path::PathBuf, sync::Arc};
use dashmap::DashMap;
struct StorageManager {
map: DashMap<String, f32>,
use crate::auto::Configurable;
#[derive(Clone)]
pub struct StorageManager {
map: Arc<DashMap<String, f32>>,
storage: Arc<PathBuf>,
}
impl StorageManager {
fn new(path: PathBuf) -> Self {
Self { map, storage }
pub fn new(path: PathBuf) -> Result<Self> {
let map = if let Ok(mut file) = File::open(path) {
let mut vec = Vec::new();
file.read_to_end(&mut vec)?;
postcard::from_bytes(&vec)?
} else {
DashMap::new()
};
let map = Arc::new(map);
Ok(Self { map, storage: Arc::new(path) })
}
pub fn save(&self) {
let map = postcard::to_stdvec_cobs(&self.map).unwrap();
let mut backing_file = File::open(self.storage.as_path()).unwrap();
backing_file.write_all(&map).unwrap();
}
pub fn load(&self, setting: &'static Configurable) -> f32 {
self.map.get(setting.name).map(|v| *v).unwrap_or(setting.default)
}
pub fn store(&self, setting: &'static Configurable, value: f32) {
self.map.insert(setting.name.to_owned(), value);
}
}