Skip to content

Commit 6729748

Browse files
committed
Autosave Race every 5 min
Signed-off-by: Zingo Andersen <zingo@zingo.org>
1 parent 9bc07fa commit 6729748

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/iTag.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,14 @@ static void saveGlobalConfig()
652652

653653
static void loadRace()
654654
{
655-
checkDisk(); // just for debug
655+
uint64_t start_time = micros();
656+
656657
std::string fileName = std::string("/").append(theRace.getFileName());
657658

658659
File raceFile = LittleFS.open(fileName.c_str(), "r");
659660
if (!raceFile) {
660661
ESP_LOGE(TAG,"ERROR: LittleFS open(%s) for read failed",fileName.c_str());
662+
checkDisk(); // just for debug
661663
return;
662664
}
663665

@@ -666,6 +668,7 @@ static void loadRace()
666668
raceFile.close();
667669
if (err) {
668670
ESP_LOGE(TAG,"ERROR: deserializeJson() failed with code %s",err.f_str());
671+
checkDisk(); // just for debug
669672
return;
670673
}
671674

@@ -794,13 +797,17 @@ static void loadRace()
794797
}
795798
}
796799
}
800+
uint64_t stop_time = micros();
801+
uint32_t tot_time = stop_time - start_time;
802+
ESP_LOGI(TAG,"Loaded race as %s time %d us", fileName.c_str(),tot_time );
803+
797804
}
798805

799806
static void saveRace()
800807
{
808+
uint64_t start_time = micros();
809+
801810
DynamicJsonDocument raceJson(50000); // TODO verify with a maximum Tags/Laps file
802-
//JsonObject raceJson = jsonDoc.createObject();
803-
//char JSONmessageBuffer[200];
804811

805812
raceJson["Appname"] = "CrazyCapyTime";
806813
raceJson["filetype"] = "racedata";
@@ -815,7 +822,6 @@ static void saveRace()
815822
JsonArray tagArrayJson = raceJson.createNestedArray("tag");
816823
for(int i=0; i<ITAG_COUNT; i++)
817824
{
818-
//JsonObject tagJson = raceJson.createNestedObject("tag");
819825
JsonObject tagJson = tagArrayJson.createNestedObject();
820826
tagJson["address"] = iTags[i].address;
821827
tagJson["color0"] = iTags[i].color0;
@@ -835,25 +841,27 @@ static void saveRace()
835841
lapJson["LastSeen"] = iTags[i].participant.getLap(lap).getLastSeen();
836842
}
837843
}
844+
838845
// Print a prettified JSON document to the serial port
839846
//String output = "";
840847
//serializeJsonPretty(raceJson, output);
841848
//ESP_LOGI(TAG,"json: \n%s", output.c_str());
842849

843-
844-
checkDisk(); // just for debug
845-
846850
std::string fileName = std::string("/").append(theRace.getFileName());
847851
File raceFile = LittleFS.open(fileName.c_str(), "w");
848852
if (!raceFile) {
849853
ESP_LOGE(TAG,"ERROR: LittleFS open(%s,w) failed", fileName.c_str());
854+
checkDisk(); // just for debug
850855
return;
851856
}
852857
serializeJson(raceJson, raceFile);
853858
raceFile.close();
854859

855-
checkDisk(); // just for debug
860+
uint64_t stop_time = micros();
861+
uint32_t tot_time = stop_time - start_time;
862+
ESP_LOGI(TAG,"Saved Race as %s time %d us", fileName.c_str(),tot_time );
856863

864+
// checkDisk(); // just for debug
857865
}
858866

859867
void vTaskRaceDB( void *pvParameters )
@@ -879,6 +887,10 @@ void vTaskRaceDB( void *pvParameters )
879887
ESP_LOGI(TAG,"Send Race to GUI");
880888
theRace.send_ConfigMsg(queueGFX);
881889

