Skip to content

Commit 81398d1

Browse files
committed
feat: invalidate saved state after 6 hours
1 parent f36aee8 commit 81398d1

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/lcd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ async fn process_lcd_overwrite(
414414
_global_state: &GlobalState,
415415
lcd_driver: &mut LcdAbstract<80, 16, 2, 3>,
416416
) -> bool {
417-
if !current_state.scene.can_be_lcd_overwritten() {
417+
if !current_state.scene.can_be_lcd_overwritten() && current_state.ota_update.is_none() {
418418
return false;
419419
}
420420

src/state.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::{rc::Rc, string::String};
22
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
3-
use embassy_time::{Duration, Instant};
3+
use embassy_time::{Duration, Instant, Timer};
44
use esp_hal_wifimanager::Nvs;
55
use serde::{Deserialize, Serialize};
66

@@ -133,7 +133,7 @@ pub struct SavedGlobalState {
133133
pub penalty: i8,
134134
pub session_id: String,
135135
pub current_competitor: u64,
136-
// pub solve_epoch: u64
136+
pub solve_epoch: u64,
137137
}
138138

139139
impl SignaledGlobalStateInner {
@@ -207,7 +207,7 @@ impl SignaledGlobalStateInner {
207207
}
208208

209209
pub fn to_saved_global_state(&self) -> Option<SavedGlobalState> {
210-
log::warn!("TO_SAVED_GLOBAL_STATE: {self:?}");
210+
log::debug!("TO_SAVED_GLOBAL_STATE: {self:?}");
211211

212212
Some(SavedGlobalState {
213213
session_id: self.session_id.clone()?,
@@ -217,6 +217,7 @@ impl SignaledGlobalStateInner {
217217
inspection_time: self
218218
.inspection_end
219219
.map(|e| (e - self.inspection_start.unwrap_or(Instant::now())).as_millis()),
220+
solve_epoch: current_epoch(),
220221
})
221222
}
222223

@@ -240,16 +241,28 @@ impl SignaledGlobalStateInner {
240241

241242
impl SavedGlobalState {
242243
pub async fn from_nvs(nvs: &Nvs) -> Option<Self> {
244+
while unsafe { EPOCH_BASE == 0 } {
245+
Timer::after_millis(5).await;
246+
}
247+
243248
let mut buf = [0; 1024];
244249
nvs.get_key(b"SAVED_GLOBAL_STATE", &mut buf).await.ok()?;
245250
let end_pos = buf.iter().position(|&x| x == 0x00).unwrap_or(buf.len());
251+
let res: SavedGlobalState = serde_json::from_slice(&buf[..end_pos]).ok()?;
252+
253+
// 6hours
254+
if current_epoch() - res.solve_epoch > 6 * 60 * 60 {
255+
log::error!("REMOVE SOLVE: {:?} {:?}", current_epoch(), res.solve_epoch);
256+
return None;
257+
}
246258

247-
serde_json::from_slice(&buf[..end_pos]).ok()
259+
Some(res)
248260
}
249261

250262
pub async fn to_nvs(&self, nvs: &Nvs) {
251263
let res = serde_json::to_vec(&self);
252264
if let Ok(vec) = res {
265+
_ = nvs.invalidate_key(b"SAVED_GLOBAL_STATE").await;
253266
let res = nvs.append_key(b"SAVED_GLOBAL_STATE", &vec).await;
254267
if let Err(e) = res {
255268
log::error!(

0 commit comments

Comments
 (0)