2022-08-13 12:18:58 +02:00
|
|
|
|
#include "Global.h"
|
|
|
|
|
|
#include "classes/IoTItem.h"
|
2022-08-25 15:46:19 +02:00
|
|
|
|
#include "NTP.h"
|
2022-08-13 12:18:58 +02:00
|
|
|
|
|
|
|
|
|
|
class Loging : public IoTItem {
|
|
|
|
|
|
private:
|
|
|
|
|
|
String logval;
|
2022-08-25 00:45:17 +02:00
|
|
|
|
String id;
|
2022-08-25 15:15:06 +02:00
|
|
|
|
int points;
|
2022-08-25 00:45:17 +02:00
|
|
|
|
String fileName = "";
|
2022-08-13 12:18:58 +02:00
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
Loging(String parameters) : IoTItem(parameters) {
|
|
|
|
|
|
jsonRead(parameters, F("logid"), logval);
|
2022-08-25 00:45:17 +02:00
|
|
|
|
jsonRead(parameters, F("id"), id);
|
2022-08-25 15:15:06 +02:00
|
|
|
|
jsonRead(parameters, F("points"), points);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setValue(IoTValue Value) {
|
|
|
|
|
|
value = Value;
|
|
|
|
|
|
regEvent((String)(int)value.valD, "VButton");
|
2022-08-13 12:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-25 01:52:58 +02:00
|
|
|
|
String getValue() {
|
|
|
|
|
|
return "";
|
2022-08-25 15:15:06 +02:00
|
|
|
|
// return (String)(int)value.valD;
|
2022-08-25 01:52:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-13 12:18:58 +02:00
|
|
|
|
void doByInterval() {
|
|
|
|
|
|
String value = getItemValue(logval);
|
2022-08-25 00:45:17 +02:00
|
|
|
|
size_t lines = 0;
|
|
|
|
|
|
//если задано не правильное logid величины для логгирования
|
2022-08-13 12:18:58 +02:00
|
|
|
|
if (value == "") {
|
2022-08-25 00:45:17 +02:00
|
|
|
|
SerialPrint("E", F("Loging"), F("no value set"));
|
2022-08-13 12:18:58 +02:00
|
|
|
|
} else {
|
2022-08-25 00:45:17 +02:00
|
|
|
|
//если время было получено из интернета
|
2022-08-24 00:59:44 +02:00
|
|
|
|
if (isTimeSynch) {
|
2022-08-25 15:15:06 +02:00
|
|
|
|
// regEvent(value, "Loging");
|
2022-08-25 00:45:17 +02:00
|
|
|
|
String logData = String(unixTimeShort) + " " + value;
|
|
|
|
|
|
|
|
|
|
|
|
static bool firstTime = true;
|
|
|
|
|
|
//если зашли сюда первый раз то создаем файл с именем id-timestamp
|
|
|
|
|
|
if (firstTime) {
|
|
|
|
|
|
firstTime = false;
|
|
|
|
|
|
createFileWithIdDatatimeName(logData);
|
|
|
|
|
|
//если 2 ой раз и последующие разы
|
|
|
|
|
|
} else {
|
|
|
|
|
|
lines = countLines(fileName);
|
|
|
|
|
|
if (lines <= 255) {
|
|
|
|
|
|
addFileLn(fileName, logData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
createFileWithIdDatatimeName(logData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
SerialPrint("i", F("Loging"), "loging in file (" + String(lines) + ") http://" + WiFi.localIP().toString() + fileName);
|
|
|
|
|
|
|
2022-08-24 00:59:44 +02:00
|
|
|
|
} else {
|
2022-08-25 00:45:17 +02:00
|
|
|
|
SerialPrint("E", F("Loging"), F("Сant loging - time not synchronized"));
|
2022-08-24 00:59:44 +02:00
|
|
|
|
}
|
2022-08-13 12:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-25 00:45:17 +02:00
|
|
|
|
|
|
|
|
|
|
void createFileWithIdDatatimeName(String &logData) {
|
|
|
|
|
|
fileName = "/logs/" + id + "-" + String(unixTimeShort) + ".txt";
|
|
|
|
|
|
addFileLn(fileName, logData);
|
|
|
|
|
|
SerialPrint("i", F("Loging"), "file created http://" + WiFi.localIP().toString() + fileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-25 15:46:19 +02:00
|
|
|
|
void sendChart() {
|
|
|
|
|
|
String reqUnixTimeStr = "25.08.2022"; //нужно получить эту дату из окна ввода под графиком.
|
|
|
|
|
|
unsigned long reqUnixTime = strDateToUnix(reqUnixTimeStr);
|
|
|
|
|
|
|
|
|
|
|
|
String directory = "logs";
|
|
|
|
|
|
SerialPrint("I", "Loging", "in directory '" + directory + "' files:");
|
|
|
|
|
|
auto dir = FileFS.openDir(directory);
|
|
|
|
|
|
String json_array;
|
|
|
|
|
|
|
|
|
|
|
|
while (dir.next()) {
|
|
|
|
|
|
String fname = dir.fileName();
|
|
|
|
|
|
String id = selectToMarker(fname, "-");
|
|
|
|
|
|
unsigned long fileUnixTime = deleteBeforeDelimiter(deleteToMarkerLast(fname, "."), "-").toInt() + START_DATETIME;
|
|
|
|
|
|
|
|
|
|
|
|
SerialPrint("I", "Loging", "found file '" + fname + "'");
|
|
|
|
|
|
|
|
|
|
|
|
if (isItemExist(id)) {
|
|
|
|
|
|
//выбираем только те файлы которые входят в выбранные сутки
|
|
|
|
|
|
if (fileUnixTime > reqUnixTime && fileUnixTime < reqUnixTime + 86400) {
|
2022-08-25 17:33:07 +02:00
|
|
|
|
Serial.println("matched!");
|
2022-08-25 15:46:19 +02:00
|
|
|
|
createOneSingleJson(json_array, "/logs/" + fname);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
SerialPrint("i", "Loging", "file '" + fname + "' not used, deleted");
|
|
|
|
|
|
removeFile(directory + "/" + fname);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
json_array = "{\"status\":[" + json_array + "]}";
|
|
|
|
|
|
json_array.replace("},]}", "}]}");
|
2022-08-25 17:33:07 +02:00
|
|
|
|
Serial.println("final: ");
|
|
|
|
|
|
Serial.println(json_array);
|
2022-08-25 15:46:19 +02:00
|
|
|
|
publishChart(id, json_array);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void createOneSingleJson(String &json_array, String file) {
|
|
|
|
|
|
File configFile = FileFS.open(file, "r");
|
|
|
|
|
|
if (!configFile) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
configFile.seek(0, SeekSet);
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
String buf = "{}";
|
|
|
|
|
|
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() + START_DATETIME);
|
|
|
|
|
|
value = deleteBeforeDelimiter(line, " ");
|
|
|
|
|
|
jsonWriteFloat(buf, "y1", value.toFloat());
|
|
|
|
|
|
if (unix_time != "" || value != "") {
|
|
|
|
|
|
json_array += buf + ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} while (psn < sz);
|
|
|
|
|
|
|
|
|
|
|
|
configFile.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-25 00:45:17 +02:00
|
|
|
|
void cleanLog() {
|
|
|
|
|
|
String directory = "logs/" + id;
|
|
|
|
|
|
auto dir = FileFS.openDir(directory);
|
|
|
|
|
|
while (dir.next()) {
|
|
|
|
|
|
String fname = dir.fileName();
|
|
|
|
|
|
removeFile(directory + "/" + fname);
|
|
|
|
|
|
SerialPrint("I", "Loging", fname + " deleted");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-13 12:18:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-08-25 00:45:17 +02:00
|
|
|
|
void *getAPI_Loging(String subtype, String param) {
|
2022-08-13 12:18:58 +02:00
|
|
|
|
if (subtype == F("Loging")) {
|
|
|
|
|
|
return new Loging(param);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-25 17:33:07 +02:00
|
|
|
|
|
|
|
|
|
|
class fileDB {
|
|
|
|
|
|
private:
|
|
|
|
|
|
public:
|
|
|
|
|
|
fileDB() {
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|