890+
int lastAutoSaveMinute = rtc.getMinute();
891+
bool autoSaveTainted = false;
892+
893+
882894
for( ;; )
883895
{
884896
msg_RaceDB msg;
@@ -933,14 +945,13 @@ void vTaskRaceDB( void *pvParameters )
933945
// We are withing grace period and RSS was better -> update lap!
934946
iTags[j].participant.setBestRSSInearNewLap(msg.iTag.RSSI);
935947
iTags[j].participant.updateLapTagIsCloser(newLapTime);
936-
937948
}
938949
}
939950
time_t newLastSeenSinceLapStart = difftime(newLapTime,iTags[j].participant.getCurrentLapStart());
940951
//ESP_LOGI(TAG,"%s Connected Time: %s delta %d->%d (%d,%d) %d To early", iTags[j].participant.getName().c_str(),rtc.getTime("%Y-%m-%d %H:%M:%S").c_str(),newLapTime,timeSinceLastSeen,iTags[j].participant.getCurrentLapStart(), iTags[j].participant.getCurrentLastSeen(),newLastSeenSinceLapStart);
941952
iTags[j].participant.setCurrentLastSeen(newLastSeenSinceLapStart);
942953
}
943-
954+
autoSaveTainted = true;
944955
iTags[j].participant.setUpdated(); // Make it redraw when GUI loop looks at it
945956
iTags[j].UpdateParticipantStatusInGUI();
946957

@@ -1062,12 +1073,16 @@ void vTaskRaceDB( void *pvParameters )
10621073
case MSG_ITAG_LOAD_RACE:
10631074
{
10641075
ESP_LOGI(TAG,"Received: MSG_ITAG_LOAD_RACE MSG:0x%x", msg.LoadRace.header.msgType);
1076+
lastAutoSaveMinute = rtc.getMinute(); // Reset autosave timer
1077+
autoSaveTainted = false;
10651078
loadRace();
10661079
break;
10671080
}
10681081
case MSG_ITAG_SAVE_RACE:
10691082
{
10701083
ESP_LOGI(TAG,"Received: MSG_ITAG_SAVE_RACE MSG:0x%x", msg.SaveRace.header.msgType);
1084+
lastAutoSaveMinute = rtc.getMinute(); // Reset autosave timer
1085+
autoSaveTainted = false;
10711086
saveRace();
10721087
break;
10731088
}
@@ -1077,19 +1092,33 @@ void vTaskRaceDB( void *pvParameters )
10771092
// This is used to not accedently count a lap in "too short laps"
10781093
refreshTagGUI();
10791094
//rtc.setTime(rtc.getEpoch()+30,0); //DEBUG fake faster time for testing REMOVE
1095+
1096+
int nowMinute = rtc.getMinute();
1097+
if (lastAutoSaveMinute != nowMinute) {
1098+
// Autosave each 5min if tainted (e.g. something changed)
1099+
if (autoSaveTainted && nowMinute % 5 == 0) {
1100+
lastAutoSaveMinute = nowMinute;
1101+
autoSaveTainted = false;
1102+
saveRace();
1103+
}
1104+
}
10801105
break;
10811106
}
10821107
// Broadcast Messages
10831108
case MSG_RACE_START:
10841109
{
10851110
ESP_LOGI(TAG,"Received: MSG_RACE_START MSG:0x%x startTime:%d", msg.Broadcast.RaceStart.header.msgType,msg.Broadcast.RaceStart.startTime);
1111+
lastAutoSaveMinute = rtc.getMinute(); // Reset autosave timer
1112+
autoSaveTainted = true;
10861113
theRace.setRaceStart(msg.Broadcast.RaceStart.startTime);
10871114
raceStartiTags(msg.Broadcast.RaceStart.startTime);
10881115
break;
10891116
}
10901117
case MSG_RACE_CLEAR:
10911118
{
10921119
ESP_LOGI(TAG,"Received: MSG_RACE_CLEAR MSG:0x%x", msg.Broadcast.RaceStart.header.msgType);
1120+
lastAutoSaveMinute = rtc.getMinute(); // Reset autosave timer
1121+
autoSaveTainted = true;
10931122
raceCleariTags();
10941123
break;
10951124
}

0 commit comments

Comments
 (0)