262 Logging added, working version

This commit is contained in:
Dmitry Borisenko
2020-11-01 04:48:35 +03:00
parent 1126575822
commit d96aea8eef
12 changed files with 105 additions and 215 deletions

View File

@@ -1,7 +1,8 @@
#include "Global.h"
#include "Init.h"
#include "Cmd.h"
#include "Cmd.h"
#include "Global.h"
#include "items/LoggingClass.h"
void loadConfig() {
configSetupJson = readFile("config.json", 4096);
@@ -25,11 +26,14 @@ void all_init() {
}
void Device_init() {
sensorReadingMap10sec = "";
dallasEnterCounter = -1;
if (myLogging != nullptr) {
myLogging->clear();
}
logging_value_names_list = "";
sensorReadingMap10sec = "";
dallasEnterCounter = -1;
//logging_value_names_list = "";
//enter_to_logging_counter = LOG1 - 1;
//analog_value_names_list = "";
//enter_to_analog_counter = 0;
@@ -59,7 +63,6 @@ void Device_init() {
removeFile(String("layout.txt"));
#endif
fileCmdExecute(String(DEVICE_CONFIG_FILE));
//outcoming_date();
}
@@ -79,8 +82,6 @@ void uptime_init() {
nullptr, true);
}
void handle_uptime() {
jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime());
}

View File

