отправка данных графиков в веб интерфейс

This commit is contained in:
Dmitry Borisenko
2022-08-31 00:13:45 +02:00
parent 1c5de5b92e
commit d947e9da71
10 changed files with 46 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
//Версия прошивки
#define FIRMWARE_VERSION 413
#define FIRMWARE_VERSION 414
#ifdef esp8266_4mb
#define FIRMWARE_NAME "esp8266_4mb"

View File

@@ -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);

View File

@@ -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();

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -134,9 +134,10 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
publishWidgets();
publishState();
//отправка данных графиков
for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "Loging") {
(*it)->sendChart();
(*it)->sendChart(true);
}
}

View File

@@ -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<IoTItem*>::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);

View File

@@ -128,7 +128,7 @@ String IoTItem::getSubtype() {
return _subtype;
}
void IoTItem::sendChart() {}
void IoTItem::sendChart(bool mqtt) {}
String IoTItem::getID() {
return _id;

View File

@@ -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);
}
}

View File

@@ -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++;