diff --git a/data/elements.json b/data/elements.json deleted file mode 100644 index 70e72031..00000000 --- a/data/elements.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "id1": "id1", - "id2": "id2", - "id3": "id3", - "id4": "id4" -} \ No newline at end of file diff --git a/data/items/signs.json b/data/items/signs.json new file mode 100644 index 00000000..7d140d4b --- /dev/null +++ b/data/items/signs.json @@ -0,0 +1,5 @@ +{ + "more":">", + "less":"<", + "eq":"=" +} \ No newline at end of file diff --git a/data/setn.device.json b/data/setn.device.json index 026beade..b3931aee 100644 --- a/data/setn.device.json +++ b/data/setn.device.json @@ -58,12 +58,66 @@ "action": "/set?cleanconf", "class": "btn btn-block btn-default" }, + { + "type": "hr" + }, + { + "type": "h4", + "title": "Если", + "style": "width:25%;float:left;" + }, { "type": "select", "name": "id-arg", "action": "/set?id=[[id-arg]]", "state": "en", - "title": "elements.json" + "title": "items/elements.json", + "style": "width:25%;float:left" + }, + { + "type": "select", + "name": "id-arg2", + "action": "/set?id=[[id-arg2]]", + "state": "en", + "title": "items/signs.json", + "style": "width:25%;float:left" + }, + { + "type": "input", + "title": "", + "name": "fsi-arg", + "pattern": "", + "state": "{{fsi}}", + "style": "width:25%;float:left" + }, + { + "type": "hr" + }, + { + "type": "h4", + "title": "То", + "style": "width:25%;float:left;" + }, + { + "type": "select", + "name": "id-arg3", + "action": "/set?id=[[id-arg3]]", + "state": "en", + "title": "items/elements.json", + "style": "width:25%;float:left" + }, + { + "type": "h4", + "title": "=", + "style": "width:25%;float:left;" + }, + { + "type": "input", + "title": "", + "name": "fsi-arg", + "pattern": "", + "state": "{{fsi}}", + "style": "width:25%;float:left" } ] } \ No newline at end of file diff --git a/include/Class/AsyncActions.h b/include/Class/AsyncActions.h new file mode 100644 index 00000000..19f7fc61 --- /dev/null +++ b/include/Class/AsyncActions.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include + +// Декларируем тип - сигнатуру метода , который мы готовы принять в данном случае это +// должен быть метод без результата и без параметров. +// Новый тип мы называем AsynсActionCb - хотя можешь назвать вообще как нравиться а что значит callBack + +//а как вызывать callback и когда их много? когда нужно вызывать например 10 разных из класса? +// + +typedef std::function AsyncActionCb; //метод без результата и параметров + +class AsyncActions { + private: + long count; + AsyncActionCb _cb; + + public: + AsyncActions(); + void loop(); + void setCallback(AsyncActionCb cb) { //передаем внутрь класса функцию любую void функцию без агрументов + _cb = cb; + } +}; + +extern AsyncActions* async; \ No newline at end of file diff --git a/include/DeviceList.h b/include/DeviceList.h index 124f55d2..1d6be8a7 100644 --- a/include/DeviceList.h +++ b/include/DeviceList.h @@ -6,5 +6,7 @@ extern void addElement(String name); extern void delAllElement(); extern int getNewElementNumber(String file); -extern void delElement(String itemsFile, String itemsLine); -extern void do_delElement(); \ No newline at end of file +extern void do_getJsonListFromCsv(); +extern String getJsonListFromCsv(String csvFile,int colum); +extern void do_delElement(); +extern void delElement(String itemsFile, String itemsLine); \ No newline at end of file diff --git a/include/Global.h b/include/Global.h index 0f951e05..06437aef 100644 --- a/include/Global.h +++ b/include/Global.h @@ -115,6 +115,10 @@ extern boolean mqttParamsChanged; extern boolean udp_data_parse; extern boolean mqtt_send_settings_to_udp; extern boolean delElementFlag; +extern boolean getJsonListFromCsvFlag; + +extern String csvFile; +extern int colum; /* * Запрос на скарнирование шины diff --git a/include/main.h b/include/main.h new file mode 100644 index 00000000..0d48dfc5 --- /dev/null +++ b/include/main.h @@ -0,0 +1,3 @@ +#pragma once + +//void myCallback; \ No newline at end of file diff --git a/src/Class/AsyncActions.cpp b/src/Class/AsyncActions.cpp new file mode 100644 index 00000000..c629a841 --- /dev/null +++ b/src/Class/AsyncActions.cpp @@ -0,0 +1,13 @@ +#include "Class/AsyncActions.h" + +AsyncActions::AsyncActions(){}; + +void AsyncActions::loop() { + count++; + if (count > 5000) { + if(_cb) _cb; //что означает эта запись? это и есть вызов callback? + count = 0; + } +} + +AsyncActions* async; \ No newline at end of file diff --git a/src/DeviceList.cpp b/src/DeviceList.cpp index 56e6d4b5..60410aa8 100644 --- a/src/DeviceList.cpp +++ b/src/DeviceList.cpp @@ -4,7 +4,7 @@ static const char* firstLine PROGMEM = "Тип элемента;Id;Виджет; void addElement(String name) { String item = readFile("items/" + name + ".txt", 1024); - + item.replace("id", "id" + String(getNewElementNumber("id.txt"))); item.replace("order", String(getNewElementNumber("order.txt"))); @@ -21,7 +21,6 @@ void delAllElement() { removeFile("order.txt"); } - int getNewElementNumber(String file) { int number = readFile(file, 100).toInt(); number++; @@ -30,7 +29,37 @@ int getNewElementNumber(String file) { return number; } +void do_getJsonListFromCsv() { + if (getJsonListFromCsvFlag) { + getJsonListFromCsvFlag = false; + removeFile("items/elements.json"); + addFile("items/elements.json", getJsonListFromCsv("conf.csv", 1)); + } +} +String getJsonListFromCsv(String csvFile, int colum) { + File configFile = LittleFS.open("/" + csvFile, "r"); + if (!configFile) { + return "error"; + } + configFile.seek(0, SeekSet); + + String outJson = "{}"; + + int count = -1; + + while (configFile.position() != configFile.size()) { + count++; + String item = configFile.readStringUntil('\n'); + if (count > 0) { + String line = selectFromMarkerToMarker(item, ";", colum); + jsonWriteStr(outJson, line, line); + } + } + configFile.close(); + csvFile = ""; + return outJson; +} void do_delElement() { if (delElementFlag) { diff --git a/src/Global.cpp b/src/Global.cpp index 89237a46..04a4041f 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -85,4 +85,8 @@ 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 +boolean delElementFlag = false; +boolean getJsonListFromCsvFlag = false; + +String csvFile = ""; +int colum; \ No newline at end of file diff --git a/src/Web.cpp b/src/Web.cpp index fe9942df..8d27006b 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -42,21 +42,27 @@ void web_init() { if (request->hasArg("element")) { String name = request->getParam("element")->value(); addElement(name); + getJsonListFromCsvFlag = true; Device_init(); request->redirect("/?setn.device"); } if (request->hasArg("cleanconf")) { delAllElement(); + removeFile("items/elements.json"); Device_init(); request->redirect("/?setn.device"); } if (request->hasArg("save")) { Device_init(); + getJsonListFromCsvFlag = true; request->redirect("/?setn.device"); } + + + //-------------------------------------------------------------------------------- if (request->hasArg("devinit")) { Device_init(); diff --git a/src/main.cpp b/src/main.cpp index 03d3ad39..57e88e5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,12 @@ -#include "Global.h" -#include "Init.h" -#include "Cmd.h" -#include "HttpServer.h" #include "Bus/BusScannerFactory.h" -#include "Utils/Timings.h" #include "Class/Switch.h" +#include "Cmd.h" #include "DeviceList.h" +#include "Global.h" +#include "HttpServer.h" +#include "Init.h" +#include "Utils/Timings.h" +#include "Class/AsyncActions.h" void not_async_actions(); @@ -67,49 +68,54 @@ void setup() { pm.info("Broadcast UDP"); udp_init(); #endif - ts.add( - TEST, 1000 * 60, [&](void*) { - pm.info(printMemoryStatus()); - }, - nullptr, true); + + ts.add(TEST, 1000 * 60, [&](void*) { pm.info(printMemoryStatus()); }, nullptr, true); just_load = false; - initialized = true; + + async = new AsyncActions(); + + //async->setCallback([&](void*) { +// +// + //}); + + async->setCallback(myCallback); // + + + } void loop() { if (!initialized) { return; } - timeNow->loop(); - #ifdef OTA_UPDATES_ENABLED ArduinoOTA.handle(); #endif #ifdef WS_enable ws.cleanupClients(); #endif - not_async_actions(); - - MqttClient::loop(); - - loopCmd(); - - mySwitch->loop(); - - loopScenario(); - #ifdef UDP_ENABLED loopUdp(); #endif - + timeNow->loop(); + async->loop(); + not_async_actions(); + MqttClient::loop(); + loopCmd(); + mySwitch->loop(); + loopScenario(); loopSerial(); - ts.update(); } void not_async_actions() { +#ifdef UDP_ENABLED + do_udp_data_parse(); + do_mqtt_send_settings_to_udp(); +#endif if (mqttParamsChanged) { MqttClient::reconnect(); mqttParamsChanged = false; @@ -118,15 +124,9 @@ void not_async_actions() { getLastVersion(); do_update(); - -#ifdef UDP_ENABLED - do_udp_data_parse(); - do_mqtt_send_settings_to_udp(); -#endif - do_scan_bus(); - do_delElement(); + do_getJsonListFromCsv(); } String getURL(const String& urls) {