From aae21f4a78a3224e6c7113ca670d5a5d454f04e1 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 15 Sep 2022 14:02:24 +0200 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=20little?= =?UTF-8?q?=20fs=20=D0=BD=D0=B0=20esp32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/extensions.json | 10 ++++++ .vscode/settings.json | 15 --------- data_svelte/items.json | 8 +++-- include/Const.h | 2 +- include/EspFileSystem.h | 13 +++++--- include/utils/FileUtils.h | 11 +++++-- myProfile.json | 2 +- platformio.ini | 5 +-- src/EspFileSystem.cpp | 1 + src/WsServer.cpp | 8 ++--- src/classes/IoTScenario.cpp | 1 - src/modules/API.cpp | 4 +-- src/modules/virtual/Logging/Loging.cpp | 34 +++++++++++++------ src/utils/FileUtils.cpp | 45 +++++++++++--------------- src/utils/JsonUtils.cpp | 2 +- 15 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 .vscode/extensions.json delete mode 100644 .vscode/settings.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..080e70d0 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 72690792..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "files.associations": { - "system_error": "cpp", - "xlocale": "cpp", - "deque": "cpp", - "initializer_list": "cpp", - "list": "cpp", - "vector": "cpp", - "xhash": "cpp", - "xiosbase": "cpp", - "xstring": "cpp", - "xtree": "cpp", - "xutility": "cpp" - } -} \ No newline at end of file diff --git a/data_svelte/items.json b/data_svelte/items.json index f78855e6..e107c0ea 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -455,16 +455,18 @@ "num": 34 }, { - "name": "35. PWM ESP8266", + "name": "35. PWM ESP32", "type": "Writing", - "subtype": "Pwm8266", + "subtype": "Pwm32", "id": "pwm", "widget": "range", "page": "Кнопки", "descr": "PWM", "int": 0, - "pin": 15, + "pin": 2, "freq": 5000, + "ledChannel": 2, + "PWM_resolution": 10, "val": 0, "apin": -1, "num": 35 diff --git a/include/Const.h b/include/Const.h index 19e16915..32f2c239 100644 --- a/include/Const.h +++ b/include/Const.h @@ -1,7 +1,7 @@ #pragma once //Версия прошивки -#define FIRMWARE_VERSION 420 +#define FIRMWARE_VERSION 421 #ifdef esp8266_4mb #define FIRMWARE_NAME "esp8266_4mb" diff --git a/include/EspFileSystem.h b/include/EspFileSystem.h index eca304cc..4723b754 100644 --- a/include/EspFileSystem.h +++ b/include/EspFileSystem.h @@ -1,11 +1,16 @@ #pragma once #include "Global.h" - #ifdef ESP32 +#if USE_LITTLEFS +#include +#define FileFS LittleFS +#define FS_NAME "LittleFS_32" +#else #include extern FS* filesystem; #define FileFS SPIFFS -#define FS_NAME "SPIFFS" +#define FS_NAME "SPIFFS_32" +#endif #endif #ifdef ESP8266 @@ -15,11 +20,11 @@ extern FS LittleFS; using littlefs_impl::LittleFSConfig; extern FS* filesystem; #define FileFS LittleFS -#define FS_NAME "LittleFS" +#define FS_NAME "LittleFS_8266" #else extern FS* filesystem; #define FileFS SPIFFS -#define FS_NAME "SPIFFS" +#define FS_NAME "SPIFFS_8266" #endif #endif diff --git a/include/utils/FileUtils.h b/include/utils/FileUtils.h index b50953a5..d1a20f0e 100644 --- a/include/utils/FileUtils.h +++ b/include/utils/FileUtils.h @@ -14,8 +14,7 @@ extern size_t countLines(const String filename); void removeFile(const String& filename); void removeDirectory(const String& dir); void cleanDirectory(String path); -void cleanLogs1(); -void cleanLogs2(); +void cleanLogs(); void saveDataDB(String id, String data); String readDataDB(String id); extern void onFlashWrite(); @@ -23,7 +22,13 @@ extern void onFlashWrite(); String getFilesList8266(String& directory); String getFilesList32(String& directory); String getFilesList(String& directory); -extern void getFSInfo(); + +struct IoTFSInfo { + size_t totalBytes; + float freePer; +}; + +extern IoTFSInfo getFSInfo(); #ifdef ESP8266 extern bool getInfo(FSInfo& info); #endif \ No newline at end of file diff --git a/myProfile.json b/myProfile.json index b807c1e6..5b57857e 100644 --- a/myProfile.json +++ b/myProfile.json @@ -21,7 +21,7 @@ }, "projectProp": { "platformio": { - "default_envs": "esp8266_4mb", + "default_envs": "esp32_4mb", "data_dir": "data_svelte" } }, diff --git a/platformio.ini b/platformio.ini index 6ea3592c..72b9330b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -27,7 +27,7 @@ lib_deps = build_flags = -Desp32_4mb="esp32_4mb" framework = arduino board = esp32dev -platform = espressif32 +platform = espressif32 @5.1.1 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -41,10 +41,11 @@ build_src_filter = ${env:esp32_4mb_fromitems.build_src_filter} [platformio] -default_envs = esp8266_4mb +default_envs = esp32_4mb data_dir = data_svelte [common_env_data] +upload_port = COM4 lib_deps_external = bblanchon/ArduinoJson @6.18.0 Links2004/WebSockets diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index 556c2aa4..d7ec44ad 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -1,4 +1,5 @@ #include "EspFileSystem.h" +#include "Global.h" bool fileSystemInit() { if (!FileFS.begin()) { diff --git a/src/WsServer.cpp b/src/WsServer.cpp index c2e62877..b7e4df59 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -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 diff --git a/src/classes/IoTScenario.cpp b/src/classes/IoTScenario.cpp index 00a94314..037209ef 100644 --- a/src/classes/IoTScenario.cpp +++ b/src/classes/IoTScenario.cpp @@ -1,4 +1,3 @@ -#pragma once #include "Global.h" #include "classes/IoTItem.h" #include "classes/IoTScenario.h" diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 9ee02105..b2f9af1f 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -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; diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index a5c84fe2..880750ac 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -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) { diff --git a/src/utils/FileUtils.cpp b/src/utils/FileUtils.cpp index 66f33ba1..ecb6b01d 100644 --- a/src/utils/FileUtils.cpp +++ b/src/utils/FileUtils.cpp @@ -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::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 diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index aaa99e56..f6da97af 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -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;