оформление функционала в класс логирования

This commit is contained in:
Dmitry Borisenko
2022-08-25 15:46:19 +02:00
parent 71c4fb5651
commit 78760f6507
5 changed files with 77 additions and 74 deletions

View File

@@ -31,8 +31,6 @@ void publishState();
void mqttCallback(char* topic, uint8_t* payload, size_t length); void mqttCallback(char* topic, uint8_t* payload, size_t length);
void handleMqttStatus(bool send); void handleMqttStatus(bool send);
void handleMqttStatus(bool send, int state); void handleMqttStatus(bool send, int state);
void sendAllFilesToMQTT();
void createOneSingleJson(String& json_array, String file);
const String getStateStr(int e); const String getStateStr(int e);

View File

@@ -24,6 +24,8 @@ class IoTItem {
void regEvent(float value, String consoleInfo); void regEvent(float value, String consoleInfo);
String getSubtype(); String getSubtype();
void sendChart();
String getID(); String getID();
virtual String getValue(); virtual String getValue();

View File

@@ -1,5 +1,4 @@
#include "MqttClient.h" #include "MqttClient.h"
#include "NTP.h"
void mqttInit() { void mqttInit() {
mqtt.setCallback(mqttCallback); mqtt.setCallback(mqttCallback);
@@ -135,7 +134,11 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
publishWidgets(); publishWidgets();
publishState(); publishState();
sendAllFilesToMQTT(); for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "Loging") {
(*it)->sendChart();
}
}
#ifdef GATE_MODE #ifdef GATE_MODE
publishTimes(); publishTimes();
@@ -313,71 +316,6 @@ void handleMqttStatus(bool send, int state) {
} }
// log-20384820.txt // log-20384820.txt
void sendAllFilesToMQTT() {
String directory = "logs";
SerialPrint("I", "Loging", "in directory '" + directory + "' files:");
auto dir = FileFS.openDir(directory);
String json_array;
String reqUnixTimeStr = "25.08.2022";
unsigned long reqUnixTime = strDateToUnix(reqUnixTimeStr);
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) {
Serial.print(", matched!");
createOneSingleJson(json_array, "/logs/" + fname);
}
} else {
SerialPrint("i", "Loging", "file '" + fname + "' not used, deleted");
removeFile(directory + "/" + fname);
}
}
Serial.println("final: ");
Serial.println(json_array);
//json_array = "{\"status\":[" + json_array + "]}";
//json_array.replace("},]}", "}]}");
//publishChart(topic, 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);
Serial.println(file);
Serial.println(json_array);
configFile.close();
}
const String getStateStr(int e) { const String getStateStr(int e) {
switch (e) { switch (e) {

View File

@@ -123,11 +123,12 @@ void IoTItem::doByInterval() {}
IoTValue IoTItem::execute(String command, std::vector<IoTValue>& param) { return {}; } IoTValue IoTItem::execute(String command, std::vector<IoTValue>& param) { return {}; }
//==========================всякая херня============================================================================================== //захрена эта хрень? - самому пригодилась сорян Илья
//захрена эта хрень? String IoTItem::getSubtype() {
// String IoTItem::getSubtype() { return _subtype;
// return _subtype; }
//}
void IoTItem::sendChart() {}
String IoTItem::getID() { String IoTItem::getID() {
return _id; return _id;

View File

@@ -1,5 +1,6 @@
#include "Global.h" #include "Global.h"
#include "classes/IoTItem.h" #include "classes/IoTItem.h"
#include "NTP.h"
class Loging : public IoTItem { class Loging : public IoTItem {
private: private:
@@ -65,6 +66,69 @@ class Loging : public IoTItem {
SerialPrint("i", F("Loging"), "file created http://" + WiFi.localIP().toString() + fileName); SerialPrint("i", F("Loging"), "file created http://" + WiFi.localIP().toString() + fileName);
} }
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) {
Serial.print(", matched!");
createOneSingleJson(json_array, "/logs/" + fname);
}
} else {
SerialPrint("i", "Loging", "file '" + fname + "' not used, deleted");
removeFile(directory + "/" + fname);
}
}
Serial.println("final: ");
Serial.println(json_array);
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
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();
}
void cleanLog() { void cleanLog() {
String directory = "logs/" + id; String directory = "logs/" + id;
auto dir = FileFS.openDir(directory); auto dir = FileFS.openDir(directory);