From e0d0e30e93f4f44a5195cb2744b06ce740833ae0 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 20 Aug 2020 03:12:04 +0300 Subject: [PATCH] Continue making web interface --- data/setn.device.json | 1 + include/DeviceList.h | 9 +++++++++ include/Global.h | 1 + src/DeviceList.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/Global.cpp | 1 + src/Timers.cpp | 8 ++++---- src/Web.cpp | 18 +++++++++++++----- src/main.cpp | 3 +++ 8 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 include/DeviceList.h create mode 100644 src/DeviceList.cpp diff --git a/data/setn.device.json b/data/setn.device.json index b6e813be..e400db6c 100644 --- a/data/setn.device.json +++ b/data/setn.device.json @@ -50,6 +50,7 @@ ], "state": "conf.csv", "style": "width:100%;", + "action": "/set?delete", "class": "btn btn-block btn-default" }, { diff --git a/include/DeviceList.h b/include/DeviceList.h new file mode 100644 index 00000000..4096fdf6 --- /dev/null +++ b/include/DeviceList.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include "Global.h" + +extern void addElement(String name); +extern void delAllElement(); +extern void delElement(); +extern void do_delElement(); \ No newline at end of file diff --git a/include/Global.h b/include/Global.h index d8fee323..921ab1fb 100644 --- a/include/Global.h +++ b/include/Global.h @@ -111,6 +111,7 @@ extern boolean updateFlag; extern boolean mqttParamsChanged; extern boolean udp_data_parse; extern boolean mqtt_send_settings_to_udp; +extern boolean delElementFlag; /* * Запрос на скарнирование шины diff --git a/src/DeviceList.cpp b/src/DeviceList.cpp new file mode 100644 index 00000000..a65c292b --- /dev/null +++ b/src/DeviceList.cpp @@ -0,0 +1,41 @@ +#include "DeviceList.h" + +static const char* firstLine PROGMEM = "Удалить;Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета"; + +void addElement(String name) { + String item = readFile("items/" + name + ".txt", 1024); + item.replace("\r\n", ""); + item.replace("\r", ""); + item.replace("\n", ""); + addFile("conf.csv", "\n" + item); +} + +void delAllElement() { + removeFile("conf.csv"); + addFile("conf.csv", String(firstLine)); +} + +void do_delElement() { + if (delElementFlag) { + delElementFlag = false; + delElement(); + } +} + +void delElement() { + File configFile = LittleFS.open("/conf.csv", "r"); + if (!configFile) { + return; + } + configFile.seek(0, SeekSet); //поставим курсор в начало файла + String finalConf; + while (configFile.position() != configFile.size()) { + String item = configFile.readStringUntil('\n'); + if (selectToMarker(item, ";") == "0") { + finalConf += "\n" + item; + } + } + removeFile("conf.csv"); + addFile("conf.csv", String(firstLine) + "\n" + finalConf); + configFile.close(); +} diff --git a/src/Global.cpp b/src/Global.cpp index 05b51345..4d6178e7 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -82,3 +82,4 @@ boolean mqtt_send_settings_to_udp = false; BusScanner_t busToScan; boolean busScanFlag = false; boolean fsCheckFlag = false; +boolean delElementFlag = false; \ No newline at end of file diff --git a/src/Timers.cpp b/src/Timers.cpp index dbf451ba..aa6bd002 100644 --- a/src/Timers.cpp +++ b/src/Timers.cpp @@ -48,10 +48,10 @@ void timerStart_() { void addTimer(String number, String time) { String tmp = jsonReadStr(configOptionJson, "timers"); //1:60,2:120, String new_timer = number + ":" + time; - int psn1 = tmp.indexOf(number + ":"); //0 ищем позицию таймера который надо заменить - if (psn1 != -1) { //если он есть - int psn2 = tmp.indexOf(",", psn1); //4 от этой позиции находим позицию запятой - String timer = tmp.substring(psn1, psn2); //1:60 выделяем таймер который надо заменить + int psn1 = tmp.indexOf(number + ":"); //0 ищем позицию таймера который надо заменить + if (psn1 != -1) { //если он есть + int psn2 = tmp.indexOf(",", psn1); //4 от этой позиции находим позицию запятой + String timer = tmp.substring(psn1, psn2); //1:60 выделяем таймер который надо заменить ///tmp.replace(timer, new_timer); //заменяем таймер на новый (во всей стороке) tmp.replace(timer + ",", ""); tmp += new_timer + ","; diff --git a/src/Web.cpp b/src/Web.cpp index 69f322fd..5cc4c119 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -1,3 +1,4 @@ +#include "DeviceList.h" #include "Global.h" #include "Init.h" @@ -40,13 +41,20 @@ void web_init() { //-------------------------------------------------------------------------------- if (request->hasArg("element")) { String name = request->getParam("element")->value(); - String item = readFile("items/" + name + ".txt", 1024); - item.replace("\r\n", ""); - item.replace("\r", ""); - item.replace("\n", ""); - addFile("conf.csv", "\n" + item); + addElement(name); request->redirect("/?setn.device"); } + + if (request->hasArg("cleanconf")) { + delAllElement(); + request->redirect("/?setn.device"); + } + + if (request->hasArg("delete")) { + delElementFlag = true; + request->redirect("/?setn.device"); + } + //-------------------------------------------------------------------------------- if (request->hasArg("devinit")) { Device_init(); diff --git a/src/main.cpp b/src/main.cpp index 9b2eeaf2..03d3ad39 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "Bus/BusScannerFactory.h" #include "Utils/Timings.h" #include "Class/Switch.h" +#include "DeviceList.h" void not_async_actions(); @@ -124,6 +125,8 @@ void not_async_actions() { #endif do_scan_bus(); + + do_delElement(); } String getURL(const String& urls) {