mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
оформление функционала в класс логирования
This commit is contained in:
@@ -31,8 +31,6 @@ void publishState();
|
||||
void mqttCallback(char* topic, uint8_t* payload, size_t length);
|
||||
void handleMqttStatus(bool send);
|
||||
void handleMqttStatus(bool send, int state);
|
||||
void sendAllFilesToMQTT();
|
||||
void createOneSingleJson(String& json_array, String file);
|
||||
|
||||
const String getStateStr(int e);
|
||||
|
||||
|
||||
@@ -24,6 +24,8 @@ class IoTItem {
|
||||
void regEvent(float value, String consoleInfo);
|
||||
|
||||
String getSubtype();
|
||||
void sendChart();
|
||||
|
||||
String getID();
|
||||
virtual String getValue();
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "MqttClient.h"
|
||||
#include "NTP.h"
|
||||
|
||||
void mqttInit() {
|
||||
mqtt.setCallback(mqttCallback);
|
||||
@@ -135,7 +134,11 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
publishWidgets();
|
||||
publishState();
|
||||
|
||||
sendAllFilesToMQTT();
|
||||
for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
|
||||
if ((*it)->getSubtype() == "Loging") {
|
||||
(*it)->sendChart();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef GATE_MODE
|
||||
publishTimes();
|
||||
@@ -313,71 +316,6 @@ void handleMqttStatus(bool send, int state) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
switch (e) {
|
||||
|
||||
@@ -123,11 +123,12 @@ void IoTItem::doByInterval() {}
|
||||
|
||||
IoTValue IoTItem::execute(String command, std::vector<IoTValue>& param) { return {}; }
|
||||
|
||||
//==========================всякая херня==============================================================================================
|
||||
//захрена эта хрень?
|
||||
// String IoTItem::getSubtype() {
|
||||
// return _subtype;
|
||||
//}
|
||||
//захрена эта хрень? - самому пригодилась сорян Илья
|
||||
String IoTItem::getSubtype() {
|
||||
return _subtype;
|
||||
}
|
||||
|
||||
void IoTItem::sendChart() {}
|
||||
|
||||
String IoTItem::getID() {
|
||||
return _id;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Global.h"
|
||||
#include "classes/IoTItem.h"
|
||||
#include "NTP.h"
|
||||
|
||||
class Loging : public IoTItem {
|
||||
private:
|
||||
@@ -65,6 +66,69 @@ class Loging : public IoTItem {
|
||||
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() {
|
||||
String directory = "logs/" + id;
|
||||
auto dir = FileFS.openDir(directory);
|
||||
|
||||
Reference in New Issue
Block a user