-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BoxSD.cpp
59 lines (50 loc) · 1.75 KB
/
BoxSD.cpp
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
#include "BoxSD.h"
void BoxSD::begin() {
_initialized = false;
Log.info("Init SD card");
int result = FatFs.begin();
if (!result) {
Log.error("SD not mounted. Code %i", FatFs.error());
return;
}
Log.info(" Capacity: %iMB", FatFs.capacity()/1024);
Log.info(" Free: %iMB", FatFs.free()/1024);
_initialized = true;
}
void BoxSD::loop() {
}
bool BoxSD::isInitialized() {
return _initialized;
}
void BoxSD::webJsonListDir(WebServer* webServer, char* directory) {
DirFs dir;
if (dir.openDir(directory)) {
webServer->sendContent("{\"files\":[");
bool firstRound = true;
while (dir.nextFile()) {
StaticJsonDocument<361> file; //Maximum 256 chars filename length //https://arduinojson.org/v6/assistant/
file["name"] = dir.fileName();
file["size"] = dir.fileSize();
file["time"] = dir.fileModTime();
file["date"] = dir.fileModDate();
file["dir"] = dir.isDir();
size_t len = measureJson(file)+1;
char json[len];
serializeJson(file, json, len); //TODO directly stream to save mem
if (!firstRound)
webServer->sendContent(",");
webServer->sendContent(json);
firstRound = false;
}
dir.closeDir();
webServer->sendContent("]}");
} else {
StaticJsonDocument<299> doc; //Maximum 256 chars path length //https://arduinojson.org/v6/assistant/
doc["error"] = "Dir not found";
Log.error("Dir %s not found", directory);
size_t len = measureJson(doc)+1;
char json[len];
serializeJson(doc, json, len); //TODO directly stream to save mem
webServer->sendContent(json);
}
}