This commit is contained in:
Dmitry Borisenko
2021-12-22 14:09:50 +01:00
parent 5e9b15e7de
commit 24a9d55ea0
195 changed files with 15629 additions and 0 deletions

66
src/main.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include "main.h"
#include "rest.h"
void setup() {
Serial.begin(115200);
Serial.flush();
Serial.println();
Serial.println(F("--------------started----------------"));
fileSystemInit();
Serial.println("before " + prettyBytes(ESP.getFreeHeap()));
setupESP();
Serial.println("after " + prettyBytes(ESP.getFreeHeap()));
}
void loop() {
}
void setupESP() {
File file1 = seekFile("/setup.json"); //читаем первый файл из памяти стримом
File file2 = FileFS.open("/setup.json.tmp", "w"); //открыл второй файл для записи
file2.println("[");
// WriteBufferingStream bfile2(file2, 64); //записываем стрим во второй файл для записи
// ReadBufferingStream bfile1{file1, 64}; //стримим первый файл
DynamicJsonDocument doc(1024);
Serial.println("during " + prettyBytes(ESP.getFreeHeap()));
int i = 0;
file1.find("[");
do {
i++;
deserializeJson(doc, file1);
doc["web"]["order"] = i;
serializeJsonPretty(doc, file2);
file2.println(",");
// DeserializationError error =
// if (error) {
// Serial.print("json error: ");
// Serial.println(error.f_str());
// }
Serial.println(
String(i) + ") " +
doc["type"].as<String>() + " " +
doc["set"]["gpio"].as<String>() + " " +
doc["web"]["order"].as<String>());
} while (file1.findUntil(",", "]"));
file2.println("]");
file2.close();
// if (cutFile("/setup.json.tmp", "/setup.json")) Serial.println("file overwrited");
Serial.println("-------------");
Serial.println(readFile("/setup.json.tmp", 20000));
Serial.println("-------------");
}

87
src/rest.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "rest.h"
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
Serial.println("[E] file error");
}
file.seek(position, SeekSet);
return file;
}
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "w");
if (!file) {
return "failed";
}
file.print(str);
file.close();
return "sucсess";
}
const String readFile(const String& filename, size_t max_size) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
}
size_t size = file.size();
if (size > max_size) {
file.close();
return "large";
}
String temp = file.readString();
file.close();
return temp;
}
const String filepath(const String& filename) {
return filename.startsWith("/") ? filename : "/" + filename;
}
bool fileSystemInit() {
if (!FileFS.begin()) {
Serial.println("FS Init ERROR, may be FS was not flashed");
return false;
}
Serial.println("FS Init completed");
return true;
}
String prettyBytes(size_t size) {
if (size < 1024)
return String(size) + "b";
else if (size < (1024 * 1024))
return String(size / 1024.0) + "kB";
else if (size < (1024 * 1024 * 1024))
return String(size / 1024.0 / 1024.0) + "MB";
else
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
}
bool cutFile(const String& src, const String& dst) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
Serial.println("cut " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
Serial.println("not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
FileFS.remove(dstPath);
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.open(dstPath, "w");
uint8_t buf[512];
while (srcFile.available()) {
size_t len = srcFile.read(buf, 512);
dstFile.write(buf, len);
}
srcFile.close();
dstFile.close();
FileFS.remove(srcPath);
return true;
}