11use alloc:: { rc:: Rc , string:: String } ;
22use embassy_sync:: { blocking_mutex:: raw:: CriticalSectionRawMutex , signal:: Signal } ;
3- use embassy_time:: { Duration , Instant } ;
3+ use embassy_time:: { Duration , Instant , Timer } ;
44use esp_hal_wifimanager:: Nvs ;
55use 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
139139impl 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
241242impl 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