diff --git a/include/MqttClient.h b/include/MqttClient.h index 3ea22eb6..f2ae5830 100644 --- a/include/MqttClient.h +++ b/include/MqttClient.h @@ -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); diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index 168feef2..0feeab5c 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -24,6 +24,8 @@ class IoTItem { void regEvent(float value, String consoleInfo); String getSubtype(); + void sendChart(); + String getID(); virtual String getValue(); diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index f1e1baf4..6fee5c8a 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -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::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) { diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index 04af9e51..027ad48d 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -123,11 +123,12 @@ void IoTItem::doByInterval() {} IoTValue IoTItem::execute(String command, std::vector& param) { return {}; } -//==========================всякая херня============================================================================================== -//захрена эта хрень? -// String IoTItem::getSubtype() { -// return _subtype; -//} +//захрена эта хрень? - самому пригодилась сорян Илья +String IoTItem::getSubtype() { + return _subtype; +} + +void IoTItem::sendChart() {} String IoTItem::getID() { return _id; diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index 4ee31fe8..5677d056 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -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);