working wersion with elements

This commit is contained in:
Dmitry Borisenko
2020-08-26 15:44:19 +03:00
parent e3f3ae7664
commit 90e6822d3b
23 changed files with 247 additions and 203 deletions

View File

@@ -1,39 +1,57 @@
#include "DeviceList.h"
#include "ItemsList.h"
static const char* firstLine PROGMEM = "Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета";
void addItem(String name) {
String item = readFile("items/" + name + ".txt", 1024);
item.replace("id", "id" + String(getNewElementNumber("id.txt")));
item.replace("id", name + String(getNewElementNumber("id.txt")));
item.replace("order", String(getNewElementNumber("order.txt")));
item.replace("pin", "pin[" + String(getFreePin()) + "]");
item.replace("\r\n", "");
item.replace("\r", "");
item.replace("\n", "");
addFile("conf.csv", "\n" + item);
addFile(DEVICE_CONFIG_FILE, "\n" + item);
}
void delAllItems() {
removeFile("conf.csv");
addFile("conf.csv", String(firstLine));
removeFile(DEVICE_CONFIG_FILE);
addFile(DEVICE_CONFIG_FILE, String(firstLine));
removeFile("id.txt");
removeFile("order.txt");
removeFile("pins.txt");
}
int getNewElementNumber(String file) {
int number = readFile(file, 100).toInt();
uint8_t getNewElementNumber(String file) {
uint8_t number = readFile(file, 100).toInt();
number++;
removeFile(file);
addFile(file, String(number));
return number;
}
uint8_t getFreePin() {
#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;
}
}
//void do_getJsonListFromCsv() {
// if (getJsonListFromCsvFlag) {
// getJsonListFromCsvFlag = false;
// removeFile("items/items.json");
// addFile("items/items.json", getJsonListFromCsv("conf.csv", 1));
// addFile("items/items.json", getJsonListFromCsv(DEVICE_CONFIG_FILE, 1));
// }
//}
//

View File

@@ -1,4 +1,6 @@
#include "Utils\JsonUtils.h"
#include "Utils/FileUtils.h"
#include "Global.h"
#include <ArduinoJson.h>
@@ -49,4 +51,8 @@ String jsonWriteFloat(String& json, String name, float value) {
json = "";
root.printTo(json);
return json;
}
void saveConfig() {
writeFile(String("config.json"), configSetupJson);
}

View File

