From e39806e0ac035b958cf169e004a3f7d0a03ad9b5 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Sat, 2 Jan 2021 03:26:16 +0100 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B0=D0=BB=D1=8C=D1=82=D0=BD=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D0=B2=D0=BD=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Utils/FileUtils.h | 6 ++++ include/Utils/StringUtils.h | 4 ++- include/items/vLogging.h | 3 +- platformio.ini | 3 +- src/Tests.cpp | 10 ++++-- src/Utils/FileUtils.cpp | 35 +++++++++++++++------ src/Utils/StringUtils.cpp | 19 +++++++++--- src/items/vLogging.cpp | 62 ++++++++++++++++++++++++++++++++++--- 8 files changed, 117 insertions(+), 25 deletions(-) diff --git a/include/Utils/FileUtils.h b/include/Utils/FileUtils.h index 3c33557e..7e176723 100644 --- a/include/Utils/FileUtils.h +++ b/include/Utils/FileUtils.h @@ -1,5 +1,6 @@ #pragma once #include + #include "Consts.h" #include "FileSystem.h" @@ -44,6 +45,11 @@ const String writeFile(const String& filename, const String& str); */ const String readFile(const String& filename, size_t max_size); +/* +* Чтение файла в строку с записью его размера +*/ +const String readFileSz(const String& filename, size_t max_size, size_t& size); + /* * Размер файла */ diff --git a/include/Utils/StringUtils.h b/include/Utils/StringUtils.h index 6ba28348..b2c52464 100644 --- a/include/Utils/StringUtils.h +++ b/include/Utils/StringUtils.h @@ -22,7 +22,9 @@ String deleteToMarkerLast(String str, String found); String selectFromMarkerToMarker(String str, String found, int number); -size_t itemsCount(String str, const String& separator); +size_t itemsCount2(String& str, const String& separator); + +size_t itemsCount(String& str, const char* delim); boolean isDigitStr(const String&); diff --git a/include/items/vLogging.h b/include/items/vLogging.h index de217983..61a158db 100644 --- a/include/items/vLogging.h +++ b/include/items/vLogging.h @@ -14,7 +14,7 @@ class LoggingClass { ~LoggingClass(); void loop(); - void execute(String payload); + void execute(String keyOrValue); private: @@ -35,4 +35,5 @@ extern void logging(); extern void loggingExecute(); extern void choose_log_date_and_send(); extern void sendLogData(String file, String topic); +extern void sendLogData2(String file, String topic); extern void cleanLogAndData(); diff --git a/platformio.ini b/platformio.ini index a74a9eca..7af0d773 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,13 +8,12 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -;Please use one of definition: +;To choose board please use one of definition: ;esp8266_1mb , esp8266_4mb , esp32_4mb [platformio] default_envs = esp8266_4mb - [common_env_data] lib_deps_external = bblanchon/ArduinoJson @5.* diff --git a/src/Tests.cpp b/src/Tests.cpp index 6b1d9cad..0ce15233 100644 --- a/src/Tests.cpp +++ b/src/Tests.cpp @@ -1,10 +1,16 @@ #include "Tests.h" -#include "Macro.h" + #include "Global.h" #include "ItemsList.h" +#include "Macro.h" +#include "Utils/StringUtils.h" void testsPerform() { Serial.println("====some tests section===="); - + + //String str = "Geeks for Geeks "; + // + //Serial.println(itemsCount2(str, " ")); + Serial.println("==========end============"); } \ No newline at end of file diff --git a/src/Utils/FileUtils.cpp b/src/Utils/FileUtils.cpp index dd8b8a0e..02aa9d9c 100644 --- a/src/Utils/FileUtils.cpp +++ b/src/Utils/FileUtils.cpp @@ -1,9 +1,8 @@ -#include "FileSystem.h" - #include "Utils/FileUtils.h" -#include "Utils\SerialPrint.h" -#include "Utils/StringUtils.h" +#include "FileSystem.h" +#include "Utils/StringUtils.h" +#include "Utils\SerialPrint.h" const String filepath(const String& filename) { return filename.startsWith("/") ? filename : "/" + filename; @@ -22,10 +21,10 @@ void removeFile(const String& filename) { String path = filepath(filename); if (FileFS.exists(path)) { if (!FileFS.remove(path)) { - SerialPrint("I","Files","remove " + path); + SerialPrint("I", "Files", "remove " + path); } } else { - SerialPrint("E","Files","not exist" + path); + SerialPrint("E", "Files", "not exist" + path); } } @@ -33,7 +32,7 @@ File seekFile(const String& filename, size_t position) { String path = filepath(filename); auto file = FileFS.open(path, "r"); if (!file) { - SerialPrint("[E]","Files","open " + path); + SerialPrint("[E]", "Files", "open " + path); } // поставим курсор в начало файла file.seek(position, SeekSet); @@ -79,14 +78,14 @@ const String addFile(const String& filename, const String& str) { bool copyFile(const String& src, const String& dst, bool overwrite) { String srcPath = filepath(src); String dstPath = filepath(dst); - SerialPrint("I","Files","copy " + srcPath + " to " + dstPath); + SerialPrint("I", "Files", "copy " + srcPath + " to " + dstPath); if (!FileFS.exists(srcPath)) { - SerialPrint("[E]","Files","not exist: " + srcPath); + SerialPrint("[E]", "Files", "not exist: " + srcPath); return false; } if (FileFS.exists(dstPath)) { if (!overwrite) { - SerialPrint("[E]","Files","already exist: " + dstPath); + SerialPrint("[E]", "Files", "already exist: " + dstPath); return false; } FileFS.remove(dstPath); @@ -131,6 +130,22 @@ const String readFile(const String& filename, size_t max_size) { return temp; } +const String readFileSz(const String& filename, size_t max_size, size_t& size) { + String path = filepath(filename); + auto file = FileFS.open(path, "r"); + if (!file) { + return "failed"; + } + size = file.size(); + if (size > max_size) { + file.close(); + return "large"; + } + String temp = file.readString(); + file.close(); + return temp; +} + const String getFileSize(const String filename) { String filepath(filename); auto file = FileFS.open(filepath, "r"); diff --git a/src/Utils/StringUtils.cpp b/src/Utils/StringUtils.cpp index a158fa94..765424d8 100644 --- a/src/Utils/StringUtils.cpp +++ b/src/Utils/StringUtils.cpp @@ -1,4 +1,5 @@ #include "Utils/StringUtils.h" + #include "Consts.h" String selectToMarkerLast(String str, String found) { @@ -75,7 +76,7 @@ uint16_t hexStringToUint16(String hex) { } } -size_t itemsCount(String str, const String& separator) { +size_t itemsCount2(String& str, const String& separator) { // если строки поиск нет сразу выход if (str.indexOf(separator) == -1) { return 0; @@ -91,6 +92,19 @@ size_t itemsCount(String str, const String& separator) { return cnt; } +size_t itemsCount(String& str, const char* delim) { + size_t cnt = 0; + char* cstr = new char[str.length() + 1]; + strcpy(cstr, str.c_str()); + char* token; + while ((token = strtok_r(cstr, delim, &cstr))) { + cnt++; + //printf("%s\n", token); + } + delete[] cstr; + return cnt; +} + boolean isDigitStr(const String& str) { for (size_t i = 0; i < str.length(); i++) { if (!isDigit(str.charAt(i))) { @@ -110,6 +124,3 @@ String prettyBytes(size_t size) { else return String(size / 1024.0 / 1024.0 / 1024.0) + "GB"; } - - - diff --git a/src/items/vLogging.cpp b/src/items/vLogging.cpp index 49b23c26..e6b999f4 100644 --- a/src/items/vLogging.cpp +++ b/src/items/vLogging.cpp @@ -36,7 +36,7 @@ void LoggingClass::execute(String keyOrValue) { } else { SerialPrint("E", "Logging", "This value not found on this device"); } - } else { //прилетело из события + } else { //прилетело из события if (isDigitStr(keyOrValue) || keyOrValue.indexOf(".") != -1) { //если это число или дробное число loggingValue = keyOrValue; } else { //если это ключ @@ -49,22 +49,33 @@ void LoggingClass::execute(String keyOrValue) { } String filename = "logs/" + _key + ".txt"; - String logData = readFile(filename, 5120); - size_t lines_cnt = itemsCount(logData, "\r\n"); + size_t sz = 0; - SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " (" + String(lines_cnt, DEC) + ")"); + String logData = readFileSz(filename, 10240, sz); + + size_t lines_cnt = itemsCount2(logData, "\r\n"); + + SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " lines " + String(lines_cnt, DEC) + ", size " + String(sz) + ", heap " + ESP.getFreeHeap()); + + if (logData == "large") { + SerialPrint("E", "Logging", "File is very large"); + } if ((lines_cnt > _maxPoints + 1) || !lines_cnt) { removeFile(filename); lines_cnt = 0; + SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(lines_cnt) + ">" + String(_maxPoints)); } if (loggingValue != "") { if (lines_cnt > _maxPoints) { //удаляем старую строку и добавляем новую + //for (int i = 0; i < 5; i++) { logData = deleteBeforeDelimiter(logData, "\r\n"); + //} if (timeNow->hasTimeSynced()) { logData += timeNow->getTimeUnix() + " " + loggingValue + "\r\n"; + writeFile(filename, logData); } } else { //просто добавляем новую строку @@ -73,6 +84,7 @@ void LoggingClass::execute(String keyOrValue) { } } } + String buf = "{}"; jsonWriteInt(buf, "x", timeNow->getTimeUnix().toInt()); jsonWriteFloat(buf, "y1", loggingValue.toFloat()); @@ -123,7 +135,7 @@ void choose_log_date_and_send() { } } -void sendLogData(String file, String topic) { +void sendLogData2(String file, String topic) { String log_date = readFile(file, 5120); if (log_date != "failed") { log_date.replace("\r\n", "\n"); @@ -156,6 +168,46 @@ void sendLogData(String file, String topic) { } } +void sendLogData(String file, String topic) { + File configFile = FileFS.open(file, "r"); + if (!configFile) { + return; + } + configFile.seek(0, SeekSet); + int i = 0; + String buf = "{}"; + String json_array; + String unix_time; + String value; + unsigned int psn; + unsigned int sz = configFile.size(); + do { + i++; + psn = configFile.position(); + String line = configFile.readStringUntil('\n'); + unix_time = selectToMarker(line, " "); + jsonWriteInt(buf, "x", unix_time.toInt()); + value = deleteBeforeDelimiter(line, " "); + jsonWriteFloat(buf, "y1", value.toFloat()); + if (unix_time != "" || value != "") { + json_array += buf + ","; + } + if (i >= 100) { + json_array = "{\"status\":[" + json_array + "]}"; + json_array.replace("},]}", "}]}"); + publishChart(topic, json_array); + json_array = ""; + i = 0; + } + } while (psn < sz); + + configFile.close(); + + json_array = "{\"status\":[" + json_array + "]}"; + json_array.replace("},]}", "}]}"); + publishChart(topic, json_array); +} + void cleanLogAndData() { #ifdef ESP8266 auto dir = FileFS.openDir("logs");