mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-28 23:22:19 +03:00
first
This commit is contained in:
66
src/main.cpp
Normal file
66
src/main.cpp
Normal 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
87
src/rest.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user