запуск little fs на esp32

This commit is contained in:
Dmitry Borisenko
2022-09-15 14:02:24 +02:00
parent d78e03176f
commit aae21f4a78
15 changed files with 87 additions and 74 deletions

View File

@@ -1,4 +1,5 @@
#include "EspFileSystem.h"
#include "Global.h"
bool fileSystemInit() {
if (!FileFS.begin()) {

View File

@@ -196,12 +196,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
}
//команда очистки всех логов esp
if (headerStr == "/clean1|") {
cleanLogs1();
}
if (headerStr == "/clean2|") {
cleanLogs2();
if (headerStr == "/clean|") {
cleanLogs();
}
//команда обновления прошивки esp

View File

@@ -1,4 +1,3 @@
#pragma once
#include "Global.h"
#include "classes/IoTItem.h"
#include "classes/IoTScenario.h"

View File

@@ -22,7 +22,7 @@ void* getAPI_ButtonOut(String subtype, String params);
void* getAPI_IoTServo(String subtype, String params);
void* getAPI_Mcp23017(String subtype, String params);
void* getAPI_Mp3(String subtype, String params);
void* getAPI_Pwm8266(String subtype, String params);
void* getAPI_Pwm32(String subtype, String params);
void* getAPI_TelegramLT(String subtype, String params);
void* getAPI_Lcd2004(String subtype, String params);
@@ -50,7 +50,7 @@ if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Pwm32(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;
return nullptr;

View File

@@ -38,7 +38,7 @@ class Loging : public IoTItem {
jsonRead(parameters, F("keepdays"), keepdays);
//создадим экземпляр класса даты
dateIoTItem = (IoTItem *)getAPI_Date("{\"id\": \"" + id + "-date\",\"int\":\"20\"}");
dateIoTItem = (IoTItem *)getAPI_Date("{\"id\": \"" + id + "-date\",\"int\":\"20\",\"subtype\":\"date\"}");
IoTItems.push_back(dateIoTItem);
SerialPrint("E", F("Loging"), "created date instance " + id);
}
@@ -75,8 +75,6 @@ class Loging : public IoTItem {
SerialPrint("E", F("Loging"), "'" + id + "' file path not found");
createNewFileWithData(logData);
return;
} else {
// SerialPrint("i", F("Loging"), "'" + id + "' file path found " + filePath);
}
//считаем количество строк
@@ -91,6 +89,8 @@ class Loging : public IoTItem {
} else {
createNewFileWithData(logData);
}
//запускаем процедуру удаления старых файлов если память переполняется
deleteLastFile();
}
void createNewFileWithData(String &logData) {
@@ -135,11 +135,6 @@ class Loging : public IoTItem {
unsigned long fileUnixTimeGMT = selectToMarkerLast(deleteToMarkerLast(buf, "."), "/").toInt() + START_DATETIME;
unsigned long fileUnixTimeLocal = gmtTimeToLocal(fileUnixTimeGMT);
//удаление старых файлов
// if ((fileUnixTimeLocal + (points * (interval / 1000))) < (unixTime - (keepdays * 86400))) {
// SerialPrint("i", F("Loging"), "file '" + buf + "' too old, deleted");
// removeFile(buf);
//} else {
unsigned long reqUnixTime = strDateToUnix(getItemValue(id + "-date"));
if (fileUnixTimeLocal > reqUnixTime && fileUnixTimeLocal < reqUnixTime + 86400) {
noData = false;
@@ -148,7 +143,6 @@ class Loging : public IoTItem {
} else {
SerialPrint("i", F("Loging"), String(f) + ") " + buf + ", nil, " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", skipped");
}
//}
filesList = deleteBeforeDelimiter(filesList, ";");
}
@@ -179,6 +173,28 @@ class Loging : public IoTItem {
}
}
void deleteLastFile() {
IoTFSInfo tmp = getFSInfo();
SerialPrint("i", "Loging", String(tmp.freePer) + " % free flash remaining");
if (tmp.freePer <= 10.00) {
String dir = "lg/" + id;
filesList = getFilesList(dir);
int i = 0;
while (filesList.length()) {
String buf = selectToMarker(filesList, ";");
i++;
if (i == 1) {
removeFile(buf);
SerialPrint("!", "Loging", String(i) + ") " + buf + " => oldest files been deleted");
return;
}
filesList = deleteBeforeDelimiter(filesList, ";");
}
}
}
void createJson(String file, int &i) {
File configFile = FileFS.open(file, "r");
if (!configFile) {

View File

@@ -171,7 +171,8 @@ size_t countLines(const String filename) {
size_t psn;
do {
cnt++;
file.readStringUntil('\r\n');
// или /n тут один знак
file.readStringUntil('\r');
psn = file.position();
} while (psn < size);
file.close();
@@ -226,8 +227,8 @@ String readDataDB(String id) {
return readFile(path, 2000);
}
void cleanLogs1() {
SerialPrint("i", "Files", "cleanLogs1");
void cleanLogs() {
SerialPrint("i", "Files", "cleanLogs");
cleanDirectory("db");
//очистка данных всех экземпляров графиков
for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
@@ -237,20 +238,6 @@ void cleanLogs1() {
}
}
void cleanLogs2() {
SerialPrint("i", "Files", "cleanLogs2");
String dir1 = "db";
SerialPrint("i", "", getFilesList(dir1));
removeDirectory(dir1);
SerialPrint("i", "", getFilesList(dir1));
String dir2 = "lg";
SerialPrint("i", "", getFilesList(dir2));
removeDirectory(dir2);
SerialPrint("i", "", getFilesList(dir2));
}
//счетчик количества записей на флешь за сеанс
void onFlashWrite() {
flashWriteNumber++;
@@ -273,7 +260,7 @@ String getFilesList8266(String& directory) {
#if defined(ESP32)
String getFilesList32(String& directory) {
String filesList = "";
String directory = "/" + directory;
directory = "/" + directory;
File root = FileFS.open(directory);
directory = String();
if (root.isDirectory()) {
@@ -301,31 +288,37 @@ String getFilesList(String& directory) {
bool getInfo(FSInfo& info) {
return FileFS.info(info);
}
// Информация о ФС
void getFSInfo() {
IoTFSInfo getFSInfo() {
IoTFSInfo myFSInfo;
FSInfo buf;
if (getInfo(buf)) {
size_t totalBytes = buf.totalBytes; // всего
size_t usedBytes = buf.usedBytes; // использовано
size_t totalBytes = myFSInfo.totalBytes = buf.totalBytes; // всего
size_t usedBytes = buf.usedBytes; // использовано
// size_t maxOpenFiles = buf.maxOpenFiles; // лимит на открые файлы
// size_t blockSize = buf.blockSize;
// size_t pageSize = buf.pageSize;
// size_t maxPathLength = buf.maxPathLength; // лимит на пути и имена файлов
size_t freeBytes = totalBytes - usedBytes;
float freePer = ((float)freeBytes / totalBytes) * 100;
float freePer = myFSInfo.freePer = ((float)freeBytes / totalBytes) * 100;
jsonWriteStr(errorsHeapJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
} else {
SerialPrint("E", F("FS"), F("FS info error"));
}
return myFSInfo;
}
#endif
#if defined(ESP32)
void getFSInfo() {
size_t totalBytes = FileFS.totalBytes(); // всего
size_t usedBytes = FileFS.usedBytes(); // использовано
IoTFSInfo getFSInfo() {
IoTFSInfo myFSInfo;
size_t totalBytes = myFSInfo.totalBytes = FileFS.totalBytes(); // всего
size_t usedBytes = FileFS.usedBytes(); // использовано
size_t freeBytes = totalBytes - usedBytes;
float freePer = ((float)freeBytes / totalBytes) * 100;
float freePer = myFSInfo.freePer = ((float)freeBytes / totalBytes) * 100;
jsonWriteStr(errorsHeapJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
return myFSInfo;
}
#endif

View File

@@ -23,7 +23,7 @@ bool jsonRead(String& json, String key, unsigned long& value, bool e) {
ret = false;
} else if (!doc.containsKey(key)) {
if (e) {
SerialPrint("EE", F("jsonRead"), key + " missing");
SerialPrint("EE", F("jsonRead"), "json key '" + key + "' missing");
jsonErrorDetected();
}
ret = false;