From 70edd92238467f68deb6fdd4512006812c121eea Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Wed, 26 Jan 2022 00:03:43 +0100 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=D0=B8?= =?UTF-8?q?=D0=B4=D0=B6=D0=B5=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CreateWidget.cpp | 36 +++++++++++++++++++++++++++--------- src/EspFileSystem.cpp | 1 + src/Main.cpp | 2 -- src/StandWebServer.cpp | 4 ++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/CreateWidget.cpp b/src/CreateWidget.cpp index 16b2f3c2..0e152f94 100644 --- a/src/CreateWidget.cpp +++ b/src/CreateWidget.cpp @@ -7,14 +7,16 @@ void createWidget(String& parameters) { String page = jsonReadStr(parameters, "page"); String descr = jsonReadStr(parameters, "descr"); + SerialPrint(F("i"), F("Widget"), "Start create widget: " + widget); + if (widget != "na") { String buf = "{}"; if (!loadWidget(widget, buf)) { return; } - // if (_cnt != "") { - // if (widget.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt); - // } + // if (_cnt != "") { + // if (widget.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt); + // } #ifdef GATE_MODE jsonWriteStr(buf, "info", " "); @@ -25,21 +27,37 @@ void createWidget(String& parameters) { jsonWriteStr(buf, "descr", descr); jsonWriteStr(buf, "topic", prex + "/" + id); + SerialPrint(F("i"), F("Widget"), buf); + #ifdef LAYOUT_IN_RAM all_widgets += widget + "\r\n"; #else - addFileLn("layout.txt", buf); + addFileLn("layout.json", buf); #endif } } bool loadWidget(const String& widget, String& buf) { - buf = readFile(getWidgetFile(widget), 2048); - bool res = !(buf == "Failed" || buf == "Large"); - if (!res) { - SerialPrint("E", "module", "on load" + widget); + File file = seekFile("/widgets.json"); + file.find("["); + bool ret = false; + while (file.available()) { + String jsonArrayElement = file.readStringUntil('}') + "}"; + if (jsonArrayElement.startsWith(",")) { + jsonArrayElement = jsonArrayElement.substring(1, jsonArrayElement.length()); //это нужно оптимизировать в последствии + } + String name; + if (!jsonRead(jsonArrayElement, F("name"), name)) { //если нет такого ключа в представленном json или он не валидный + SerialPrint(F("E"), F("Config"), "json error " + name); + continue; + } else { + if (name == widget) { + buf = jsonArrayElement; + ret = true; + } + } } - return res; + return ret; } const String getWidgetFile(const String& name) { diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index 0a2f4636..4923a351 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -12,6 +12,7 @@ bool fileSystemInit() { void globalVarsSync() { settingsFlashJson = readFile(F("settings.json"), 4096); settingsFlashJson.replace("\r\n", ""); + writeFile("layout.json", ""); } void saveSettingsFlashJson() { diff --git a/src/Main.cpp b/src/Main.cpp index 0e086e9d..6717ea03 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -39,8 +39,6 @@ void setup() { configure("/config.json"); - // readFileBin("/config.json"); - //создали задачу которая будет выполняться каждые 30 секунд ts.add( MYTEST, 1000 * 30, [&](void*) { diff --git a/src/StandWebServer.cpp b/src/StandWebServer.cpp index 52e1ff57..6377eec8 100644 --- a/src/StandWebServer.cpp +++ b/src/StandWebServer.cpp @@ -17,6 +17,10 @@ void standWebServerInit() { HTTP.send(200, "application/json", readFile(F("config.json"), 10000)); }); + HTTP.on("/layout.json", HTTP_GET, []() { + HTTP.send(200, "application/json", readFile(F("layout.json"), 10000)); + }); + HTTP.on("/restart", HTTP_GET, []() { // ESP.restart(); HTTP.send(200, "text/plain", "ok");