@@ -1,150 +0,0 @@
#include "Global.h"
#include <LITTLEFS.h>
void sendLogData(String file, String topic);
#ifdef LOGGING_ENABLED
//===============================================Логирование============================================================
//logging temp1 1 10 Температура Датчики 2
/*void logging2() {
String value_name = sCmd.next();
String period_min = sCmd.next();
String maxCount = sCmd.next();
String widget_name = sCmd.next();
widget_name.replace("#", " ");
String page_name = sCmd.next();
String page_number = sCmd.next();
logging_value_names_list += value_name + ",";
enter_to_logging_counter++; //считаем количество входов в эту функцию
jsonWriteStr(configOptionJson, value_name + "_c", maxCount); //создаем в файловой системе переменную количества точек на графике с отметкой _c что значит count
//создаем график в приложении с топиком _ch /prefix/3234045-1589487/value_name_ch
createChart(widget_name, page_name, page_number, "chart", value_name + "_ch", maxCount);
if (enter_to_logging_counter == LOG1) {
ts.add(
LOG1, period_min.toInt() * 1000 * 60, [&](void*) {
String tmp_buf_1 = selectFromMarkerToMarker(logging_value_names_list, ",", 0);
deleteOldDate("log." + tmp_buf_1 + ".txt", jsonReadInt(configOptionJson, tmp_buf_1 + "_c"), jsonReadStr(configLiveJson, tmp_buf_1));
SerialPrint("I","module","logging for " + tmp_buf_1 + " done");
},
nullptr, false);
}
if (enter_to_logging_counter == LOG2) {
ts.add(
LOG2, period_min.toInt() * 1000 * 60, [&](void*) {
String tmp_buf_2 = selectFromMarkerToMarker(logging_value_names_list, ",", 1);
deleteOldDate("log." + tmp_buf_2 + ".txt", jsonReadInt(configOptionJson, tmp_buf_2 + "_c"), jsonReadStr(configLiveJson, tmp_buf_2));
SerialPrint("I","module","logging for " + tmp_buf_2 + " done");
},
nullptr, false);
}
if (enter_to_logging_counter == LOG3) {
ts.add(
LOG3, period_min.toInt() * 1000 * 60, [&](void*) {
String tmp_buf_3 = selectFromMarkerToMarker(logging_value_names_list, ",", 2);
deleteOldDate("log." + tmp_buf_3 + ".txt", jsonReadInt(configOptionJson, tmp_buf_3 + "_c"), jsonReadStr(configLiveJson, tmp_buf_3));
SerialPrint("I","module","logging for " + tmp_buf_3 + " done");
},
nullptr, false);
}
if (enter_to_logging_counter == LOG4) {
ts.add(
LOG4, period_min.toInt() * 1000 * 60, [&](void*) {
String tmp_buf_4 = selectFromMarkerToMarker(logging_value_names_list, ",", 3);
deleteOldDate("log." + tmp_buf_4 + ".txt", jsonReadInt(configOptionJson, tmp_buf_4 + "_c"), jsonReadStr(configLiveJson, tmp_buf_4));
SerialPrint("I","module","logging for " + tmp_buf_4 + " done");
},
nullptr, false);
}
if (enter_to_logging_counter == LOG5) {
ts.add(
LOG5, period_min.toInt() * 1000 * 60, [&](void*) {
String tmp_buf_5 = selectFromMarkerToMarker(logging_value_names_list, ",", 4);
deleteOldDate("log." + tmp_buf_5 + ".txt", jsonReadInt(configOptionJson, tmp_buf_5 + "_c"), jsonReadStr(configLiveJson, tmp_buf_5));
SerialPrint("I","module","logging for " + tmp_buf_5 + " done");
},
nullptr, false);
}
}*/
/*
* Удаление стрых данных и запись новых
*/
/*void deleteOldDate(const String filename, size_t max_lines_cnt, String payload) {
String log_date = readFile(filename, 5120);
size_t lines_cnt = itemsCount(log_date, "\r\n");
SerialPrint("I","module","log " + filename + " (" + String(lines_cnt, DEC) + ")");
if ((lines_cnt > max_lines_cnt + 1) || !lines_cnt) {
removeFile(filename);
lines_cnt = 0;
}
if (lines_cnt > max_lines_cnt) {
log_date = deleteBeforeDelimiter(log_date, "\r\n");
if (timeNow->hasTimeSynced()) {
log_date += timeNow->getTimeUnix() + " " + payload + "\r\n";
writeFile(filename, log_date);
}
} else {
if (timeNow->hasTimeSynced()) {
addFileLn(filename, timeNow->getTimeUnix() + " " + payload);
}
}
}*/
//=========================================Выбор какие данные отправлять==================================================================
void choose_log_date_and_send() {
String all_line = logging_value_names_list;
while (all_line.length() != 0) {
String tmp = selectToMarker(all_line, ",");
sendLogData("log." + tmp + ".txt", tmp + "_ch");
all_line = deleteBeforeDelimiter(all_line, ",");
}
}
//=========================================Отправка данных===================================================================================
void sendLogData(String file, String topic) {
String log_date = readFile(file, 5120);
if (log_date != "failed") {
log_date.replace("\r\n", "\n");
log_date.replace("\r", "\n");
String buf = "{}";
String json_array;
String unix_time;
String value;
while (log_date.length()) {
String tmp = selectToMarker(log_date, "\n");
log_date = deleteBeforeDelimiter(log_date, "\n");
unix_time = selectToMarker(tmp, " ");
jsonWriteInt(buf, "x", unix_time.toInt());
value = deleteBeforeDelimiter(tmp, " ");
jsonWriteFloat(buf, "y1", value.toFloat());
if (log_date.length() < 3) {
json_array += buf;
} else {
json_array += buf + ",";
}
buf = "{}";
}
unix_time = "";
value = "";
log_date = "";
json_array = "{\"status\":[" + json_array + "]}";
SerialPrint("I","module",json_array);
publishChart(topic, json_array);
}
}
void clean_log_date() {
String all_line = logging_value_names_list;
while (all_line.length()) {
String tmp = selectToMarker(all_line, ",");
removeFile("log." + tmp + ".txt");
all_line = deleteBeforeDelimiter(all_line, ",");
}
}
#endif

View File

@@ -1,7 +1,7 @@
#include "MqttClient.h"
#include <LittleFS.h>
#include "items/LoggingClass.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"

View File

@@ -20,10 +20,10 @@ void removeFile(const String& filename) {
String path = filepath(filename);
if (LittleFS.exists(path)) {
if (!LittleFS.remove(path)) {
SerialPrint("[E]","Files","remove " + path);
SerialPrint("I","Files","remove " + path);
}
} else {
SerialPrint("I","Files","not exist" + path);
SerialPrint("E","Files","not exist" + path);
}
}

View File

@@ -1,5 +1,5 @@
#include "Web.h"
#include "items/LoggingClass.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"

View File

