Files
IoTManager/src/modules/virtual/Logging/Loging.cpp

335 lines
11 KiB
C++
Raw Normal View History

#include "Global.h"
#include "classes/IoTItem.h"
#include "ESPConfiguration.h"
#include "NTP.h"
void *getAPI_Date(String params);
class Loging : public IoTItem {
private:
String logid;
String id;
2022-09-08 00:27:38 +02:00
String filesList = "";
int _publishType = -2;
int _wsNum = -1;
int points;
int keepdays;
IoTItem *dateIoTItem;
String prevDate = "";
bool firstTimeDate = true;
unsigned long interval;
public:
Loging(String parameters) : IoTItem(parameters) {
jsonRead(parameters, F("logid"), logid);
jsonRead(parameters, F("id"), id);
jsonRead(parameters, F("points"), points);
if (points > 300) {
2022-08-30 00:23:06 +02:00
points = 300;
SerialPrint("E", F("Loging"), "'" + id + "' user set more points than allowed, value reset to 300");
}
jsonRead(parameters, F("int"), interval);
2022-09-08 19:21:28 +02:00
interval = interval * 1000 * 60; //приводим к милисекундам
jsonRead(parameters, F("keepdays"), keepdays);
//создадим экземпляр класса даты
dateIoTItem = (IoTItem *)getAPI_Date("{\"id\": \"" + id + "-date\",\"int\":\"20\"}");
IoTItems.push_back(dateIoTItem);
2022-09-11 10:39:51 +02:00
SerialPrint("E", F("Loging"), "created date instance " + id);
}
void doByInterval() {
//если объект логгирования не был создан
if (!isItemExist(logid)) {
SerialPrint("E", F("Loging"), "'" + id + "' loging object not exist");
return;
}
String value = getItemValue(logid);
//если значение логгирования пустое
if (value == "") {
SerialPrint("E", F("Loging"), "'" + id + "' loging value is empty");
return;
}
//если время не было получено из интернета
if (!isTimeSynch) {
SerialPrint("E", F("Loging"), "'" + id + "' Сant loging - time not synchronized");
return;
}
regEvent(value, F("Loging"));
String logData = String(unixTimeShort) + " " + value;
//прочитаем путь к файлу последнего сохранения
2022-08-27 01:24:59 +02:00
String filePath = readDataDB(id);
//если данные о файле отсутствуют, создадим новый
2022-08-30 00:23:06 +02:00
if (filePath == "failed" || filePath == "") {
SerialPrint("E", F("Loging"), "'" + id + "' file path not found");
createNewFileWithData(logData);
2022-08-27 01:32:31 +02:00
return;
2022-09-08 00:27:38 +02:00
} else {
// SerialPrint("i", F("Loging"), "'" + id + "' file path found " + filePath);
}
//считаем количество строк
2022-08-27 01:24:59 +02:00
int lines = countLines(filePath);
SerialPrint("i", F("Loging"), "'" + id + "' " + String(lines) + " lines found in file");
//если количество строк до заданной величины и дата не менялась
if (lines <= points && !hasDayChanged()) {
//просто добавим в существующий файл новые данные
addNewDataToExistingFile(filePath, logData);
//если больше или поменялась дата то создадим следующий файл
} else {
createNewFileWithData(logData);
}
}
void createNewFileWithData(String &logData) {
2022-09-08 00:27:38 +02:00
String path = "/lg/" + id + "/" + String(unixTimeShort) + ".txt"; //создадим путь вида /lg/id/133256622333.txt
2022-08-30 00:23:06 +02:00
addFileLn(path, logData); //запишем файл и данные в него
saveDataDB(id, path); //запишем путь к файлу в базу данных
SerialPrint("i", F("Loging"), "'" + id + "' file created http://" + WiFi.localIP().toString() + path);
}
2022-08-30 00:23:06 +02:00
void addNewDataToExistingFile(String &path, String &logData) {
addFileLn(path, logData);
SerialPrint("i", F("Loging"), "'" + id + "' loging in file http://" + WiFi.localIP().toString() + path);
}
bool hasDayChanged() {
bool changed = false;
String currentDate = getDateDotFormated();
if (!firstTimeDate) {
if (prevDate != currentDate) {
changed = true;
SerialPrint("i", F("NTP"), "Change day event");
}
}
firstTimeDate = false;
prevDate = currentDate;
return changed;
}
void sendChart() {
2022-09-14 18:06:40 +02:00
String dir = "lg/" + id;
filesList = getFilesList(dir);
2022-09-08 00:27:38 +02:00
int f = 0;
2022-08-30 00:23:06 +02:00
bool noData = true;
2022-09-08 00:27:38 +02:00
while (filesList.length()) {
String buf = selectToMarker(filesList, ";");
f++;
int i = 0;
2022-09-08 19:21:28 +02:00
unsigned long fileUnixTimeGMT = selectToMarkerLast(deleteToMarkerLast(buf, "."), "/").toInt() + START_DATETIME;
unsigned long fileUnixTimeLocal = gmtTimeToLocal(fileUnixTimeGMT);
2022-09-08 00:27:38 +02:00
2022-09-08 19:21:28 +02:00
//удаление старых файлов
// 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;
createJson(buf, i);
SerialPrint("i", F("Loging"), String(f) + ") " + buf + ", " + String(i) + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", sent");
2022-09-08 19:21:28 +02:00
} else {
SerialPrint("i", F("Loging"), String(f) + ") " + buf + ", nil, " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", skipped");
2022-09-08 19:21:28 +02:00
}
//}
2022-09-08 19:21:28 +02:00
2022-09-08 00:27:38 +02:00
filesList = deleteBeforeDelimiter(filesList, ";");
2022-08-30 00:23:06 +02:00
}
//если данных нет отправляем пустой грфик
if (noData) {
cleanChart();
}
2022-09-08 00:27:38 +02:00
}
void cleanChart() {
SerialPrint("i", F("Loging"), "clear chart");
String cleanJson = createEmtyJson();
publishJson(cleanJson);
}
2022-09-08 00:27:38 +02:00
void cleanData() {
2022-09-14 18:06:40 +02:00
String dir = "lg/" + id;
filesList = getFilesList(dir);
2022-09-08 00:27:38 +02:00
int i = 0;
while (filesList.length()) {
String buf = selectToMarker(filesList, ";");
i++;
removeFile(buf);
SerialPrint("i", "Files", String(i) + ") " + buf + " => deleted");
filesList = deleteBeforeDelimiter(filesList, ";");
2022-08-30 00:23:06 +02:00
}
2022-09-08 00:27:38 +02:00
}
void createJson(String file, int &i) {
2022-09-08 00:27:38 +02:00
File configFile = FileFS.open(file, "r");
if (!configFile) {
SerialPrint("E", F("Loging"), "'" + id + "' open file error");
return;
2022-08-30 00:23:06 +02:00
}
2022-09-08 00:27:38 +02:00
configFile.seek(0, SeekSet);
String buf = "{}";
String oneSingleJson;
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 != "") {
oneSingleJson += buf + ",";
}
} while (psn < sz);
configFile.close();
String topic = mqttRootDevice + "/" + id;
oneSingleJson = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + oneSingleJson + "]}";
oneSingleJson.replace("},]}", "}]}");
publishJson(oneSingleJson);
}
// publishType 1 - в mqtt, 2 - в ws, 3 - mqtt и ws
// wsNum = -1 => broadcast
void setPublishType(int publishType, int wsNum) {
_publishType = publishType;
_wsNum = wsNum;
2022-09-08 00:27:38 +02:00
}
void publishJson(String &oneSingleJson) {
if (_publishType == 1) {
publishChartMqtt(id, oneSingleJson);
} else if (_publishType == 2) {
publishChartWs(_wsNum, oneSingleJson);
} else if (_publishType == 3) {
publishChartMqtt(id, oneSingleJson);
publishChartWs(_wsNum, oneSingleJson);
} else {
SerialPrint("E", F("Loging"), "wrong publishType");
2022-08-30 00:23:06 +02:00
}
}
2022-08-30 00:23:06 +02:00
String getValue() {
return "";
}
void loop() {
if (enableDoByInt) {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (difference >= interval) {
prevMillis = millis();
this->doByInterval();
}
}
}
void regEvent(String value, String consoleInfo = "") {
String userDate = getItemValue(id + "-date");
String currentDate = getDateDotFormated();
//отправляем в график данные только когда выбран сегодняшний день
if (userDate == currentDate) {
generateEvent(_id, value);
publishStatusMqtt(_id, value);
String json = createSingleJson(_id, value);
publishChartWs(-1, json);
SerialPrint("i", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'");
}
}
String createSingleJson(String id, String value) {
String topic = mqttRootDevice + "/" + _id;
return "{\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}";
}
String createEmtyJson() {
String topic = mqttRootDevice + "/" + _id;
return "{\"topic\":\"" + topic + "\",\"status\":[],\"maxCount\":\"0\"}";
}
//просто максимальное количество точек
2022-09-08 00:27:38 +02:00
int calculateMaxCount() {
return 86400;
2022-09-08 00:27:38 +02:00
}
};
void *getAPI_Loging(String subtype, String param) {
if (subtype == F("Loging")) {
return new Loging(param);
} else {
return nullptr;
}
}
class Date : public IoTItem {
private:
bool firstTime = true;
public:
String id;
Date(String parameters) : IoTItem(parameters) {
jsonRead(parameters, F("id"), id);
value.isDecimal = false;
}
void setValue(String valStr) {
value.valS = valStr;
setValue(value);
}
void setValue(IoTValue Value) {
value = Value;
regEvent(value.valS, "");
//отправка данных при изменении даты
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "Loging") {
//отправляем только свои данные
if ((*it)->getID() == selectToMarker(id, "-")) {
(*it)->setPublishType(3, -1);
(*it)->cleanChart();
(*it)->sendChart();
}
}
}
}
void doByInterval() {
if (isTimeSynch) {
if (firstTime) {
setValue(getDateDotFormated());
SerialPrint("E", F("Loging"), "today date set " + getDateDotFormated());
firstTime = false;
}
}
}
};
void *getAPI_Date(String param) {
return new Date(param);
}