From f90176879fdd7c4a158808f0490cf14aaeead691 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 25 Aug 2022 15:15:06 +0200 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86?= =?UTF-8?q?=D0=B5=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/items.json | 3 ++- include/MqttClient.h | 2 +- include/NTP.h | 1 + src/Main.cpp | 1 + src/MqttClient.cpp | 32 ++++++++++++++++++------ src/NTP.cpp | 21 ++++++++++++++++ src/classes/IoTItem.cpp | 2 +- src/modules/virtual/Logging/Loging.cpp | 10 +++++++- src/modules/virtual/Logging/modinfo.json | 3 ++- 9 files changed, 63 insertions(+), 12 deletions(-) diff --git a/data_svelte/items.json b/data_svelte/items.json index 6668aad2..ffb049c3 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -16,7 +16,8 @@ "descr": "График", "int": 60, "logid": "tmp", - "num": 1 + "num": 1, + "points": 255 }, { "name": "2. Таймер", diff --git a/include/MqttClient.h b/include/MqttClient.h index 7009a6db..3ea22eb6 100644 --- a/include/MqttClient.h +++ b/include/MqttClient.h @@ -32,7 +32,7 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length); void handleMqttStatus(bool send); void handleMqttStatus(bool send, int state); void sendAllFilesToMQTT(); -void sendLogData(String file, String topic); +void createOneSingleJson(String& json_array, String file); const String getStateStr(int e); diff --git a/include/NTP.h b/include/NTP.h index 8ae14633..7bf1f1f2 100644 --- a/include/NTP.h +++ b/include/NTP.h @@ -11,3 +11,4 @@ extern void synchTime(); extern const String getTimeLocal_hhmm(); extern const String getTimeLocal_hhmmss(); extern const String getDateTimeDotFormated(); +extern unsigned long strDateToUnix(String date); diff --git a/src/Main.cpp b/src/Main.cpp index 51ef1bd2..80d09123 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -70,6 +70,7 @@ void setup() { // test Serial.println("-------test start--------"); + Serial.println(strDateToUnix("25.08.2022")); Serial.println("--------test end---------"); // симуляция добавления внешних событий diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 5fc36311..f3985cc2 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -1,4 +1,5 @@ #include "MqttClient.h" +#include "NTP.h" void mqttInit() { mqtt.setCallback(mqttCallback); @@ -316,20 +317,34 @@ 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 '" + String(fileUnixTime) + "'"); + if (isItemExist(id)) { - SerialPrint("I", "Loging", fname); - sendLogData("/logs/" + fname, id); + //выбираем только те файлы которые входят в выбранные сутки + if (fileUnixTime > reqUnixTime && fileUnixTime < reqUnixTime + 86400) { + SerialPrint("I", "Loging", "file sent to MQTT: '" + fname + "'"); + createOneSingleJson(json_array, "/logs/" + fname); + } } else { SerialPrint("i", "Loging", "file '" + fname + "' not used, deleted"); removeFile(directory + "/" + fname); } } + Serial.println("final: "); + Serial.println(json_array); } -void sendLogData(String file, String topic) { +void createOneSingleJson(String& json_array, String file) { File configFile = FileFS.open(file, "r"); if (!configFile) { return; @@ -337,7 +352,6 @@ void sendLogData(String file, String topic) { configFile.seek(0, SeekSet); int i = 0; String buf = "{}"; - String json_array; String unix_time; String value; unsigned int psn; @@ -353,13 +367,17 @@ void sendLogData(String file, String topic) { if (unix_time != "" || value != "") { json_array += buf + ","; } + } while (psn < sz); + Serial.println(file); + Serial.println(json_array); + configFile.close(); - json_array = "{\"status\":[" + json_array + "]}"; - json_array.replace("},]}", "}]}"); - publishChart(topic, json_array); + // json_array = "{\"status\":[" + json_array + "]}"; + // json_array.replace("},]}", "}]}"); + // publishChart(topic, json_array); } const String getStateStr(int e) { diff --git a/src/NTP.cpp b/src/NTP.cpp index 02cea48e..bd1d53fe 100644 --- a/src/NTP.cpp +++ b/src/NTP.cpp @@ -116,3 +116,24 @@ const String getDateTimeDotFormated() { sprintf(buf, "%02d.%02d.%02d %02d:%02d:%02d", _time_local.day_of_month, _time_local.month, _time_local.year, _time_local.hour, _time_local.minute, _time_local.second); return String(buf); } + +unsigned long strDateToUnix(String date) { + int day = selectToMarker(date, ".").toInt(); + date = deleteBeforeDelimiter(date, "."); + int month = selectToMarker(date, ".").toInt(); + date = deleteBeforeDelimiter(date, "."); + int year = selectToMarker(date, ".").toInt(); + int secsInOneDay = 86400; + int daysInOneYear = 365; + int daysInLeepYear = 366; + int numberOfLeepYears = 12; + int totalNormalYears = year - 1970 - numberOfLeepYears; + unsigned int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int numberOfDaysInPastMonths = 0; + for (int i = 0; i <= 11; i++) { + if (i <= month - 2) { + numberOfDaysInPastMonths = numberOfDaysInPastMonths + daysInMonth[i]; + } + } + return (day * secsInOneDay) + (numberOfDaysInPastMonths * secsInOneDay) + (totalNormalYears * daysInOneYear * secsInOneDay) + (numberOfLeepYears * daysInLeepYear * secsInOneDay); +} diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index ed38505b..04af9e51 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -71,7 +71,7 @@ void IoTItem::setValue(String valStr) { setValue(value); } -//непонятный метод к удалению +//установить void IoTItem::setValue(IoTValue Value) { value = Value; if (value.isDecimal) { diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index 1663e55e..4ee31fe8 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -5,16 +5,24 @@ class Loging : public IoTItem { private: String logval; String id; + int points; String fileName = ""; public: Loging(String parameters) : IoTItem(parameters) { jsonRead(parameters, F("logid"), logval); jsonRead(parameters, F("id"), id); + jsonRead(parameters, F("points"), points); + } + + void setValue(IoTValue Value) { + value = Value; + regEvent((String)(int)value.valD, "VButton"); } String getValue() { return ""; + // return (String)(int)value.valD; } void doByInterval() { @@ -26,7 +34,7 @@ class Loging : public IoTItem { } else { //если время было получено из интернета if (isTimeSynch) { - regEvent(value, "Loging"); + // regEvent(value, "Loging"); String logData = String(unixTimeShort) + " " + value; static bool firstTime = true; diff --git a/src/modules/virtual/Logging/modinfo.json b/src/modules/virtual/Logging/modinfo.json index c2703767..694156fb 100644 --- a/src/modules/virtual/Logging/modinfo.json +++ b/src/modules/virtual/Logging/modinfo.json @@ -11,7 +11,8 @@ "descr": "График", "int": 60, "logid": "tmp", - "num": 1 + "num": 1, + "points": 255 } ], "about": {