From d947e9da715f0826e5d601bafa9d5623d44f15fa Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Wed, 31 Aug 2022 00:13:45 +0200 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=84=D0=B8=D0=BA=D0=BE=D0=B2=20=D0=B2=20=D0=B2=D0=B5?= =?UTF-8?q?=D0=B1=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Const.h | 2 +- include/WsServer.h | 1 + include/classes/IoTItem.h | 2 +- include/utils/FileUtils.h | 1 + src/EspFileSystem.cpp | 2 +- src/MqttClient.cpp | 3 ++- src/WsServer.cpp | 21 ++++++++++++++++----- src/classes/IoTItem.cpp | 2 +- src/modules/virtual/Logging/Loging.cpp | 19 +++++++++++-------- src/utils/FileUtils.cpp | 13 +++++++++++-- 10 files changed, 46 insertions(+), 20 deletions(-) diff --git a/include/Const.h b/include/Const.h index dad350c6..0e371f6c 100644 --- a/include/Const.h +++ b/include/Const.h @@ -1,7 +1,7 @@ #pragma once //Версия прошивки -#define FIRMWARE_VERSION 413 +#define FIRMWARE_VERSION 414 #ifdef esp8266_4mb #define FIRMWARE_NAME "esp8266_4mb" diff --git a/include/WsServer.h b/include/WsServer.h index d32c2cb0..d09d5627 100644 --- a/include/WsServer.h +++ b/include/WsServer.h @@ -15,6 +15,7 @@ extern void hexdump(const void* mem, uint32_t len, uint8_t cols); void sendFileToWs(const char* filename, uint8_t num, size_t frameSize); void publishStatusWs(const String& topic, const String& data); +void publishStatusWsJson(const String& topic, String& json); void periodicWsSend(); void sendStringToWs(const String& msg, uint8_t num, String name); diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index 4c29f7e7..cb8d7279 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -24,7 +24,7 @@ class IoTItem { void regEvent(float value, String consoleInfo); String getSubtype(); - virtual void sendChart(); + virtual void sendChart(bool mqtt); String getID(); virtual String getValue(); diff --git a/include/utils/FileUtils.h b/include/utils/FileUtils.h index 6a528fe6..cbf81c9b 100644 --- a/include/utils/FileUtils.h +++ b/include/utils/FileUtils.h @@ -13,6 +13,7 @@ extern bool cutFile(const String& src, const String& dst); extern size_t countLines(const String filename); void removeFile(const String& filename); void cleanDirectory(String path); +void cleanLogs(); void saveDataDB(String id, String data); String readDataDB(String id); extern void onFlashWrite(); diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index 768e8c94..556c2aa4 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -25,7 +25,7 @@ void globalVarsSync() { String getParamsJson() { String json; - serializeJson(*getLocalItemsAsJSON(), json); // Ilya, data: "1.00" (analog sensor, round set to 1, should be "1.0") + serializeJson(*getLocalItemsAsJSON(), json); jsonWriteStr_(json, "params", ""); return json; } diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 6fee5c8a..1741e1c3 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -134,9 +134,10 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { publishWidgets(); publishState(); + //отправка данных графиков for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { if ((*it)->getSubtype() == "Loging") { - (*it)->sendChart(); + (*it)->sendChart(true); } } diff --git a/src/WsServer.cpp b/src/WsServer.cpp index 40ba0d07..b569d9c7 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -51,11 +51,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) } // Страница веб интерфейса dashboard=================================================================== - // отправляем только файл layout.json // if (headerStr == "/|") { + sendFileToWs("/layout.json", num, 1024); String json = getParamsJson(); standWebSocket.sendTXT(num, json); - sendFileToWs("/layout.json", num, 1024); + //отправка данных графиков + for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { + if ((*it)->getSubtype() == "Loging") { + (*it)->sendChart(false); + } + } } // Страница веб интерфейса configutation================================================================ //========отправка=========================================================// @@ -150,8 +155,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) } //команда очистки всех логов esp// if (headerStr == "/clean|") { - cleanDirectory("lg"); - cleanDirectory("db"); + cleanLogs(); } //Прием сообщений cotrol ============================================================================== @@ -203,13 +207,20 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) //публикация статус сообщений void publishStatusWs(const String& topic, const String& data) { - String path = mqttRootDevice + "/" + topic; //+ "/status"; + String path = mqttRootDevice + "/" + topic; String json = "{}"; jsonWriteStr(json, "status", data); jsonWriteStr(json, "topic", path); standWebSocket.broadcastTXT(json); } +//публикация статус сообщений уже готовых +void publishStatusWsJson(const String& topic, String& json) { + String path = mqttRootDevice + "/" + topic; + jsonWriteStr(json, "topic", path); + standWebSocket.broadcastTXT(json); +} + //данные которые мы отправляем в сокеты переодически void periodicWsSend() { standWebSocket.broadcastTXT(devListHeapJson); diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index 027ad48d..97869c0e 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -128,7 +128,7 @@ String IoTItem::getSubtype() { return _subtype; } -void IoTItem::sendChart() {} +void IoTItem::sendChart(bool mqtt) {} String IoTItem::getID() { return _id; diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index b3a603b2..83fd9f52 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -17,7 +17,7 @@ class Loging : public IoTItem { jsonRead(parameters, F("logid"), logid); jsonRead(parameters, F("id"), id); jsonRead(parameters, F("points"), points); - if (points >= 300) { + if (points > 300) { points = 300; SerialPrint("E", F("Loging"), "'" + id + "' user set more points than allowed, value reset to 300"); } @@ -91,7 +91,7 @@ class Loging : public IoTItem { SerialPrint("i", F("Loging"), "'" + id + "' loging in file http://" + WiFi.localIP().toString() + path); } - void sendChart() { + void sendChart(bool mqtt) { SerialPrint("i", F("Loging"), "'" + id + "'----------------------------"); String reqUnixTimeStr = "27.08.2022"; //нужно получить эту дату из окна ввода под графиком. unsigned long reqUnixTime = strDateToUnix(reqUnixTimeStr); @@ -124,7 +124,7 @@ class Loging : public IoTItem { // if (fileUnixTime > reqUnixTime && fileUnixTime < reqUnixTime + 86400) { SerialPrint("i", F("Loging"), "'" + id + "' matching file found '" + fname + "'"); //выгрузка по частям, по одному файлу - publishJsonPartly("/lg/" + fname, calculateMaxCount(), i); + publishJsonPartly("/lg/" + fname, calculateMaxCount(), i, mqtt); //} //удаление старых файлов if ((fileUnixTime + (points * interval)) < (unixTime - (keepdays * 86400))) { @@ -144,7 +144,7 @@ class Loging : public IoTItem { SerialPrint("i", F("Loging"), "'" + id + "'--------------'" + String(i) + "'--------------"); } - void publishJsonPartly(String file, int maxCount, int &i) { + void publishJsonPartly(String file, int maxCount, int &i, bool mqtt) { File configFile = FileFS.open(file, "r"); if (!configFile) { SerialPrint("E", F("Loging"), "'" + id + "' open file error"); @@ -171,14 +171,17 @@ class Loging : public IoTItem { } while (psn < sz); configFile.close(); - publishJson(dividedJson, maxCount); + + publishJson(dividedJson, maxCount, mqtt); } - void publishJson(String & oneSingleJson, int &maxCount) { + void publishJson(String & oneSingleJson, int &maxCount, bool mqtt) { oneSingleJson = "{\"maxCount\":" + String(maxCount) + ",\"status\":[" + oneSingleJson + "]}"; oneSingleJson.replace("},]}", "}]}"); - if (!publishChart(id, oneSingleJson)) { - SerialPrint("E", F("Loging"), "'" + id + "' mqtt publish error"); + if (mqtt) { + publishChart(id, oneSingleJson); + } else { + publishStatusWsJson(id, oneSingleJson); } } diff --git a/src/utils/FileUtils.cpp b/src/utils/FileUtils.cpp index efecfe4e..48515a53 100644 --- a/src/utils/FileUtils.cpp +++ b/src/utils/FileUtils.cpp @@ -197,7 +197,7 @@ void cleanDirectory(String path) { while (dir.next()) { String fname = dir.fileName(); removeFile(path + "/" + fname); - SerialPrint("I", "Files", path + "/" + fname + " => deleted"); + SerialPrint("i", "Files", path + "/" + fname + " => deleted"); } onFlashWrite(); #endif @@ -205,12 +205,16 @@ void cleanDirectory(String path) { path = "/" + path; File root = FileFS.open(path); path = String(); + if (!root) { + SerialPrint("E", "Files", "nothing to delete"); + return; + } if (root.isDirectory()) { File file = root.openNextFile(); while (file) { String fname = file.name(); removeFile(fname); - SerialPrint("I", "Files", fname + " => deleted"); + SerialPrint("i", "Files", fname + " => deleted"); file = root.openNextFile(); } } @@ -227,6 +231,11 @@ String readDataDB(String id) { return readFile(path, 2000); } +void cleanLogs() { + cleanDirectory("lg"); + cleanDirectory("db"); +} + //счетчик количества записей на флешь за сеанс void onFlashWrite() { flashWriteNumber++;