@@ -1,7 +1,9 @@
#include "Utils/SysUtils.h"
#include "Utils/PrintMessage.h"
#include "Global.h"
static const char* MODULE = "Main";
const String getUniqueId(const char* name) {
return String(name) + getMacAddress();
}
@@ -20,6 +22,11 @@ const String getChipId() {
return res;
}
void setChipId() {
chipId = getChipId();
pm.info("id: " + chipId);
}
#ifdef ESP8266
static uint32_t total_memory = 52864;
#else
@@ -69,6 +76,48 @@ const String getMacAddress() {
return String(buf);
}
#ifdef ESP8266
void setLedStatus(LedStatus_t status) {
pinMode(LED_PIN, OUTPUT);
switch (status) {
case LED_OFF:
noTone(LED_PIN);
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
noTone(LED_PIN);
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
tone(LED_PIN, 1);
break;
case LED_FAST:
tone(LED_PIN, 20);
break;
default:
break;
}
}
#else
void setLedStatus(LedStatus_t status) {
pinMode(LED_PIN, OUTPUT);
switch (status) {
case LED_OFF:
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
break;
case LED_FAST:
break;
default:
break;
}
}
#endif
//===================================================================
/*
void web_print (String text) {

88
src/Utils/WebUtils.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include "Utils\WebUtils.h"
#include "ESPAsyncWebServer.h"
String getURL(const String& urls) {
String res = "";
HTTPClient http;
http.begin(urls);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
res = http.getString();
} else {
res = "error";
}
http.end();
return res;
}
const String getMethodName(AsyncWebServerRequest* request) {
String res = F("UNKNOWN");
if (request->method() == HTTP_GET)
res = F("GET");
else if (request->method() == HTTP_POST)
res = F("POST");
else if (request->method() == HTTP_DELETE)
res = F("DELETE");
else if (request->method() == HTTP_PUT)
res = F("PUT");
else if (request->method() == HTTP_PATCH)
res = F("PATCH");
else if (request->method() == HTTP_HEAD)
res = F("HEAD");
else if (request->method() == HTTP_OPTIONS)
res = F("OPTIONS");
return res;
}
const String getRequestInfo(AsyncWebServerRequest* request) {
String res = getMethodName(request);
res += ' ';
res += "http://";
res += request->host();
res += request->url();
res += '\n';
if (request->contentLength()) {
res += "content-type: ";
res += request->contentType();
res += " content-lenght: ";
res += prettyBytes(request->contentLength());
res += '\n';
}
if (request->headers()) {
res += "headers:\n";
for (size_t i = 0; i < request->headers(); i++) {
AsyncWebHeader* h = request->getHeader(i);
res += h->name();
res += '=';
res += h->value();
res += '\n';
}
}
if (request->params()) {
res += "params:\n";
for (size_t i = 0; i < request->params(); i++) {
AsyncWebParameter* p = request->getParam(i);
if (p->isFile()) {
res += "FILE";
} else if (p->isPost()) {
res += "POST";
} else {
res += "GET";
}
res += ' ';
res += p->name();
res += ':';
res += p->value();
if (p->isFile()) {
res += " size:";
res += p->size();
}
res += '\n';
}
}
return res;
}

View File

@@ -1,4 +1,5 @@
#include "DeviceList.h"
#include "Web.h"
#include "ItemsList.h"
#include "Global.h"
#include "Init.h"
#include "Class/NotAsinc.h"
@@ -93,17 +94,17 @@ void web_init() {
loadScenario();
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("updatelist")) {
removeFile("/dev.csv");
addFileLn("dev.csv", "device id;device name;ip address");
request->redirect("/?set.udp");
}
//--------------------------------------------------------------------------------
if (request->hasArg("updatepage")) {
request->redirect("/?set.udp");
}
//--------------------------------------------------------------------------------
if (request->hasArg("devname")) {
jsonWriteStr(configSetupJson, "name", request->getParam("devname")->value());
saveConfig();
@@ -115,34 +116,37 @@ void web_init() {
saveConfig();
request->send(200);
}
if (request->hasArg("routerpass")) {
jsonWriteStr(configSetupJson, "routerpass", request->getParam("routerpass")->value());
saveConfig();
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("apssid")) {
jsonWriteStr(configSetupJson, "apssid", request->getParam("apssid")->value());
saveConfig();
request->send(200, "text/text", "OK");
}
if (request->hasArg("appass")) {
jsonWriteStr(configSetupJson, "appass", request->getParam("appass")->value());
saveConfig();
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("weblogin")) {
jsonWriteStr(configSetupJson, "weblogin", request->getParam("weblogin")->value());
saveConfig();
request->send(200);
}
if (request->hasArg("webpass")) {
jsonWriteStr(configSetupJson, "webpass", request->getParam("webpass")->value());
saveConfig();
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("timezone")) {
String timezoneStr = request->getParam("timezone")->value();
jsonWriteStr(configSetupJson, "timezone", timezoneStr);
@@ -150,6 +154,7 @@ void web_init() {
timeNow->setTimezone(timezoneStr.toInt());
request->send(200);
}
if (request->hasArg("ntp")) {
String ntpStr = request->getParam("ntp")->value();
jsonWriteStr(configSetupJson, "ntp", ntpStr);
@@ -157,7 +162,7 @@ void web_init() {
timeNow->setNtpPool(ntpStr);
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("blink")) {
bool value = request->getParam("blink")->value().toInt();
jsonWriteBool(configSetupJson, "blink", value);
@@ -196,13 +201,12 @@ void web_init() {
myNotAsincActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("mqttsend")) {
myNotAsincActions->make(do_MQTTUDP);
request->send(200);
}
//--------------------------------------------------------------------------------
if (request->hasArg("mqttcheck")) {
String buf = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + MqttClient::getStateStr();
@@ -295,4 +299,10 @@ void web_init() {
myNotAsincActions->make(do_UPGRADE);;
request->send(200, "text/html");
});
}
void setConfigParam(const char* param, const String& value) {
pm.info("set " + String(param) + ": " + value);
jsonWriteStr(configSetupJson, param, value);
saveConfig();
}

View File

@@ -3,9 +3,9 @@
#include "Class/NotAsinc.h"
#include "Class/Switch.h"
#include "Cmd.h"
#include "DeviceList.h"
#include "ItemsList.h"
#include "Global.h"
#include "HttpServer.h"
#include "Utils\WebUtils.h"
#include "Init.h"
#include "Utils/Timings.h"
@@ -106,76 +106,15 @@ void loop() {
String getURL(const String& urls) {
String res = "";
HTTPClient http;
http.begin(urls);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
res = http.getString();
} else {
res = "error";
}
http.end();
return res;
}
void setChipId() {
chipId = getChipId();
pm.info("id: " + chipId);
}
void saveConfig() {
writeFile(String("config.json"), configSetupJson);
}
void setConfigParam(const char* param, const String& value) {
pm.info("set " + String(param) + ": " + value);
jsonWriteStr(configSetupJson, param, value);
saveConfig();
}
#ifdef ESP8266
void setLedStatus(LedStatus_t status) {
pinMode(LED_PIN, OUTPUT);
switch (status) {
case LED_OFF:
noTone(LED_PIN);
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
noTone(LED_PIN);
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
tone(LED_PIN, 1);
break;
case LED_FAST:
tone(LED_PIN, 20);
break;
default:
break;
}
}
#else
void setLedStatus(LedStatus_t status) {
pinMode(LED_PIN, OUTPUT);
switch (status) {
case LED_OFF:
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
break;
case LED_FAST:
break;
default:
break;
}
}
#endif
void clock_init() {
timeNow = new Clock();