-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.h
97 lines (85 loc) · 2.28 KB
/
FileManager.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
#ifndef FILE_MANAGER_H
#define FILE_MANAGER_H
#include "SPIFFS.h"
#include "Constants.h"
bool FILE_DEBUG = 0;
void SetupFileManager() {
if (!SPIFFS.begin()) {
if (FILE_DEBUG) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("SPIFFS mount failed");
}
return;
}
if (FILE_DEBUG) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("SPIFFS mounted");
}
}
void formatFS() {
if (FILE_DEBUG) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("Formatting SPIFFS filesystem");
}
SPIFFS.format();
}
//String listDir(String dirname) {
// String message;
// message = String(FILE_MANAGER_PREFIX) + "Listing directory: " + dirname + "\n";
//
// Dir root = SPIFFS.openDir(dirname);
//
// while (root.next()) {
// File file = root.openFile("r");
// message += " FILE: ";
// message += root.fileName();
// message += " SIZE: ";
// message += file.size();
// time_t cr = file.getCreationTime();
// time_t lw = file.getLastWrite();
// file.close();
// }
// if (FILE_DEBUG) {
// Serial.println(message);
// }
// return message;
//}
String readFile(String path) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.print("Reading file: ");
Serial.println(path);
File file = SPIFFS.open(path, "r");
if (!file) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("Failed to open file for reading");
return "";
}
String toReturn = file.readString();
file.close();
return toReturn;
}
bool writeFile(String path, String message) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.print("Writing file: ");
Serial.println(path);
bool success = false;
File file = SPIFFS.open(path, "w");
if (!file) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("Failed to open file for writing");
return false;
}
if (file.print(message)) {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("File written");
success = true;
} else {
Serial.print(FILE_MANAGER_PREFIX);
Serial.println("Write failed");
success = false;
}
delay(2000);
file.close();
return success;
}
#endif //FILE_MANAGER_H