попытка использовать альтнрнативный метод поиска

This commit is contained in:
Dmitry Borisenko
2021-01-02 03:26:16 +01:00
parent 0fa7876b80
commit e39806e0ac
8 changed files with 117 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include <Arduino.h>
#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);
/*
* Размер файла
*/

View File

@@ -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&);

View File

@@ -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();

View File

@@ -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.*

View File

@@ -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============");
}

View File

@@ -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");

View File

@@ -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";
}

View File

@@ -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");