storage manager
This commit is contained in:
parent
9c61d88fe4
commit
d90011d921
5 changed files with 40 additions and 10 deletions
1
auto/Cargo.lock
generated
1
auto/Cargo.lock
generated
|
@ -1100,6 +1100,7 @@ dependencies = [
|
||||||
"lock_api",
|
"lock_api",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"parking_lot_core",
|
"parking_lot_core",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
1
interface/Cargo.lock
generated
1
interface/Cargo.lock
generated
|
@ -1092,6 +1092,7 @@ dependencies = [
|
||||||
"lock_api",
|
"lock_api",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"parking_lot_core",
|
"parking_lot_core",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -23,4 +23,4 @@ chrono = "0.4.40"
|
||||||
image = "0.25.5"
|
image = "0.25.5"
|
||||||
libloading = "0.8.6"
|
libloading = "0.8.6"
|
||||||
emath = "0.31.1"
|
emath = "0.31.1"
|
||||||
dashmap = "6.1.0"
|
dashmap = { version = "6.1.0", features = ["serde"] }
|
||||||
|
|
|
@ -6,6 +6,8 @@ use eframe::Storage;
|
||||||
use libloading::{Library, Symbol};
|
use libloading::{Library, Symbol};
|
||||||
use tokio::sync::{self, broadcast, mpsc, watch};
|
use tokio::sync::{self, broadcast, mpsc, watch};
|
||||||
|
|
||||||
|
use crate::storage::StorageManager;
|
||||||
|
|
||||||
pub struct AutoConfig {
|
pub struct AutoConfig {
|
||||||
turn_gain: f32,
|
turn_gain: f32,
|
||||||
auto_fire_distance: f32,
|
auto_fire_distance: f32,
|
||||||
|
@ -15,7 +17,7 @@ pub struct AutoConfig {
|
||||||
pub struct AutoInterface {
|
pub struct AutoInterface {
|
||||||
command_sender: Option<mpsc::Sender<ControlPacket>>,
|
command_sender: Option<mpsc::Sender<ControlPacket>>,
|
||||||
data_receiver: watch::Receiver<TelemetryPacket>,
|
data_receiver: watch::Receiver<TelemetryPacket>,
|
||||||
config: &'static dyn Storage,
|
config: StorageManager,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,9 +47,7 @@ impl AutoInterface {
|
||||||
}
|
}
|
||||||
pub fn enabled(&self) -> bool { self.enabled}
|
pub fn enabled(&self) -> bool { self.enabled}
|
||||||
pub fn conf(&self, key: &'static Configurable) -> f32 {
|
pub fn conf(&self, key: &'static Configurable) -> f32 {
|
||||||
self.config.get_string(&key.name)
|
self.config.load(key)
|
||||||
.map(|s| s.parse().ok())
|
|
||||||
.flatten().unwrap_or(key.default)
|
|
||||||
}
|
}
|
||||||
fn send_message(&self, message: String) {unimplemented!()}
|
fn send_message(&self, message: String) {unimplemented!()}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
use dashmap::DashMap;
|
||||||
|
|
||||||
struct StorageManager {
|
use crate::auto::Configurable;
|
||||||
map: DashMap<String, f32>,
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct StorageManager {
|
||||||
|
map: Arc<DashMap<String, f32>>,
|
||||||
storage: Arc<PathBuf>,
|
storage: Arc<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StorageManager {
|
impl StorageManager {
|
||||||
fn new(path: PathBuf) -> Self {
|
pub fn new(path: PathBuf) -> Result<Self> {
|
||||||
Self { map, storage }
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue