mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
callback
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"id1": "id1",
|
||||
"id2": "id2",
|
||||
"id3": "id3",
|
||||
"id4": "id4"
|
||||
}
|
||||
5
data/items/signs.json
Normal file
5
data/items/signs.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"more":">",
|
||||
"less":"<",
|
||||
"eq":"="
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
29
include/Class/AsyncActions.h
Normal file
29
include/Class/AsyncActions.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
// Декларируем тип - сигнатуру метода , который мы готовы принять в данном случае это
|
||||
// должен быть метод без результата и без параметров.
|
||||
// Новый тип мы называем AsynсActionCb - хотя можешь назвать вообще как нравиться а что значит callBack
|
||||
|
||||
//а как вызывать callback и когда их много? когда нужно вызывать например 10 разных из класса?
|
||||
//
|
||||
|
||||
typedef std::function<void(void*)> AsyncActionCb; //метод без результата и параметров
|
||||
|
||||
class AsyncActions {
|
||||
private:
|
||||
long count;
|
||||
AsyncActionCb _cb;
|
||||
|
||||
public:
|
||||
AsyncActions();
|
||||
void loop();
|
||||
void setCallback(AsyncActionCb cb) { //передаем внутрь класса функцию любую void функцию без агрументов
|
||||
_cb = cb;
|
||||
}
|
||||
};
|
||||
|
||||
extern AsyncActions* async;
|
||||
@@ -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();
|
||||
extern void do_getJsonListFromCsv();
|
||||
extern String getJsonListFromCsv(String csvFile,int colum);
|
||||
extern void do_delElement();
|
||||
extern void delElement(String itemsFile, String itemsLine);
|
||||
@@ -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;
|
||||
|
||||
/*
|
||||
* Запрос на скарнирование шины
|
||||
|
||||
3
include/main.h
Normal file
3
include/main.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
//void myCallback;
|
||||
13
src/Class/AsyncActions.cpp
Normal file
13
src/Class/AsyncActions.cpp
Normal file
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
@@ -85,4 +85,8 @@ boolean mqtt_send_settings_to_udp = false;
|
||||
BusScanner_t busToScan;
|
||||
boolean busScanFlag = false;
|
||||
boolean fsCheckFlag = false;
|
||||
boolean delElementFlag = false;
|
||||
boolean delElementFlag = false;
|
||||
boolean getJsonListFromCsvFlag = false;
|
||||
|
||||
String csvFile = "";
|
||||
int colum;
|
||||
@@ -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();
|
||||
|
||||
64
src/main.cpp
64
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) {
|
||||
|
||||
Reference in New Issue
Block a user