2020-09-28 08:44:14 +00:00
|
|
|
#include "ESPUI.h"
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#include <functional>
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataControlsJS.h"
|
2019-04-15 11:49:15 +00:00
|
|
|
#include "dataGraphJS.h"
|
2020-09-28 08:40:31 +00:00
|
|
|
#include "dataIndexHTML.h"
|
|
|
|
#include "dataNormalizeCSS.h"
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataSliderJS.h"
|
2020-09-28 08:40:31 +00:00
|
|
|
#include "dataStyleCSS.h"
|
2019-03-03 21:21:21 +00:00
|
|
|
#include "dataTabbedcontentJS.h"
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataZeptoJS.h"
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2023-07-18 17:49:58 +00:00
|
|
|
#if ESP8266
|
|
|
|
#include <umm_malloc/umm_heap_select.h>
|
|
|
|
#endif
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
static String heapInfo(const __FlashStringHelper* mode)
|
2023-07-18 17:49:58 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
String result;
|
2023-07-18 17:49:58 +00:00
|
|
|
#if ESP8266
|
|
|
|
|
|
|
|
uint32_t hfree;
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t hmax;
|
2023-07-18 17:49:58 +00:00
|
|
|
uint8_t hfrag;
|
|
|
|
result.reserve(128);
|
|
|
|
|
|
|
|
#ifdef UMM_HEAP_IRAM
|
|
|
|
// here esp8266 is configurerd to use an extra 16KB (i)ram
|
|
|
|
{
|
|
|
|
HeapSelectIram useInstructionRamHere;
|
|
|
|
ESP.getHeapStats(&hfree, &hmax, &hfrag);
|
|
|
|
}
|
|
|
|
result += F("IRAM: free: ");
|
|
|
|
result += hfree;
|
|
|
|
result += F(" max: ");
|
|
|
|
result += hmax;
|
|
|
|
result += F(" frag: ");
|
|
|
|
result += hfrag;
|
|
|
|
result += "%\n";
|
|
|
|
#endif // !UMM_HEAP_IRAM
|
|
|
|
{
|
|
|
|
HeapSelectDram useRegularRamHere;
|
|
|
|
ESP.getHeapStats(&hfree, &hmax, &hfrag);
|
|
|
|
}
|
|
|
|
result += F("DRAM: free: ");
|
|
|
|
result += hfree;
|
|
|
|
result += F(" max: ");
|
|
|
|
result += hmax;
|
|
|
|
result += F(" frag: ");
|
|
|
|
result += hfrag;
|
|
|
|
result += "%\n";
|
|
|
|
|
|
|
|
#else // !ESP8266
|
|
|
|
|
|
|
|
result += ESP.getFreeHeap();
|
|
|
|
result += ' ';
|
|
|
|
|
|
|
|
#endif // !ESP8266
|
|
|
|
|
|
|
|
result += mode;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-04 10:35:43 +00:00
|
|
|
// ################# LITTLEFS functions
|
2018-01-14 11:22:26 +00:00
|
|
|
#if defined(ESP32)
|
2020-09-28 08:40:31 +00:00
|
|
|
void listDir(const char* dirname, uint8_t levels)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2020-06-23 18:34:05 +00:00
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
2020-10-18 09:06:38 +00:00
|
|
|
Serial.printf_P(PSTR("Listing directory: %s\n"), dirname);
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
#endif
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
File root = LittleFS.open(dirname);
|
|
|
|
#else
|
|
|
|
File root = LITTLEFS.open(dirname);
|
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
File root = LittleFS.open(dirname);
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2020-10-18 09:06:38 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!root)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Failed to open directory"));
|
|
|
|
}
|
|
|
|
#endif
|
2018-01-14 11:22:26 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!root.isDirectory())
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Not a directory"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
2020-06-23 18:34:05 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
|
|
|
|
File file = root.openNextFile();
|
|
|
|
|
|
|
|
while (file)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.print(F(" DIR : "));
|
|
|
|
Serial.println(file.name());
|
|
|
|
}
|
|
|
|
#endif
|
2018-01-14 11:22:26 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (levels)
|
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
listDir(file.path(), levels - 1);
|
|
|
|
#else
|
|
|
|
listDir(file.name(), levels - 1);
|
|
|
|
#endif
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.print(F(" FILE: "));
|
|
|
|
Serial.print(file.name());
|
|
|
|
Serial.print(F(" SIZE: "));
|
|
|
|
Serial.println(file.size());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
file = root.openNextFile();
|
|
|
|
}
|
2018-01-08 11:26:32 +00:00
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
#else
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void listDir(const char* dirname, uint8_t levels)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("Listing directory: %s\n"), dirname);
|
|
|
|
}
|
|
|
|
#endif
|
2020-09-28 08:40:31 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
Dir dir = LittleFS.openDir(dirname);
|
2020-09-28 08:40:31 +00:00
|
|
|
|
|
|
|
while (dir.next())
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
if (dir.isDirectory())
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.print(F(" DIR : "));
|
|
|
|
Serial.println(dir.fileName());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (levels)
|
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
File file = dir.openFile("r");
|
|
|
|
listDir(file.fullName(), levels - 1);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.print(F(" FILE: "));
|
|
|
|
Serial.print(dir.fileName());
|
|
|
|
Serial.print(F(" SIZE: "));
|
|
|
|
Serial.println(dir.fileSize());
|
|
|
|
}
|
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-06-23 18:34:05 +00:00
|
|
|
void ESPUIClass::list()
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
if (!LittleFS.begin())
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
if (!LITTLEFS.begin())
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("LITTLEFS Mount Failed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
if (!LittleFS.begin())
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Mount Failed"));
|
|
|
|
return;
|
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
listDir("/", 1);
|
2018-01-14 11:22:26 +00:00
|
|
|
#if defined(ESP32)
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
Serial.print(F("Total KB: "));
|
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
Serial.println(LittleFS.totalBytes() / 1024);
|
|
|
|
#else
|
|
|
|
Serial.println(LITTLEFS.totalBytes() / 1024);
|
|
|
|
#endif
|
|
|
|
Serial.print(F("Used KB: "));
|
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
Serial.println(LittleFS.usedBytes() / 1024);
|
|
|
|
#else
|
|
|
|
Serial.println(LITTLEFS.usedBytes() / 1024);
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-10-18 09:06:38 +00:00
|
|
|
#else
|
|
|
|
FSInfo fs_info;
|
2020-09-28 08:40:31 +00:00
|
|
|
LittleFS.info(fs_info);
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
Serial.print(F("Total KB: "));
|
|
|
|
Serial.println(fs_info.totalBytes / 1024);
|
|
|
|
Serial.print(F("Used KB: "));
|
|
|
|
Serial.println(fs_info.usedBytes / 1024);
|
2018-01-14 11:22:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void deleteFile(const char* path)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
bool exists = LittleFS.exists(path);
|
|
|
|
#else
|
|
|
|
bool exists = LITTLEFS.exists(path);
|
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
bool exists = LittleFS.exists(path);
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2020-10-18 09:06:38 +00:00
|
|
|
if (!exists)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("File: %s does not exist, not deleting\n"), path);
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-29 10:32:07 +00:00
|
|
|
|
2020-10-18 09:06:38 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("Deleting file: %s\n"), path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
bool didRemove = LittleFS.remove(path);
|
|
|
|
#else
|
|
|
|
bool didRemove = LITTLEFS.remove(path);
|
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
bool didRemove = LittleFS.remove(path);
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2021-10-29 17:20:20 +00:00
|
|
|
if (didRemove)
|
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("File deleted"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Delete failed"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void writeFile(const char* path, const char* data)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-10-18 09:06:38 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("Writing file: %s\n"), path);
|
|
|
|
}
|
|
|
|
#endif
|
2020-09-27 11:20:14 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
File file = LittleFS.open(path, FILE_WRITE);
|
|
|
|
#else
|
|
|
|
File file = LITTLEFS.open(path, FILE_WRITE);
|
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
File file = LittleFS.open(path, FILE_WRITE);
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2021-10-29 17:20:20 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Failed to open file for writing"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
#if defined(ESP32)
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (file.print(data))
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("File written"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
else
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Write failed"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
#else
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (file.print(FPSTR(data)))
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("File written"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
else
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Write failed"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
#endif
|
2020-09-28 08:40:31 +00:00
|
|
|
file.close();
|
2017-11-29 13:36:19 +00:00
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2022-01-04 10:35:43 +00:00
|
|
|
// end LITTLEFS functions
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
void ESPUIClass::prepareFileSystem(bool format)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
// this function should only be used once
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("About to prepare filesystem..."));
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-25 15:39:54 +00:00
|
|
|
|
|
|
|
#if defined(ESP32)
|
2022-10-13 20:10:20 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!LittleFS.begin(false)) // Test for an already formatted LittleFS by a mount failure
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!LITTLEFS.begin(false)) // Test for an already formatted LittleFS by a mount failure
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!LittleFS.begin(true)) // Attempt to format LittleFS
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!LITTLEFS.begin(true)) // Attempt to format LittleFS
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2023-08-03 14:32:54 +00:00
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Format Failed"));
|
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2023-08-03 14:32:54 +00:00
|
|
|
else if (format)
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
2023-08-03 14:32:54 +00:00
|
|
|
LittleFS.format();
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2023-08-03 14:32:54 +00:00
|
|
|
LITTLEFS.format();
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
|
|
|
#if defined(DEBUG_ESPUI)
|
2023-08-03 14:32:54 +00:00
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Formatted"));
|
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
listDir("/", 1);
|
2022-09-21 19:45:48 +00:00
|
|
|
Serial.println(F("LittleFS Mount ESP32 Done"));
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2017-12-25 15:39:54 +00:00
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!LittleFS.begin()) // Test for an already formatted LittleFS by a mount failure
|
2022-10-13 20:10:20 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
if (LittleFS.format()) // Attempt to format LittleFS
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2023-08-03 14:32:54 +00:00
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Formatted"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2023-08-03 14:32:54 +00:00
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Mount Failed"));
|
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
}
|
2023-08-03 14:32:54 +00:00
|
|
|
else if (format)
|
|
|
|
{
|
|
|
|
LittleFS.format();
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2023-08-03 14:32:54 +00:00
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("LittleFS Formatted"));
|
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2023-08-03 14:32:54 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
listDir("/", 1);
|
|
|
|
Serial.println(F("LittleFS Mount ESP8266 Done"));
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2017-12-25 15:39:54 +00:00
|
|
|
#endif
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
deleteFile("/index.htm");
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
deleteFile("/css/style.css");
|
|
|
|
deleteFile("/css/normalize.css");
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
deleteFile("/js/zepto.min.js");
|
|
|
|
deleteFile("/js/controls.js");
|
|
|
|
deleteFile("/js/slider.js");
|
|
|
|
deleteFile("/js/graph.js");
|
|
|
|
deleteFile("/js/tabbedcontent.js");
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("Cleanup done"));
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
// Now write
|
2023-08-03 14:32:54 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
writeFile("/index.htm", HTML_INDEX);
|
|
|
|
LittleFS.mkdir("/css");
|
|
|
|
writeFile("/css/style.css", CSS_STYLE);
|
|
|
|
writeFile("/css/normalize.css", CSS_NORMALIZE);
|
|
|
|
LittleFS.mkdir("/js");
|
|
|
|
writeFile("/js/zepto.min.js", JS_ZEPTO);
|
|
|
|
writeFile("/js/controls.js", JS_CONTROLS);
|
|
|
|
writeFile("/js/slider.js", JS_SLIDER);
|
|
|
|
writeFile("/js/graph.js", JS_GRAPH);
|
|
|
|
|
|
|
|
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
|
|
|
|
#else
|
|
|
|
writeFile("/index.htm", HTML_INDEX);
|
|
|
|
LITTLEFS.mkdir("/css");
|
|
|
|
writeFile("/css/style.css", CSS_STYLE);
|
|
|
|
writeFile("/css/normalize.css", CSS_NORMALIZE);
|
|
|
|
LITTLEFS.mkdir("/js");
|
|
|
|
writeFile("/js/zepto.min.js", JS_ZEPTO);
|
|
|
|
writeFile("/js/controls.js", JS_CONTROLS);
|
|
|
|
writeFile("/js/slider.js", JS_SLIDER);
|
|
|
|
writeFile("/js/graph.js", JS_GRAPH);
|
|
|
|
|
|
|
|
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
|
|
|
|
#endif
|
|
|
|
#else
|
2020-09-28 08:40:31 +00:00
|
|
|
writeFile("/index.htm", HTML_INDEX);
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
writeFile("/css/style.css", CSS_STYLE);
|
|
|
|
writeFile("/css/normalize.css", CSS_NORMALIZE);
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
writeFile("/js/zepto.min.js", JS_ZEPTO);
|
|
|
|
writeFile("/js/controls.js", JS_CONTROLS);
|
|
|
|
writeFile("/js/slider.js", JS_SLIDER);
|
|
|
|
writeFile("/js/graph.js", JS_GRAPH);
|
2019-04-15 11:49:15 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("Done Initializing filesystem :-)"));
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
#if defined(ESP32)
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
listDir("/", 1);
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
#endif
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
LittleFS.end();
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
LITTLEFS.end();
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
LittleFS.end();
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2017-11-28 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 13:00:53 +00:00
|
|
|
// Handle Websockets Communication
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::onWsEvent(
|
|
|
|
AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
// Serial.println(String("ESPUIClass::OnWsEvent: type: ") + String(type));
|
|
|
|
RemoveToBeDeletedControls();
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
if (WS_EVT_DISCONNECT == type)
|
2022-09-21 19:45:48 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("WS_EVT_DISCONNECT"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
if (MapOfClients.end() != MapOfClients.find(client->id()))
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
// Serial.println("Delete client.");
|
|
|
|
delete MapOfClients[client->id()];
|
|
|
|
MapOfClients.erase(client->id());
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-21 19:45:48 +00:00
|
|
|
else
|
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
if (MapOfClients.end() == MapOfClients.find(client->id()))
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
// Serial.println("ESPUIClass::OnWsEvent:Create new client.");
|
|
|
|
MapOfClients[client->id()] = new ESPUIclient(client);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-11-09 16:06:44 +00:00
|
|
|
if(MapOfClients[client->id()]->onWsEvent(type, arg, data, len))
|
|
|
|
{
|
|
|
|
// Serial.println("ESPUIClass::OnWsEvent:notify the clients that they need to be updated.");
|
|
|
|
NotifyClients(ESPUIclient::UpdateNeeded);
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 19:49:18 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
return;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
2017-10-16 22:10:48 +00:00
|
|
|
|
2022-06-11 04:42:25 +00:00
|
|
|
uint16_t ESPUIClass::addControl(ControlType type, const char* label)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-06-11 04:42:25 +00:00
|
|
|
return addControl(type, label, String(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value)
|
|
|
|
{
|
|
|
|
return addControl(type, label, value, ControlColor::Turquoise);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color)
|
|
|
|
{
|
|
|
|
return addControl(type, label, value, color, Control::noParent);
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t ESPUIClass::addControl(
|
|
|
|
ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl)
|
2022-06-11 04:42:25 +00:00
|
|
|
{
|
2023-09-14 22:59:10 +00:00
|
|
|
return addControl(type, label, value, color, parentControl, new Control(type, label, nullptr, value, color, true, parentControl));
|
2022-06-11 04:42:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color,
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t parentControl, std::function<void(Control*, int)> callback)
|
2022-06-11 04:42:25 +00:00
|
|
|
{
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t id = addControl(type, label, value, color, parentControl);
|
2022-06-11 04:42:25 +00:00
|
|
|
// set the original style callback
|
|
|
|
getControl(id)->callback = callback;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::addControl(
|
|
|
|
ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, Control* control)
|
2022-06-11 04:42:25 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
|
|
|
|
#endif // def ESP32
|
|
|
|
|
|
|
|
if (controls == nullptr)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
controls = control;
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
else
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
Control* iterator = controls;
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
while (iterator->next != nullptr)
|
|
|
|
{
|
|
|
|
iterator = iterator->next;
|
|
|
|
}
|
2018-05-13 18:19:29 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
iterator->next = control;
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
controlCount++;
|
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
xSemaphoreGive(ControlsSemaphore);
|
|
|
|
#endif // def ESP32
|
|
|
|
|
|
|
|
NotifyClients(ClientUpdateType_t::RebuildNeeded);
|
2022-01-12 20:12:20 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
return control->id;
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
bool ESPUIClass::removeControl(uint16_t id, bool force_rebuild_ui)
|
2020-06-15 09:06:34 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
bool Response = false;
|
2020-06-15 09:06:34 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
2022-06-10 16:44:53 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
Response = true;
|
|
|
|
control->DeleteControl();
|
|
|
|
controlCount--;
|
2020-06-23 18:34:05 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
if (force_rebuild_ui)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
jsonReload();
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
NotifyClients(ClientUpdateType_t::RebuildNeeded);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2022-09-21 19:45:48 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_ESPUI
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Serial.println(String("Could not Remove Control ") + String(id));
|
|
|
|
}
|
|
|
|
#endif // def DEBUG_ESPUI
|
|
|
|
|
|
|
|
return Response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPUIClass::RemoveToBeDeletedControls()
|
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
#ifdef ESP32
|
2022-09-21 19:45:48 +00:00
|
|
|
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif // def ESP32
|
2022-09-21 19:45:48 +00:00
|
|
|
|
|
|
|
Control* PreviousControl = nullptr;
|
|
|
|
Control* CurrentControl = controls;
|
|
|
|
|
|
|
|
while (nullptr != CurrentControl)
|
|
|
|
{
|
|
|
|
Control* NextControl = CurrentControl->next;
|
|
|
|
if (CurrentControl->ToBeDeleted())
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
if (CurrentControl == controls)
|
|
|
|
{
|
|
|
|
// this is the root control
|
|
|
|
controls = NextControl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PreviousControl->next = NextControl;
|
|
|
|
}
|
|
|
|
delete CurrentControl;
|
|
|
|
CurrentControl = NextControl;
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
PreviousControl = CurrentControl;
|
|
|
|
CurrentControl = NextControl;
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2020-06-23 18:34:05 +00:00
|
|
|
}
|
2023-08-03 14:32:54 +00:00
|
|
|
#ifdef ESP32
|
2022-09-21 19:45:48 +00:00
|
|
|
xSemaphoreGive(ControlsSemaphore);
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif // def ESP32
|
2020-06-15 09:06:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
uint16_t ESPUIClass::label(const char* label, ControlColor color, const String& value)
|
|
|
|
{
|
|
|
|
return addControl(ControlType::Label, label, value, color);
|
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
uint16_t ESPUIClass::graph(const char* label, ControlColor color)
|
|
|
|
{
|
|
|
|
return addControl(ControlType::Graph, label, "", color);
|
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t ESPUIClass::slider(
|
2023-09-14 22:59:10 +00:00
|
|
|
const char* label, std::function<void(Control*, int)> callback, ControlColor color, int value, int min, int max)
|
2022-06-11 05:20:25 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t sliderId
|
2023-09-14 22:59:10 +00:00
|
|
|
= addControl(ControlType::Slider, label, String(value), color, Control::noParent, callback);
|
2020-09-28 08:40:31 +00:00
|
|
|
addControl(ControlType::Min, label, String(min), ControlColor::None, sliderId);
|
|
|
|
addControl(ControlType::Max, label, String(max), ControlColor::None, sliderId);
|
|
|
|
return sliderId;
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 16:22:02 +00:00
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::button(const char* label, std::function<void(Control*, int)> callback, ControlColor color, const String& value)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::Button, label, value, color, Control::noParent, callback);
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 16:22:02 +00:00
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::switcher(const char* label, std::function<void(Control*, int)> callback, ControlColor color, bool startState)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback);
|
2017-10-16 13:00:53 +00:00
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::pad(const char* label, std::function<void(Control*, int)> callback, ControlColor color)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::Pad, label, "", color, Control::noParent, callback);
|
2019-03-24 18:18:53 +00:00
|
|
|
}
|
2022-06-11 05:20:25 +00:00
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::padWithCenter(const char* label, std::function<void(Control*, int)> callback, ControlColor color)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::PadWithCenter, label, "", color, Control::noParent, callback);
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t ESPUIClass::number(
|
2023-09-14 22:59:10 +00:00
|
|
|
const char* label, std::function<void(Control*, int)> callback, ControlColor color, int number, int min, int max)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
uint16_t numberId = addControl(ControlType::Number, label, String(number), color, Control::noParent, callback);
|
|
|
|
addControl(ControlType::Min, label, String(min), ControlColor::None, numberId);
|
|
|
|
addControl(ControlType::Max, label, String(max), ControlColor::None, numberId);
|
|
|
|
return numberId;
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
uint16_t ESPUIClass::gauge(const char* label, ControlColor color, int number, int min, int max)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
uint16_t numberId = addControl(ControlType::Gauge, label, String(number), color, Control::noParent);
|
|
|
|
addControl(ControlType::Min, label, String(min), ControlColor::None, numberId);
|
|
|
|
addControl(ControlType::Max, label, String(max), ControlColor::None, numberId);
|
|
|
|
return numberId;
|
2019-03-26 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
uint16_t ESPUIClass::separator(const char* label)
|
|
|
|
{
|
2022-01-08 20:58:22 +00:00
|
|
|
return addControl(ControlType::Separator, label, "", ControlColor::Alizarin, Control::noParent, nullptr);
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::accelerometer(const char* label, std::function<void(Control*, int)> callback, ControlColor color)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::Accel, label, "", color, Control::noParent, callback);
|
2019-03-26 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
uint16_t ESPUIClass::text(const char* label, std::function<void(Control*, int)> callback, ControlColor color, const String& value)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return addControl(ControlType::Text, label, value, color, Control::noParent, callback);
|
2017-10-19 11:46:47 +00:00
|
|
|
}
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
Control* ESPUIClass::getControl(uint16_t id)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
|
|
|
|
Control* Response = getControlNoLock(id);
|
|
|
|
xSemaphoreGive(ControlsSemaphore);
|
|
|
|
return Response;
|
|
|
|
#else
|
|
|
|
return getControlNoLock(id);
|
|
|
|
#endif // !def ESP32
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Anytime you walk the chain of controllers, the protection semaphore
|
|
|
|
// MUST be locked. This function assumes that the semaphore is locked
|
|
|
|
// at the time it is called. Make sure YOU locked it :)
|
|
|
|
Control* ESPUIClass::getControlNoLock(uint16_t id)
|
|
|
|
{
|
|
|
|
Control* Response = nullptr;
|
|
|
|
Control* control = controls;
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
while (nullptr != control)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
if (control->id == id)
|
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
if (!control->ToBeDeleted())
|
2022-09-21 19:45:48 +00:00
|
|
|
{
|
|
|
|
Response = control;
|
|
|
|
}
|
|
|
|
break;
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
control = control->next;
|
|
|
|
}
|
2018-05-13 18:19:29 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
return Response;
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
void ESPUIClass::updateControl(Control* control, int)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!control)
|
2020-08-26 20:00:20 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
return;
|
2020-08-26 20:00:20 +00:00
|
|
|
}
|
2023-11-09 16:06:44 +00:00
|
|
|
// tell the control it has been updated
|
|
|
|
control->SetControlChangedId(ESPUI.GetNextControlChangeId());
|
2022-09-21 19:45:48 +00:00
|
|
|
NotifyClients(ClientUpdateType_t::UpdateNeeded);
|
2017-10-19 15:30:32 +00:00
|
|
|
}
|
2019-03-08 21:22:01 +00:00
|
|
|
|
2023-11-09 16:06:44 +00:00
|
|
|
uint32_t ESPUIClass::GetNextControlChangeId()
|
|
|
|
{
|
|
|
|
if(uint32_t(-1) == ControlChangeID)
|
|
|
|
{
|
|
|
|
// force a reload which resets the counters
|
|
|
|
jsonReload();
|
|
|
|
}
|
|
|
|
return ++ControlChangeID;
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:37:50 +00:00
|
|
|
void ESPUIClass::setPanelStyle(uint16_t id, const String& style, int clientId)
|
2022-01-02 21:56:32 +00:00
|
|
|
{
|
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
|
|
|
control->panelStyle = style;
|
|
|
|
updateControl(control, clientId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:37:50 +00:00
|
|
|
void ESPUIClass::setElementStyle(uint16_t id, const String& style, int clientId)
|
2022-01-02 21:56:32 +00:00
|
|
|
{
|
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
|
|
|
control->elementStyle = style;
|
|
|
|
updateControl(control, clientId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:37:50 +00:00
|
|
|
void ESPUIClass::setInputType(uint16_t id, const String& type, int clientId)
|
2022-05-22 22:31:48 +00:00
|
|
|
{
|
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
|
|
|
control->inputType = type;
|
|
|
|
updateControl(control, clientId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPUIClass::setPanelWide(uint16_t id, bool wide)
|
|
|
|
{
|
2022-01-08 21:25:10 +00:00
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
|
|
|
control->wide = wide;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::setEnabled(uint16_t id, bool enabled, int clientId)
|
|
|
|
{
|
2022-01-31 21:43:09 +00:00
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
// Serial.println(String("CreateAllowed: id: ") + String(clientId) + " State: " + String(enabled));
|
2022-01-31 21:43:09 +00:00
|
|
|
control->enabled = enabled;
|
|
|
|
updateControl(control, clientId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::setVertical(uint16_t id, bool vert)
|
|
|
|
{
|
2022-01-21 23:30:08 +00:00
|
|
|
Control* control = getControl(id);
|
|
|
|
if (control)
|
|
|
|
{
|
|
|
|
control->vertical = vert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:34:05 +00:00
|
|
|
void ESPUIClass::updateControl(uint16_t id, int clientId)
|
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
Control* control = getControl(id);
|
2019-03-08 21:22:01 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!control)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
Serial.printf_P(PSTR("Error: Update Control: There is no control with ID %d\n"), id);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
2019-03-08 21:22:01 +00:00
|
|
|
}
|
2019-03-26 15:22:21 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
updateControl(control, clientId);
|
2019-03-08 21:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateControlValue(Control* control, const String& value, int clientId)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!control)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-03-24 14:44:27 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
control->value = value;
|
|
|
|
updateControl(control, clientId);
|
2019-03-08 21:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 09:59:35 +00:00
|
|
|
void ESPUIClass::updateControlValue(uint16_t id, const String& value, int clientId)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
Control* control = getControl(id);
|
2017-10-19 15:30:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (!control)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
Serial.printf_P(PSTR("Error: updateControlValue Control: There is no control with ID %d\n"), id);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2019-03-26 15:22:21 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
updateControlValue(control, value, clientId);
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
2019-03-08 21:22:01 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::updateControlLabel(uint16_t id, const char* value, int clientId)
|
2022-09-21 19:45:48 +00:00
|
|
|
{
|
|
|
|
updateControlLabel(getControl(id), value, clientId);
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::updateControlLabel(Control* control, const char* value, int clientId)
|
2022-09-21 19:45:48 +00:00
|
|
|
{
|
|
|
|
if (!control)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (verbosity)
|
|
|
|
{
|
|
|
|
Serial.printf_P(PSTR("Error: updateControlLabel Control: There is no control with the requested ID \n"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
control->label = value;
|
|
|
|
updateControl(control, clientId);
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::updateVisibility(uint16_t id, bool visibility, int clientId)
|
|
|
|
{
|
2022-01-28 20:31:25 +00:00
|
|
|
Control* control = getControl(id);
|
2023-08-03 14:32:54 +00:00
|
|
|
if (control)
|
2022-01-28 20:31:25 +00:00
|
|
|
{
|
|
|
|
control->visible = visibility;
|
2022-09-21 19:45:48 +00:00
|
|
|
updateControl(control, clientId);
|
2022-01-28 20:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::print(uint16_t id, const String& value)
|
|
|
|
{
|
|
|
|
updateControlValue(id, value);
|
|
|
|
}
|
2018-12-02 10:41:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateLabel(uint16_t id, const String& value)
|
|
|
|
{
|
|
|
|
updateControlValue(id, value);
|
|
|
|
}
|
2019-03-03 20:46:38 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::updateButton(uint16_t id, const String& value)
|
|
|
|
{
|
2022-01-16 14:47:41 +00:00
|
|
|
updateControlValue(id, value);
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, String(nValue), clientId);
|
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, String(nValue ? "1" : "0"), clientId);
|
|
|
|
}
|
2017-11-29 10:32:07 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateNumber(uint16_t id, int number, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, String(number), clientId);
|
|
|
|
}
|
2018-11-26 17:25:10 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateText(uint16_t id, const String& text, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, text, clientId);
|
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateSelect(uint16_t id, const String& text, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, text, clientId);
|
|
|
|
}
|
2019-03-04 20:07:39 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::updateGauge(uint16_t id, int number, int clientId)
|
|
|
|
{
|
|
|
|
updateControlValue(id, String(number), clientId);
|
|
|
|
}
|
2019-03-26 15:22:21 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
void ESPUIClass::updateTime(uint16_t id, int clientId)
|
2022-01-20 21:50:06 +00:00
|
|
|
{
|
|
|
|
updateControl(id, clientId);
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
void ESPUIClass::clearGraph(uint16_t id, int clientId)
|
|
|
|
{
|
2022-11-03 02:31:04 +00:00
|
|
|
do // once
|
|
|
|
{
|
|
|
|
Control* control = getControl(id);
|
|
|
|
if (!control)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicJsonDocument document(jsonUpdateDocumentSize);
|
|
|
|
JsonObject root = document.to<JsonObject>();
|
|
|
|
|
2022-11-05 13:06:13 +00:00
|
|
|
root[F("type")] = (int)ControlType::Graph + UpdateOffset;
|
2022-11-03 02:31:04 +00:00
|
|
|
root[F("value")] = 0;
|
|
|
|
root[F("id")] = control->id;
|
|
|
|
|
|
|
|
SendJsonDocToWebSocket(document, clientId);
|
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
} while (false);
|
|
|
|
}
|
2019-04-15 13:32:14 +00:00
|
|
|
|
2020-06-23 18:34:05 +00:00
|
|
|
void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
|
|
|
|
{
|
2022-09-22 13:36:12 +00:00
|
|
|
do // once
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-09-22 13:36:12 +00:00
|
|
|
Control* control = getControl(id);
|
|
|
|
if (!control)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicJsonDocument document(jsonUpdateDocumentSize);
|
|
|
|
JsonObject root = document.to<JsonObject>();
|
|
|
|
|
|
|
|
root[F("type")] = (int)ControlType::GraphPoint;
|
|
|
|
root[F("value")] = nValue;
|
|
|
|
root[F("id")] = control->id;
|
2019-04-15 13:32:14 +00:00
|
|
|
|
2022-09-22 13:36:12 +00:00
|
|
|
SendJsonDocToWebSocket(document, clientId);
|
2020-09-28 08:40:31 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
} while (false);
|
2022-09-21 19:45:48 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId)
|
|
|
|
{
|
|
|
|
bool Response = false;
|
2020-09-28 08:40:31 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
if (0 > clientId)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
if (MapOfClients.end() != MapOfClients.find(clientId))
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
Response = MapOfClients[clientId]->SendJsonDocToWebSocket(document);
|
2019-04-15 13:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-21 19:45:48 +00:00
|
|
|
else
|
2022-01-12 20:12:20 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
for (auto CurrentClient : MapOfClients)
|
2022-09-21 19:45:48 +00:00
|
|
|
{
|
2022-09-22 13:36:12 +00:00
|
|
|
Response |= CurrentClient.second->SendJsonDocToWebSocket(document);
|
2022-09-21 19:45:48 +00:00
|
|
|
}
|
2022-01-12 20:12:20 +00:00
|
|
|
}
|
2022-01-01 22:04:32 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
return Response;
|
|
|
|
}
|
2022-01-01 22:04:32 +00:00
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
void ESPUIClass::jsonDom(uint16_t, AsyncWebSocketClient*, bool)
|
|
|
|
{
|
|
|
|
NotifyClients(ClientUpdateType_t::RebuildNeeded);
|
2022-01-01 22:04:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:48 +00:00
|
|
|
// Tell all of the clients that they need to ask for an upload of the control data.
|
|
|
|
void ESPUIClass::NotifyClients(ClientUpdateType_t newState)
|
2022-01-02 21:56:32 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
for (auto& CurrentClient : MapOfClients)
|
2022-01-12 20:12:20 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
CurrentClient.second->NotifyClient(newState);
|
2022-01-12 20:12:20 +00:00
|
|
|
}
|
2022-09-21 19:45:48 +00:00
|
|
|
}
|
2022-01-12 20:12:20 +00:00
|
|
|
|
2020-06-23 18:34:05 +00:00
|
|
|
void ESPUIClass::jsonReload()
|
2020-06-15 09:06:34 +00:00
|
|
|
{
|
2023-08-03 14:32:54 +00:00
|
|
|
for (auto& CurrentClient : MapOfClients)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
// Serial.println("Requesting Reload");
|
|
|
|
CurrentClient.second->NotifyClient(ClientUpdateType_t::ReloadNeeded);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2020-06-15 09:06:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 17:20:20 +00:00
|
|
|
void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const char* password, uint16_t port)
|
2022-01-04 10:35:43 +00:00
|
|
|
{
|
|
|
|
// Backwards compatibility wrapper
|
|
|
|
beginLITTLEFS(_title, username, password, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const char* password, uint16_t port)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-10-18 09:06:38 +00:00
|
|
|
ui_title = _title;
|
2022-09-21 19:45:48 +00:00
|
|
|
basicAuthUsername = username;
|
|
|
|
basicAuthPassword = password;
|
2020-09-27 11:20:14 +00:00
|
|
|
|
2020-10-18 09:06:38 +00:00
|
|
|
if (username == nullptr && password == nullptr)
|
|
|
|
{
|
|
|
|
basicAuth = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
basicAuth = true;
|
|
|
|
}
|
2020-09-27 11:20:14 +00:00
|
|
|
|
2021-10-29 17:20:20 +00:00
|
|
|
server = new AsyncWebServer(port);
|
2020-10-18 09:06:38 +00:00
|
|
|
ws = new AsyncWebSocket("/ws");
|
2020-09-28 08:40:31 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
bool fsBegin = LittleFS.begin();
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
bool fsBegin = LITTLEFS.begin();
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
bool fsBegin = LittleFS.begin();
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2021-10-29 17:20:20 +00:00
|
|
|
if (!fsBegin)
|
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
2022-01-04 10:35:43 +00:00
|
|
|
Serial.println(F("LITTLEFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
|
2020-09-28 08:40:31 +00:00
|
|
|
"PREPARE YOUR ESP!!!!!!!"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-18 09:06:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
listDir("/", 1);
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2020-09-28 08:40:31 +00:00
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
bool indexExists = LittleFS.exists("/index.htm");
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
bool indexExists = LITTLEFS.exists("/index.htm");
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
bool indexExists = LittleFS.exists("/index.htm");
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2021-10-29 17:20:20 +00:00
|
|
|
if (!indexExists)
|
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("Please read the README!!!!!!!, Make sure to "
|
2022-09-21 19:45:48 +00:00
|
|
|
"prepareFileSystem() once in an empty sketch"));
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
ws->onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data,
|
|
|
|
size_t len) { ESPUI.onWsEvent(server, client, type, arg, data, len); });
|
2020-09-28 08:40:31 +00:00
|
|
|
server->addHandler(ws);
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (basicAuth)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
if (WS_AUTHENTICATION)
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
ws->setAuthentication(basicAuthUsername, basicAuthPassword);
|
2020-09-28 08:40:31 +00:00
|
|
|
}
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2021-10-29 17:20:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-13 20:10:20 +00:00
|
|
|
#if defined(ESP32)
|
2023-08-03 14:32:54 +00:00
|
|
|
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
|
|
|
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
|
|
|
|
#else
|
2022-10-13 20:10:20 +00:00
|
|
|
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm");
|
2023-08-03 14:32:54 +00:00
|
|
|
#endif
|
2022-10-13 20:10:20 +00:00
|
|
|
#else
|
2021-10-29 17:20:20 +00:00
|
|
|
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
|
2022-10-13 20:10:20 +00:00
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
// Heap for general Servertest
|
|
|
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:49:58 +00:00
|
|
|
request->send(200, "text/plain", heapInfo(F("In LITTLEFS mode")));
|
2020-09-28 08:40:31 +00:00
|
|
|
});
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2022-09-19 16:38:10 +00:00
|
|
|
server->onNotFound([this](AsyncWebServerRequest* request) {
|
2023-08-03 14:32:54 +00:00
|
|
|
if (captivePortal)
|
|
|
|
{
|
|
|
|
request->redirect("/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->send(404);
|
|
|
|
}
|
|
|
|
});
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->begin();
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("UI Initialized"));
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-26 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 17:20:20 +00:00
|
|
|
void ESPUIClass::begin(const char* _title, const char* username, const char* password, uint16_t port)
|
2020-06-23 18:34:05 +00:00
|
|
|
{
|
2020-09-28 08:40:31 +00:00
|
|
|
basicAuthUsername = username;
|
|
|
|
basicAuthPassword = password;
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (username != nullptr && password != nullptr)
|
|
|
|
{
|
|
|
|
basicAuth = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
basicAuth = false;
|
|
|
|
}
|
2018-12-26 12:38:38 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
ui_title = _title;
|
2018-12-26 12:38:38 +00:00
|
|
|
|
2021-10-29 17:20:20 +00:00
|
|
|
server = new AsyncWebServer(port);
|
2020-09-28 08:40:31 +00:00
|
|
|
ws = new AsyncWebSocket("/ws");
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2023-08-03 14:32:54 +00:00
|
|
|
ws->onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data,
|
|
|
|
size_t len) { ESPUI.onWsEvent(server, client, type, arg, data, len); });
|
2022-09-21 19:45:48 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->addHandler(ws);
|
2018-12-26 12:38:38 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
if (basicAuth && WS_AUTHENTICATION)
|
|
|
|
ws->setAuthentication(username, password);
|
2018-12-26 12:38:38 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response = request->beginResponse_P(200, "text/html", HTML_INDEX);
|
|
|
|
request->send(response);
|
|
|
|
});
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
// Javascript files
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "application/javascript", JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2019-03-24 14:44:27 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 21:21:21 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2019-04-15 11:49:15 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/js/graph.js", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-04-15 11:49:15 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "application/javascript", JS_GRAPH_GZIP, sizeof(JS_GRAPH_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2019-03-24 14:44:27 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/js/tabbedcontent.js", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-24 17:10:21 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response = request->beginResponse_P(
|
|
|
|
200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof(JS_TABBEDCONTENT_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2019-03-03 21:21:21 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
// Stylesheets
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->on("/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
AsyncWebServerResponse* response
|
|
|
|
= request->beginResponse_P(200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
});
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
// Heap for general Servertest
|
|
|
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
|
|
|
|
{
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2023-07-18 17:49:58 +00:00
|
|
|
request->send(200, "text/plain", heapInfo(F("In Memorymode")));
|
2020-09-28 08:40:31 +00:00
|
|
|
});
|
2017-10-16 13:00:53 +00:00
|
|
|
|
2022-09-19 16:38:10 +00:00
|
|
|
server->onNotFound([this](AsyncWebServerRequest* request) {
|
2023-08-03 14:32:54 +00:00
|
|
|
if (captivePortal)
|
|
|
|
{
|
|
|
|
request->redirect("/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->send(404);
|
|
|
|
}
|
|
|
|
});
|
2017-10-16 13:00:53 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
server->begin();
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
#if defined(DEBUG_ESPUI)
|
2022-09-21 19:45:48 +00:00
|
|
|
if (verbosity)
|
2020-09-28 08:40:31 +00:00
|
|
|
{
|
|
|
|
Serial.println(F("UI Initialized"));
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2019-03-24 15:06:35 +00:00
|
|
|
|
2020-09-28 08:40:31 +00:00
|
|
|
void ESPUIClass::setVerbosity(Verbosity v)
|
|
|
|
{
|
2022-09-21 19:45:48 +00:00
|
|
|
verbosity = v;
|
2022-06-11 04:42:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:34:05 +00:00
|
|
|
ESPUIClass ESPUI;
|