2020-09-02 22:34:49 +03:00
|
|
|
#include "ItemsList.h"
|
|
|
|
|
|
2020-10-18 09:52:17 +03:00
|
|
|
#include "Class/NotAsync.h"
|
2020-10-10 01:44:11 +03:00
|
|
|
#include "Init.h"
|
2020-10-18 09:52:17 +03:00
|
|
|
#include "Utils/StringUtils.h"
|
2020-09-02 22:34:49 +03:00
|
|
|
|
2020-10-10 01:44:11 +03:00
|
|
|
static const char* firstLine PROGMEM = "Удалить;Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета";
|
|
|
|
|
|
|
|
|
|
void itemsListInit() {
|
2020-10-18 09:52:17 +03:00
|
|
|
myNotAsyncActions->add(
|
2020-10-10 01:44:11 +03:00
|
|
|
do_deviceInit, [&](void*) {
|
|
|
|
|
Device_init();
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
|
2020-10-18 09:52:17 +03:00
|
|
|
myNotAsyncActions->add(
|
2020-10-10 01:44:11 +03:00
|
|
|
do_delChoosingItems, [&](void*) {
|
|
|
|
|
delChoosingItems();
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|
2020-09-02 22:34:49 +03:00
|
|
|
|
|
|
|
|
void addItem(String name) {
|
|
|
|
|
String item = readFile("items/" + name + ".txt", 1024);
|
2020-10-10 01:44:11 +03:00
|
|
|
|
|
|
|
|
name = deleteToMarkerLast(name, ".");
|
|
|
|
|
|
2020-09-04 15:35:35 +03:00
|
|
|
item.replace("id", name + "-" + String(getNewElementNumber("id.txt")));
|
2020-09-02 22:34:49 +03:00
|
|
|
item.replace("order", String(getNewElementNumber("order.txt")));
|
2020-10-10 01:44:11 +03:00
|
|
|
|
2020-09-04 15:35:35 +03:00
|
|
|
if (item.indexOf("pin") != -1) { //all cases (random pins from available)
|
2020-09-02 22:34:49 +03:00
|
|
|
item.replace("pin", "pin[" + String(getFreePinAll()) + "]");
|
2020-10-10 01:44:11 +03:00
|
|
|
} else
|
|
|
|
|
|
|
|
|
|
if (item.indexOf("gol") != -1) { //analog
|
2020-09-04 00:35:38 +03:00
|
|
|
item.replace("gol", "pin[" + String(getFreePinAnalog()) + "]");
|
2020-10-10 01:44:11 +03:00
|
|
|
} else
|
|
|
|
|
|
|
|
|
|
if (item.indexOf("cin") != -1) { //ultrasonic
|
2020-09-04 00:35:38 +03:00
|
|
|
item.replace("cin", "pin[" + String(getFreePinAll()) + "," + String(getFreePinAll()) + "]");
|
2020-10-10 01:44:11 +03:00
|
|
|
} else
|
|
|
|
|
|
|
|
|
|
if (item.indexOf("sal") != -1) { //dallas
|
2020-09-04 00:35:38 +03:00
|
|
|
item.replace("sal", "pin[2]");
|
2020-10-10 01:44:11 +03:00
|
|
|
} else
|
|
|
|
|
|
|
|
|
|
if (item.indexOf("thd") != -1) { //dht11/22
|
2020-09-04 15:35:35 +03:00
|
|
|
item.replace("thd", "pin[2]");
|
2020-09-02 22:34:49 +03:00
|
|
|
}
|
2020-09-04 00:35:38 +03:00
|
|
|
|
2020-09-02 22:34:49 +03:00
|
|
|
item.replace("\r\n", "");
|
|
|
|
|
item.replace("\r", "");
|
|
|
|
|
item.replace("\n", "");
|
|
|
|
|
addFile(DEVICE_CONFIG_FILE, "\n" + item);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 00:04:07 +03:00
|
|
|
void addPreset(String name) {
|
2020-11-03 19:07:22 +03:00
|
|
|
String preset = readFile("presets/" + name + ".txt", 4048);
|
2020-11-03 00:04:07 +03:00
|
|
|
addFile(DEVICE_CONFIG_FILE, "\n" + preset);
|
|
|
|
|
|
|
|
|
|
name.replace(".c",".s");
|
|
|
|
|
|
2020-11-03 19:07:22 +03:00
|
|
|
String scenario = readFile("presets/" + name + ".txt", 4048);
|
2020-11-03 00:04:07 +03:00
|
|
|
removeFile(DEVICE_SCENARIO_FILE);
|
|
|
|
|
addFile(DEVICE_SCENARIO_FILE, scenario);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 22:34:49 +03:00
|
|
|
void delAllItems() {
|
|
|
|
|
removeFile(DEVICE_CONFIG_FILE);
|
|
|
|
|
addFile(DEVICE_CONFIG_FILE, String(firstLine));
|
2020-11-15 02:46:27 +03:00
|
|
|
removeFile(DEVICE_SCENARIO_FILE);
|
|
|
|
|
addFile(DEVICE_SCENARIO_FILE, "//");
|
2020-09-02 22:34:49 +03:00
|
|
|
removeFile("id.txt");
|
|
|
|
|
removeFile("order.txt");
|
|
|
|
|
removeFile("pins.txt");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t getNewElementNumber(String file) {
|
|
|
|
|
uint8_t number = readFile(file, 100).toInt();
|
|
|
|
|
number++;
|
|
|
|
|
removeFile(file);
|
|
|
|
|
addFile(file, String(number));
|
|
|
|
|
return number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t getFreePinAll() {
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
uint8_t pins[] = {0, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5};
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
uint8_t pins[] = {0, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5};
|
|
|
|
|
#endif
|
|
|
|
|
uint8_t array_sz = sizeof(pins) / sizeof(pins[0]);
|
|
|
|
|
uint8_t i = getNewElementNumber("pins.txt");
|
|
|
|
|
if (i < array_sz) {
|
|
|
|
|
return pins[i];
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t getFreePinAnalog() {
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-10 01:44:11 +03:00
|
|
|
void delChoosingItems() {
|
|
|
|
|
File configFile = LittleFS.open("/" + String(DEVICE_CONFIG_FILE), "r");
|
|
|
|
|
if (!configFile) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
configFile.seek(0, SeekSet);
|
|
|
|
|
String finalConf;
|
|
|
|
|
bool firstLine = true;
|
|
|
|
|
while (configFile.position() != configFile.size()) {
|
|
|
|
|
String item = configFile.readStringUntil('\n');
|
|
|
|
|
if (firstLine) {
|
|
|
|
|
finalConf += item;
|
|
|
|
|
} else {
|
|
|
|
|
int checkbox = selectToMarker(item, ";").toInt();
|
|
|
|
|
if (checkbox == 0) {
|
|
|
|
|
finalConf += "\n" + item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
firstLine = false;
|
|
|
|
|
}
|
|
|
|
|
removeFile(String(DEVICE_CONFIG_FILE));
|
|
|
|
|
addFile(String(DEVICE_CONFIG_FILE), finalConf);
|
|
|
|
|
Serial.println(finalConf);
|
|
|
|
|
configFile.close();
|
|
|
|
|
}
|