-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
98 lines (88 loc) · 1.58 KB
/
util.cpp
File metadata and controls
98 lines (88 loc) · 1.58 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
#include "FS.h"
#include "config.h"
#include "util.h"
File logFile;
void log(String l) {
Serial.println(l);
if(true || logFile) {
int b = logFile.println(l);
if(!b) {
Serial.println("Could not write to log file. Log line: " + l);
}
}
else {
Serial.println("Could not open log file. Log line: " + l);
}
}
void initFS() {
if(SPIFFS.begin()) {
log("Filesystem mounted");
}
else {
log("Filesystem mounting failed");
}
}
void initLogs() {
// truncate the log file
logFile = SPIFFS.open(LOG_FILE, "w+");
if(!logFile) {
Serial.println("Couldn't create log file");
}
}
String getFileContents(String fileName) {
String c;
File f;
if(fileName == LOG_FILE) {
f = logFile;
f.seek(0, fs::SeekSet);
}
else {
f = SPIFFS.open(fileName, "r");
}
if(f) {
c = f.readString();
if (fileName != LOG_FILE) {
f.close();
}
}
else {
log("Couldn't open file: " + fileName);
}
return c;
}
bool writeToFile(String fileName, String contents) {
log("Writing to file " + fileName + ": " + contents);
bool s;
File f = SPIFFS.open(fileName, "a+");
if(f) {
int b = f.print(contents);
if(!b) {
log("Couldn't write to file: " + fileName);
}
else {
s = true;
}
f.close();
}
else {
log("Couldn't open file: " + fileName);
}
return s;
}
void mergeJSON(JsonVariant dst, JsonVariantConst src)
{
if (src.is<JsonObjectConst>())
{
for (JsonPairConst kvp : src.as<JsonObjectConst>())
{
if (dst[kvp.key()])
mergeJSON(dst[kvp.key()], kvp.value());
else
dst[kvp.key()] = kvp.value();
}
}
else
{
dst.set(src);
}
}