@@ -6,9 +6,10 @@
#include "Global.h"
#include "ItemsCmd.h"
LoggingClass::LoggingClass(unsigned long period, unsigned int maxPoints, String key) {
LoggingClass::LoggingClass(unsigned long period, unsigned int maxPoints, String loggingValueKey, String key) {
_period = period * 1000;
_maxPoints = maxPoints;
_loggingValueKey = loggingValueKey;
_key = key;
}
@@ -19,7 +20,7 @@ void LoggingClass::loop() {
unsigned long difference = currentMillis - prevMillis;
if (difference >= _period) {
prevMillis = millis();
addNewDelOldData("log." + _key + ".txt", _maxPoints, jsonReadStr(configLiveJson, _key));
addNewDelOldData("logs/" + _key + ".txt", _maxPoints, jsonReadStr(configLiveJson, _loggingValueKey));
}
}
@@ -33,7 +34,7 @@ void LoggingClass::addNewDelOldData(const String filename, size_t maxPoints, Str
removeFile(filename);
lines_cnt = 0;
}
if (payload != "") {
if (lines_cnt > maxPoints) {
logData = deleteBeforeDelimiter(logData, "\r\n");
@@ -53,13 +54,67 @@ MyLoggingVector* myLogging = nullptr;
void logging() {
myLineParsing.update();
String value = myLineParsing.gvalue();
String loggingValueKey = myLineParsing.gvalue();
String key = myLineParsing.gkey();
String interv = myLineParsing.gint();
String maxcnt = myLineParsing.gmaxcnt();
myLineParsing.clear();
logging_value_names_list += key + ",";
static bool firstTime = true;
if (firstTime) myLogging = new MyLoggingVector();
firstTime = false;
myLogging->push_back(LoggingClass(interv.toInt(), maxcnt.toInt(), value));
myLogging->push_back(LoggingClass(interv.toInt(), maxcnt.toInt(), loggingValueKey, key));
}
void choose_log_date_and_send() {
String all_line = logging_value_names_list;
while (all_line.length() != 0) {
String tmp = selectToMarker(all_line, ",");
sendLogData("logs/" + tmp + ".txt", tmp);
all_line = deleteBeforeDelimiter(all_line, ",");
}
}
void sendLogData(String file, String topic) {
String log_date = readFile(file, 5120);
if (log_date != "failed") {
log_date.replace("\r\n", "\n");
log_date.replace("\r", "\n");
String buf = "{}";
String json_array;
String unix_time;
String value;
while (log_date.length()) {
String tmp = selectToMarker(log_date, "\n");
log_date = deleteBeforeDelimiter(log_date, "\n");
unix_time = selectToMarker(tmp, " ");
jsonWriteInt(buf, "x", unix_time.toInt());
value = deleteBeforeDelimiter(tmp, " ");
jsonWriteFloat(buf, "y1", value.toFloat());
if (log_date.length() < 3) {
json_array += buf;
} else {
json_array += buf + ",";
}
buf = "{}";
}
unix_time = "";
value = "";
log_date = "";
json_array = "{\"status\":[" + json_array + "]}";
//SerialPrint("I", "module", json_array);
publishChart(topic, json_array);
}
}
void clean_log_date() {
auto dir = LittleFS.openDir("logs");
while (dir.next()) {
String fname = dir.fileName();
SerialPrint("I", "System", fname);
removeFile("logs/" + fname);
}
}

View File

@@ -2,6 +2,7 @@
#include <SSDP.h>
#include "BufferExecute.h"
#include "Bus.h"
#include "Class/CallBackTest.h"
#include "Class/NotAsync.h"
#include "Class/ScenarioClass.h"
@@ -9,13 +10,12 @@
#include "Global.h"
#include "Init.h"
#include "ItemsList.h"
#include "RemoteOrdersUdp.h"
#include "Utils/StatUtils.h"
#include "Utils/Timings.h"
#include "Utils/WebUtils.h"
#include "items/ButtonInClass.h"
#include "items/LoggingClass.h"
#include "RemoteOrdersUdp.h"
#include "Bus.h"
void not_async_actions();
@@ -74,11 +74,11 @@ void setup() {
SerialPrint("I", "Stat", "Stat Init");
initSt();
#ifdef UDP_ENABLED
SerialPrint("I","UDP","Udp Init");
#ifdef UDP_ENABLED
SerialPrint("I", "UDP", "Udp Init");
asyncUdpInit();
#endif
#endif
SerialPrint("I", "Bus", "Bus Init");
busInit();
@@ -99,11 +99,11 @@ void setup() {
just_load = false;
initialized = true; //this second POST makes the data to be processed (you don't need to connect as "keep-alive" for that to work)
//myLogging = new MyLoggingVector();
//myLogging->push_back(LoggingClass(30000, 10, "analog-adc-1"));
//myLogging->push_back(LoggingClass(10000, 1, "10 sec"));
}
void loop() {
if (!initialized) {
return;