-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkout_storage.cpp
More file actions
187 lines (170 loc) · 5.54 KB
/
Copy pathworkout_storage.cpp
File metadata and controls
187 lines (170 loc) · 5.54 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "workout_storage.h"
#include <Arduino.h>
using namespace WorkoutStorage;
bool WorkoutStorage::begin() {
// Try to mount the "spiffs" labeled partition used by huge_app.csv; format on failure.
if (!LittleFS.begin(false, "/littlefs", 10, "spiffs")) {
Serial.println("LittleFS mount failed in WorkoutStorage; formatting...");
LittleFS.begin(true, "/littlefs", 10, "spiffs");
}
if (!LittleFS.exists("/workouts")) {
LittleFS.mkdir("/workouts");
}
return true;
}
static String path_for(const String& id) {
return "/workouts/" + id + ".json";
}
// Serialize a single swim as {speed,dur,note} (the format the UI has always used).
static void write_swim(JsonObject o, const SwimStep &s) {
o["speed"] = s.pace100s; // pace100s -> "speed"
o["dur"] = s.durSec;
o["note"] = s.note;
}
String WorkoutStorage::to_json(const Workout &w) {
// Use heap allocation to avoid stack overflow; size based on content
size_t swimCount = 0;
for (const auto &it : w.items) swimCount += it.isSet ? it.swims.size() : 1;
size_t cap = 512 + (swimCount * 96);
for (const auto &it : w.items) {
cap += it.note.length();
if (it.isSet) { for (const auto &s : it.swims) cap += s.note.length(); }
else cap += it.swim.note.length();
}
if (cap < 1024) cap = 1024;
if (cap > 8192) cap = 8192;
DynamicJsonDocument doc(cap);
doc["id"] = w.id; // string id, preserved verbatim
doc["title"] = w.name; // use "title" not "name"
JsonArray arr = doc.createNestedArray("swims"); // "swims" key
for (const auto &it : w.items) {
JsonObject o = arr.createNestedObject();
if (it.isSet) {
o["type"] = "set";
o["repeat"] = it.repeat;
o["note"] = it.note;
JsonArray sub = o.createNestedArray("swims");
for (const auto &s : it.swims) write_swim(sub.createNestedObject(), s);
} else {
write_swim(o, it.swim); // plain swim: unchanged {speed,dur,note}
}
}
String out;
serializeJson(doc, out);
return out;
}
bool WorkoutStorage::from_json(const uint8_t *data, size_t len, Workout &w) {
// Allocate document on heap sized to input length (clamped)
size_t cap = len + 512;
if (cap < 1024) cap = 1024;
if (cap > 8192) cap = 8192;
DynamicJsonDocument doc(cap);
auto err = deserializeJson(doc, data, len);
if (err) {
Serial.printf("JSON parse error: %s\n", err.c_str());
return false;
}
w.id = String(doc["id"] | ""); // preserve string id verbatim
w.name = String(doc["title"] | "Unnamed"); // use "title"
auto read_swim = [](JsonObjectConst o) {
SwimStep s;
s.pace100s = o["speed"] | 0U; // "speed" -> pace100s
s.durSec = o["dur"] | 0UL;
s.note = String(o["note"] | "");
return s;
};
w.items.clear();
// Each element of "swims" is either a plain swim {speed,dur,note} (legacy &
// current) or a set {type:"set", repeat, note, swims:[...]}. Elements without
// type:"set" parse as plain swims, so old workout files still load unchanged.
for (JsonObjectConst elem : doc["swims"].as<JsonArrayConst>()) {
const char* type = elem["type"] | "";
WorkoutItem it;
if (strcmp(type, "set") == 0) {
it.isSet = true;
uint32_t rep = elem["repeat"] | 1U;
if (rep < 1) rep = 1;
if (rep > 100) rep = 100; // sanity clamp
it.repeat = (uint16_t)rep;
it.note = String(elem["note"] | "");
for (JsonObjectConst s : elem["swims"].as<JsonArrayConst>())
it.swims.push_back(read_swim(s));
} else {
it.isSet = false;
it.swim = read_swim(elem);
}
w.items.push_back(std::move(it));
}
return true;
}
std::vector<FlatStep> WorkoutStorage::flatten(const Workout &w) {
std::vector<FlatStep> out;
int16_t setIdx = -1;
for (const auto &it : w.items) {
if (it.isSet) {
++setIdx;
uint16_t reps = it.repeat < 1 ? 1 : it.repeat;
for (uint16_t r = 1; r <= reps; ++r) {
uint16_t pos = 0;
for (const auto &s : it.swims) {
FlatStep fs;
fs.swim = s;
fs.setIndex = setIdx;
fs.setRep = r;
fs.setReps = reps;
fs.setSize = (uint16_t)it.swims.size();
fs.setPos = pos++;
fs.setName = it.note;
out.push_back(fs);
}
}
} else {
FlatStep fs;
fs.swim = it.swim; // setIndex stays -1
out.push_back(fs);
}
}
return out;
}
std::vector<String> WorkoutStorage::list_ids() {
std::vector<String> ids;
File dir = LittleFS.open("/workouts","r");
if (!dir) return ids;
File file = dir.openNextFile();
while (file) {
String name = file.name();
if ( name.endsWith(".json")) {
String id_str = name.substring(0, name.length() - 5);
ids.push_back(id_str);
}
file = dir.openNextFile();
}
return ids;
}
bool WorkoutStorage::load(String id, Workout &out) {
String p = path_for(id);
Serial.println("opening file");
Serial.println(p);
File f = LittleFS.open(p, "r");
if (!f) return false;
String j = f.readString();
f.close();
return from_json((const uint8_t*)j.c_str(), j.length(), out);
}
bool WorkoutStorage::save(const Workout &w) {
if (!LittleFS.exists("/workouts")) LittleFS.mkdir("/workouts");
String p = path_for(w.id);
Serial.println("saving file");
Serial.println(p);
File f = LittleFS.open(p, "w");
if (!f) return false;
String S = to_json(w);
Serial.println("saving file");
Serial.println(S);
f.print(S);
f.close();
return true;
}
bool WorkoutStorage::erase(String id) {
return LittleFS.remove(path_for(id));
}