-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.ino
63 lines (51 loc) · 1.49 KB
/
cache.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Store the cache data into SPIFFS
*/
bool storeCache() {
Serial.println("Storing Cache...");
File cacheFile = SPIFFS.open("/cache.csv", "w");
if (!cacheFile) {
Serial.println("Failed to write Cache. Could not open file.");
return false;
}
// First value in the file is the last date the watering activity was run
if (lastExecutedDate) {
cacheFile.println(lastExecutedDate);
}
if (lastExecutedTime) {
cacheFile.println(currentRTCTime());
}
cacheFile.close();
return true;
}
/**
* Load the cached data from SPIFFS
*/
bool restoreCache() {
Serial.println("Restoring Cache...");
File cacheFile = SPIFFS.open("/cache.csv", "r");
if (!cacheFile) {
Serial.println("No previous cache found.");
return false;
}
// Read the first line as the last run date (YYYYMMDD)
if (cacheFile.available()) {
String lastExecutedDateString = cacheFile.readStringUntil('\n');
lastExecutedDate = lastExecutedDateString.toInt();
}
// Read the second line as the last run time (HHMMSS)
if (cacheFile.available()) {
String lastExecutedTimeString = cacheFile.readStringUntil('\n');
lastExecutedTime = lastExecutedTimeString.toInt();
}
cacheFile.close();
if (lastExecutedDate && lastExecutedTime) {
Serial.print("Watering last executed ");
Serial.print(lastExecutedDate);
Serial.print(" @ ");
Serial.println(lastExecutedTime);
} else {
Serial.println("No previous watering activity recorded.");
}
return true;
}