-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandler.h
220 lines (195 loc) · 5.65 KB
/
FileHandler.h
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//Sets error to true to let the user know something is wrong
void setErrorToTrue() {
error = true;
Serial.println(error);
}
void writeLogTimeAlive(){
if (SPIFFS.exists("/time_alive.txt")){
SPIFFS.remove("/time_alive.txt");
}
File file = SPIFFS.open("/time_alive.txt", "w");
file.print(millis());
DeserializationError error = deserializeJson(aliveSince, file);
file.close();
}
void readLogTimeAlive(){
if (!SPIFFS.exists("/time_alive.txt")){
writeLogTimeAlive();
}
File file = SPIFFS.open("/time_alive.txt", "r");
DeserializationError error = deserializeJson(aliveSince, file);
file.close();
}
//All txt files with the exception of config.txt are added to an arraylist
void pushAllFilesToJson() {
dir = SPIFFS.openDir("/");
int i = 0;
while (dir.next()) {
if (dir.fileName().indexOf(".txt") > 0 && dir.fileName().indexOf("config.txt") == -1) {
if (dir.fileName().indexOf("button_config.txt") == -1) {
if(dir.fileName().indexOf("time_alive.txt") == -1){
BUTTON_JSON[i] = String() + dir.fileName();
i++;
amount_of_files = i;
}
}
}
}
if (amount_of_files < 1) {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("No .txt files have been found");
setErrorToTrue();
}
}
//The button files are desirialized to json
void getButtonJSON() {
if (amount_of_files > 0) {
if (SPIFFS.exists(BUTTON_JSON[currentFile])) {
File file = SPIFFS.open(BUTTON_JSON[currentFile], "r");
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/v6/assistant to compute the capacity.
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error) {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("Error parsing file: " + BUTTON_JSON[currentFile]);
tft.println(error.c_str());
setErrorToTrue();
delay(5000);
}
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
}
else {
setErrorToTrue();
}
}
else {
setErrorToTrue();
}
}
//The config file is desirialized to json
void getConfigJSON() {
if (SPIFFS.exists(configFile)) {
File file = SPIFFS.open(configFile, "r");
DeserializationError error = deserializeJson(configDoc, file);
if (error) {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("Error parsing config file");
tft.println(error.c_str());
setErrorToTrue();
delay(5000);
}
file.close();
}
else {
setErrorToTrue();
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("config.txt not found");
delay(5000);
}
}
//The config file is desirialized to json
void getButtonConfigJSON() {
if (SPIFFS.exists(buttonConfigFile)) {
File file = SPIFFS.open(buttonConfigFile, "r");
DeserializationError error = deserializeJson(buttonConfigDoc, file);
if (error) {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("Error parsing button_config file");
tft.println(error.c_str());
setErrorToTrue();
delay(5000);
}
file.close();
}
else {
setErrorToTrue();
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("button_config.txt not found");
tft.println("Buttons will not work");
delay(5000);
}
}
//File changes based on user input
void chooseFile(int keyNumber) {
currentFile = keyNumber - 1;
if (amount_of_files < keyNumber) {
currentFile = 0;
}
getButtonJSON();
interateOverPages();
}
void writeToButtonConfigFile() {
File file = SPIFFS.open(buttonConfigFile, "w");
serializeJsonPretty(buttonConfigDoc, file);
tft.println("Writing to doc");
tft.println(error);
file.close();
}
void writeToConfigFile() {
File file = SPIFFS.open(configFile, "w");
serializeJsonPretty(configDoc, file);
tft.println("Writing to doc");
tft.println(error);
file.close();
}
int getPageIndex(String fileName) {
if (!fileName.startsWith("/")) {
fileName = "/" + fileName;
}
if (!fileName.endsWith(".txt")) {
fileName = fileName + ".txt";
}
for (int x = 0; x < amount_of_files; x++) {
if (fileName == BUTTON_JSON[x]) {
return x;
}
}
}
void writeToButtonFile(String jsonButtons) {
DynamicJsonDocument docBackUp(10422);
DeserializationError error = deserializeJson(docBackUp, jsonButtons);
int index = getPageIndex(docBackUp["fileName"]);
File file = SPIFFS.open(BUTTON_JSON[index], "w");
docBackUp.remove("fileName");
serializeJsonPretty(docBackUp, file);
docBackUp.clear();
file.close();
}
void createNewButtonFile(String jsonButtons){
DynamicJsonDocument docBackUp(10422);
DeserializationError error = deserializeJson(docBackUp, jsonButtons);
String fileName = docBackUp["fileName"];
if (!fileName.startsWith("/")) {
fileName = "/" + fileName;
}
if (!fileName.endsWith(".txt")) {
fileName = fileName + ".txt";
}
File file = SPIFFS.open(fileName, "w");
docBackUp.remove("fileName");
serializeJsonPretty(docBackUp, file);
docBackUp.clear();
file.close();
}
void deleteButtonFile(String jsonButtons){
DynamicJsonDocument docBackUp(10422);
DeserializationError error = deserializeJson(docBackUp, jsonButtons);
String fileName = docBackUp["fileName"];
if (!fileName.startsWith("/")) {
fileName = "/" + fileName;
}
if (!fileName.endsWith(".txt")) {
fileName = fileName + ".txt";
}
if (dir.fileName().indexOf("button_config.txt") == -1){
if (dir.fileName().indexOf("config.txt") == -1){
if(dir.fileName().indexOf("time_alive.txt") == -1){
}
SPIFFS.remove(fileName);
}
}
docBackUp.clear();
}