2017-10-19 15:43:39 +00:00
|
|
|
#include "ESPUI.h"
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataIndexHTML.h"
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataNormalizeCSS.h"
|
|
|
|
#include "dataStyleCSS.h"
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2018-05-27 09:47:53 +00:00
|
|
|
#include "dataControlsJS.h"
|
|
|
|
#include "dataSliderJS.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
|
|
|
|
2017-10-16 13:00:53 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2017-05-18 22:05:32 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
uint16_t Control::idCounter = 0;
|
|
|
|
|
2017-11-29 13:36:19 +00:00
|
|
|
// ################# Spiffs functions
|
2018-01-14 11:22:26 +00:00
|
|
|
#if defined(ESP32)
|
2019-03-03 20:13:45 +00:00
|
|
|
void listDir( const char* dirname, uint8_t levels ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "Listing directory: %s\n", dirname );
|
|
|
|
}
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
File root = SPIFFS.open( dirname );
|
|
|
|
|
|
|
|
if ( !root ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Failed to open directory" );
|
|
|
|
}
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( !root.isDirectory() ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Not a directory" );
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File file = root.openNextFile();
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
while ( file ) {
|
|
|
|
if ( file.isDirectory() ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.print( " DIR : " );
|
|
|
|
Serial.println( file.name() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( levels ) {
|
|
|
|
listDir( file.name(), levels - 1 );
|
2018-01-14 11:22:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.print( " FILE: " );
|
|
|
|
Serial.print( file.name() );
|
|
|
|
Serial.print( " SIZE: " );
|
|
|
|
Serial.println( file.size() );
|
|
|
|
}
|
2018-01-08 11:26:32 +00:00
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void listDir( const char* dirname, uint8_t levels ) {
|
2018-01-14 11:22:26 +00:00
|
|
|
// ignoring levels for esp8266
|
2019-03-03 20:13:45 +00:00
|
|
|
Serial.printf( "Listing directory: %s\n", dirname );
|
2018-01-14 11:22:26 +00:00
|
|
|
|
|
|
|
String str = "";
|
2019-03-03 20:13:45 +00:00
|
|
|
Dir dir = SPIFFS.openDir( "/" );
|
|
|
|
|
|
|
|
while ( dir.next() ) {
|
|
|
|
Serial.print( " FILE: " );
|
|
|
|
Serial.print( dir.fileName() );
|
|
|
|
Serial.print( " SIZE: " );
|
|
|
|
Serial.println( dir.fileSize() );
|
2018-01-14 11:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ESPUIClass::list() {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( !SPIFFS.begin() ) {
|
|
|
|
Serial.println( "SPIFFS Mount Failed" );
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
listDir( "/", 1 );
|
2018-01-14 11:22:26 +00:00
|
|
|
#if defined(ESP32)
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
Serial.println( SPIFFS.totalBytes() );
|
|
|
|
Serial.println( SPIFFS.usedBytes() );
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
#else
|
|
|
|
FSInfo fs_info;
|
2019-03-03 20:13:45 +00:00
|
|
|
SPIFFS.info( fs_info );
|
|
|
|
|
|
|
|
Serial.println( fs_info.totalBytes );
|
|
|
|
Serial.println( fs_info.usedBytes );
|
2018-01-14 11:22:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void deleteFile( const char* path ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.print( SPIFFS.exists( path ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !SPIFFS.exists( path ) ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "File: %s does not exist, not deleting\n", path );
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "Deleting file: %s\n", path );
|
|
|
|
}
|
2017-11-29 10:32:07 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( SPIFFS.remove( path ) ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "File deleted" );
|
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Delete failed" );
|
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void writeFile( const char* path, const char* data ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "Writing file: %s\n", path );
|
|
|
|
}
|
|
|
|
|
|
|
|
File file = SPIFFS.open( path, FILE_WRITE );
|
|
|
|
|
|
|
|
if ( !file ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Failed to open file for writing" );
|
|
|
|
}
|
2017-11-29 10:32:07 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
if ( file.print( data ) ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "File written" );
|
|
|
|
}
|
2018-04-17 17:54:07 +00:00
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Write failed" );
|
|
|
|
}
|
2018-04-17 17:54:07 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
if ( file.print( FPSTR( data ) ) ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "File written" );
|
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Write failed" );
|
|
|
|
}
|
2018-01-14 11:22:26 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
#endif
|
2018-01-14 11:22:26 +00:00
|
|
|
file.close();
|
2017-11-29 13:36:19 +00:00
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2017-11-29 13:36:19 +00:00
|
|
|
// end Spiffs functions
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
void ESPUIClass::prepareFileSystem() {
|
|
|
|
// this function should only be used once
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "About to prepare filesystem..." );
|
|
|
|
}
|
2017-12-25 15:39:54 +00:00
|
|
|
|
|
|
|
#if defined(ESP32)
|
2018-01-08 11:26:32 +00:00
|
|
|
SPIFFS.format();
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
if ( !SPIFFS.begin( true ) ) {
|
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "SPIFFS Mount Failed" );
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
2017-12-25 15:39:54 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
if ( this->verbosity ) {
|
|
|
|
listDir( "/", 1 );
|
|
|
|
Serial.println( "SPIFFS Mount ESP32 Done" );
|
|
|
|
}
|
|
|
|
|
2017-12-25 15:39:54 +00:00
|
|
|
#else
|
|
|
|
SPIFFS.format();
|
2018-01-08 11:26:32 +00:00
|
|
|
SPIFFS.begin();
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "SPIFFS Mount ESP8266 Done" );
|
|
|
|
}
|
|
|
|
|
2017-12-25 15:39:54 +00:00
|
|
|
#endif
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
deleteFile( "/index.htm" );
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
deleteFile( "/css/style.css" );
|
|
|
|
deleteFile( "/css/normalize.css" );
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
deleteFile( "/js/zepto.min.js" );
|
|
|
|
deleteFile( "/js/controls.js" );
|
|
|
|
deleteFile( "/js/slider.js" );
|
2019-03-03 21:21:21 +00:00
|
|
|
deleteFile( "/js/tabbedcontent.js" );
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "Cleanup done" );
|
|
|
|
}
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
// Now write
|
2019-03-03 20:13:45 +00:00
|
|
|
writeFile( "/index.htm", HTML_INDEX );
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
writeFile( "/css/style.css", CSS_STYLE );
|
|
|
|
writeFile( "/css/normalize.css", CSS_NORMALIZE );
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
writeFile( "/js/zepto.min.js", JS_ZEPTO );
|
|
|
|
writeFile( "/js/controls.js", JS_CONTROLS );
|
2019-03-03 21:21:21 +00:00
|
|
|
writeFile( "/js/slider.js", JS_SLIDER );;
|
|
|
|
writeFile( "/js/tabbedcontent.js", JS_TABBEDCONTENT );
|
2017-11-29 13:36:19 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "Done Initializing filesystem :-)" );
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if ( this->verbosity ) {
|
|
|
|
listDir( "/", 1 );
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
#endif
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
SPIFFS.end();
|
2017-11-28 12:48:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 13:00:53 +00:00
|
|
|
// Handle Websockets Communication
|
2019-03-03 20:13:45 +00:00
|
|
|
void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
|
|
|
|
AwsEventType type, void* arg, uint8_t* data, size_t len ) {
|
|
|
|
switch ( type ) {
|
2018-11-26 17:25:10 +00:00
|
|
|
case WS_EVT_DISCONNECT: {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "Disconnected!\n" );
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
break;
|
2017-12-26 20:10:54 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
case WS_EVT_PONG: {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "Received PONG!\n" );
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
break;
|
2017-12-26 20:10:54 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
case WS_EVT_ERROR: {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.printf( "WebSocket Error!\n" );
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
break;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
case WS_EVT_CONNECT: {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.print( "Connected: " );
|
|
|
|
Serial.println( client->id() );
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
2018-05-13 17:44:56 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
ESPUI.jsonDom( client );
|
|
|
|
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "JSON Data Sent to Client!" );
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
case WS_EVT_DATA: {
|
|
|
|
String msg = "";
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
for ( size_t i = 0; i < len; i++ ) {
|
|
|
|
msg += ( char )data[i];
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
2019-03-04 19:49:18 +00:00
|
|
|
uint16_t id = msg.substring( msg.lastIndexOf( ':' ) + 1 ).toInt();
|
2018-05-13 17:44:56 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
if ( ESPUI.verbosity >= Verbosity::VerboseJSON ) {
|
|
|
|
Serial.print("WS rec: ");
|
|
|
|
Serial.println( msg );
|
|
|
|
Serial.print( "WS recognised ID: " );
|
|
|
|
Serial.println( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
Control* c = ESPUI.getControl( id );
|
|
|
|
|
|
|
|
if ( c == nullptr ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.print( "No control found for ID " );
|
|
|
|
Serial.println( id );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2019-03-04 19:49:18 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( c->callback == nullptr ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
2019-03-04 19:49:18 +00:00
|
|
|
Serial.print( "No callback found for ID " );
|
|
|
|
Serial.println( id );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-04 19:49:18 +00:00
|
|
|
|
|
|
|
if ( msg.startsWith( "bdown:" ) ) {
|
|
|
|
c->callback( c, B_DOWN );
|
|
|
|
} else if ( msg.startsWith( "bup:" ) ) {
|
|
|
|
c->callback( c, B_UP );
|
|
|
|
} else if ( msg.startsWith( "pfdown:" ) ) {
|
|
|
|
c->callback( c, P_FOR_DOWN );
|
|
|
|
} else if ( msg.startsWith( "pfup:" ) ) {
|
|
|
|
c->callback( c, P_FOR_UP );
|
|
|
|
} else if ( msg.startsWith( "pldown:" ) ) {
|
|
|
|
c->callback( c, P_LEFT_DOWN );
|
|
|
|
} else if ( msg.startsWith( "plup:" ) ) {
|
|
|
|
c->callback( c, P_LEFT_UP );
|
|
|
|
} else if ( msg.startsWith( "prdown:" ) ) {
|
|
|
|
c->callback( c, P_RIGHT_DOWN );
|
|
|
|
} else if ( msg.startsWith( "prup:" ) ) {
|
|
|
|
c->callback( c, P_RIGHT_UP );
|
|
|
|
} else if ( msg.startsWith( "pbdown:" ) ) {
|
|
|
|
c->callback( c, P_BACK_DOWN );
|
|
|
|
} else if ( msg.startsWith( "pbup:" ) ) {
|
|
|
|
c->callback( c, P_BACK_UP );
|
|
|
|
} else if ( msg.startsWith( "pcdown:" ) ) {
|
|
|
|
c->callback( c, P_CENTER_DOWN );
|
|
|
|
} else if ( msg.startsWith( "pcup:" ) ) {
|
|
|
|
c->callback( c, P_CENTER_UP );
|
|
|
|
} else if ( msg.startsWith( "sactive:" ) ) {
|
|
|
|
ESPUI.updateSwitcher( c->id, true );
|
|
|
|
c->callback( c, S_ACTIVE );
|
|
|
|
} else if ( msg.startsWith( "sinactive:" ) ) {
|
|
|
|
ESPUI.updateSwitcher( c->id, false );
|
|
|
|
c->callback( c, S_INACTIVE );
|
|
|
|
} else if ( msg.startsWith( "slvalue:" ) ) {
|
|
|
|
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
|
|
|
|
ESPUI.updateSlider( c->id, value, client->id() );
|
|
|
|
c->callback( c, SL_VALUE );
|
|
|
|
} else if ( msg.startsWith( "nvalue:" ) ) {
|
|
|
|
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
|
|
|
|
ESPUI.updateNumber( c->id, value, client->id() );
|
|
|
|
c->callback( c, N_VALUE );
|
|
|
|
} else if ( msg.startsWith( "tvalue:" ) ) {
|
|
|
|
String value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) );
|
|
|
|
ESPUI.updateText( c->id, value, client->id() );
|
|
|
|
c->callback( c, T_VALUE );
|
2019-03-04 20:07:39 +00:00
|
|
|
} else if ( msg.startsWith( "svalue:" ) ) {
|
|
|
|
String value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) );
|
|
|
|
ESPUI.updateSelect( c->id, value, client->id() );
|
|
|
|
c->callback( c, S_VALUE );
|
2019-03-04 19:49:18 +00:00
|
|
|
} else {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println( "Malformated message from the websocket" );
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-26 17:25:10 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
2017-10-16 22:10:48 +00:00
|
|
|
|
2019-03-03 22:22:01 +00:00
|
|
|
uint16_t ESPUIClass::addControl( ControlType type, const char* label,
|
|
|
|
String value, ControlColor color,
|
2019-03-04 19:49:18 +00:00
|
|
|
uint16_t parentControl,
|
|
|
|
void ( *callback )( Control*, int )
|
2019-03-03 22:22:01 +00:00
|
|
|
) {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->getControl( label ) != nullptr ) {
|
2019-03-03 20:50:06 +00:00
|
|
|
if ( this->verbosity ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
Serial.println( "UI ERROR: Element " + String( label ) +
|
2019-03-03 20:50:06 +00:00
|
|
|
" exists, skipping creating element!" );
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2018-05-13 17:39:21 +00:00
|
|
|
return -1;
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:22:01 +00:00
|
|
|
Control* control = new Control( type, label, callback, value, color, parentControl );
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
if ( this->controls == nullptr ) {
|
|
|
|
this->controls = control;
|
|
|
|
} else {
|
|
|
|
Control* iterator = this->controls;
|
|
|
|
|
|
|
|
while ( iterator->next != nullptr ) {
|
|
|
|
iterator = iterator->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator->next = control;
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
return control->id;
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
int ESPUIClass::label( const char* label, ControlColor color, String value ) {
|
2019-03-03 20:50:06 +00:00
|
|
|
return addControl( ControlType::Label, label, value, color );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-28 12:48:58 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
int ESPUIClass::graph( const char* label, ControlColor color ) {
|
2019-03-03 20:50:06 +00:00
|
|
|
return addControl( ControlType::Graph, label, "", color );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
// TODO: this still needs a range setting
|
2019-03-04 19:49:18 +00:00
|
|
|
int ESPUIClass::slider( const char* label, void ( *callback )( Control*, int ),
|
2019-03-03 20:13:45 +00:00
|
|
|
ControlColor color, String value ) {
|
2019-03-04 20:08:52 +00:00
|
|
|
return addControl( ControlType::Slider, label, "", color, Control::noParent, callback );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 16:22:02 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
int ESPUIClass::button( const char* label, void ( *callback )( Control*, int ),
|
2019-03-03 20:13:45 +00:00
|
|
|
ControlColor color, String value ) {
|
2019-03-04 19:49:18 +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
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
int ESPUIClass::switcher( const char* label, bool startState,
|
2019-03-04 19:49:18 +00:00
|
|
|
void ( *callback )( Control*, int ), ControlColor color ) {
|
|
|
|
return addControl( ControlType::Switcher, label, "", color, Control::noParent, callback );
|
2017-10-16 13:00:53 +00:00
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
int ESPUIClass::pad( const char* label, bool center,
|
2019-03-04 19:49:18 +00:00
|
|
|
void ( *callback )( Control*, int ), ControlColor color ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( center ) {
|
2019-03-04 19:49:18 +00:00
|
|
|
return addControl( ControlType::PadWithCenter, label, "", color, Control::noParent, callback );
|
2019-03-03 20:13:45 +00:00
|
|
|
} else {
|
2019-03-04 19:49:18 +00:00
|
|
|
return addControl( ControlType::Pad, label, "", color, Control::noParent, callback );
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
// TODO: min and max need to be saved, they also need to be sent to the frontend
|
2019-03-04 19:49:18 +00:00
|
|
|
int ESPUIClass::number( const char* label, void ( *callback )( Control*, int ),
|
2019-03-03 20:13:45 +00:00
|
|
|
ControlColor color, int number, int min, int max ) {
|
2019-03-04 19:49:18 +00:00
|
|
|
return addControl( ControlType::Number, label, String( number ), color, Control::noParent, callback );
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
int ESPUIClass::text( const char* label, void ( *callback )( Control*, int ),
|
2019-03-03 20:13:45 +00:00
|
|
|
ControlColor color, String value ) {
|
2019-03-04 19:49:18 +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
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
Control* ESPUIClass::getControl( uint16_t id ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
Control* control = this->controls;
|
|
|
|
|
|
|
|
while ( control != nullptr ) {
|
|
|
|
if ( control->id == id ) {
|
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = control->next;
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control* ESPUIClass::getControl( String label ) {
|
|
|
|
Control* control = this->controls;
|
|
|
|
|
|
|
|
while ( control != nullptr ) {
|
|
|
|
if ( String( control->label ) == label ) {
|
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = control->next;
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
return nullptr;
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateControl( Control* control, String value, int clientId ) {
|
|
|
|
if ( control ) {
|
|
|
|
control->value = value;
|
2017-10-19 11:46:47 +00:00
|
|
|
String json;
|
|
|
|
StaticJsonBuffer<200> jsonBuffer;
|
2019-03-03 20:13:45 +00:00
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
|
|
|
|
root["type"] = ( int )control->type + ControlType::UpdateOffset;
|
|
|
|
root["value"] = control->value;
|
|
|
|
root["id"] = control->id;
|
|
|
|
root.printTo( json );
|
|
|
|
|
|
|
|
if ( clientId > 0 ) {
|
|
|
|
// This is a hacky workaround because ESPAsyncWebServer does not have a function
|
|
|
|
// like this and it's clients array is private
|
|
|
|
int tryId = 0;
|
|
|
|
|
|
|
|
for ( int count = 0; count < this->ws->count(); ) {
|
|
|
|
if ( this->ws->hasClient( tryId ) ) {
|
|
|
|
if ( clientId != tryId ) {
|
|
|
|
this->ws->client( tryId )->text( json );
|
|
|
|
|
|
|
|
if ( this->verbosity >= Verbosity::VerboseJSON ) {
|
|
|
|
Serial.println( json );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tryId++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( this->verbosity >= Verbosity::VerboseJSON ) {
|
|
|
|
Serial.println( json );
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ws->textAll( json );
|
|
|
|
}
|
2017-10-19 15:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateControl( uint16_t id, String value, int clientId ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
Control* control = getControl( id );
|
2017-10-19 15:30:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( control ) {
|
|
|
|
updateControl( control, value, clientId );
|
|
|
|
} else {
|
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( String( "Error: There is no control with ID " ) + String( id ) );
|
|
|
|
}
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateControl( String label, String value, int clientId ) {
|
|
|
|
Control* control = getControl( label );
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( control ) {
|
|
|
|
updateControl( control, value, clientId );
|
2018-05-13 18:19:29 +00:00
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( String( "Error: There is no control with label " ) + label );
|
|
|
|
}
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::print( uint16_t id, String value ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
updateControl( id, value );
|
2018-12-02 10:41:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::print( String label, String value ) {
|
2019-03-03 20:46:38 +00:00
|
|
|
updateControl( label, value );
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateLabel( uint16_t id, String value ) {
|
2019-03-03 20:46:38 +00:00
|
|
|
updateControl( id, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPUIClass::updateLabel( String label, String value ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
updateControl( label, value );
|
2018-05-13 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateSlider( uint16_t id, int nValue, int clientId ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
updateControl( id, String( nValue ), clientId );
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateSlider( String label, int nValue, int clientId ) {
|
|
|
|
updateControl( label, String( nValue ), clientId );
|
2017-11-29 10:32:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateSwitcher( uint16_t id, bool nValue, int clientId ) {
|
2019-03-04 19:49:18 +00:00
|
|
|
updateControl( id, String( nValue ? "1" : "0" ), clientId );
|
2017-11-29 10:32:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateSwitcher( String label, bool nValue, int clientId ) {
|
2019-03-04 19:49:18 +00:00
|
|
|
updateControl( label, String( nValue ? "1" : "0" ), clientId );
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateNumber( uint16_t id, int number, int clientId ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
updateControl( id, String( number ), clientId );
|
2018-11-26 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateNumber( String label, int number, int clientId ) {
|
|
|
|
updateControl( label, String( number ), clientId );
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void ESPUIClass::updateText( uint16_t id, String text, int clientId ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
updateControl( id, text, clientId );
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::updateText( String label, String text, int clientId ) {
|
|
|
|
updateControl( label, text, clientId );
|
2017-11-13 15:10:56 +00:00
|
|
|
}
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2019-03-04 20:07:39 +00:00
|
|
|
void ESPUIClass::updateSelect( uint16_t id, String text, int clientId ) {
|
|
|
|
updateControl( id, text, clientId );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPUIClass::updateSelect( String label, String text, int clientId ) {
|
|
|
|
updateControl( label, text, clientId );
|
|
|
|
}
|
|
|
|
|
2018-12-26 11:35:35 +00:00
|
|
|
/*
|
|
|
|
Convert & Transfer Arduino elements to JSON elements
|
|
|
|
Initially this function used to send the control element data individually.
|
|
|
|
Due to a change in the ESPAsyncWebserver library this had top be changed to be
|
|
|
|
sent as one blob at the beginning. Therefore a new type is used as well
|
|
|
|
*/
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
|
2018-12-26 11:35:35 +00:00
|
|
|
String json;
|
2019-03-03 20:13:45 +00:00
|
|
|
DynamicJsonBuffer jsonBuffer( 2000 );
|
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
root["type"] = ( int )UI_INITIAL_GUI;
|
|
|
|
JsonArray& items = jsonBuffer.createArray();
|
2018-12-26 11:35:35 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
Control* control = this->controls;
|
2018-12-26 11:35:35 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
{
|
|
|
|
JsonObject& item = jsonBuffer.createObject();
|
|
|
|
item["type"] = ( int )UI_TITLE;
|
|
|
|
item["label"] = String( ui_title );
|
|
|
|
items.add( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( control != nullptr ) {
|
|
|
|
JsonObject& item = jsonBuffer.createObject();
|
|
|
|
|
|
|
|
item["id"] = String( control->id );
|
|
|
|
item["type"] = ( int )control->type;
|
|
|
|
item["label"] = String( control->label );
|
|
|
|
item["value"] = String( control->value );
|
|
|
|
item["color"] = ( int )control->color;
|
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
if ( control->parentControl != Control::noParent ) {
|
2019-03-03 22:22:01 +00:00
|
|
|
item["parentControl"] = String( control->parentControl );
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
items.add( item );
|
|
|
|
|
|
|
|
control = control->next;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
2018-12-26 11:35:35 +00:00
|
|
|
|
|
|
|
// Send as one big bunch
|
|
|
|
root["controls"] = items;
|
2019-03-03 20:13:45 +00:00
|
|
|
root.printTo( json );
|
2017-10-16 22:10:48 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity >= Verbosity::VerboseJSON ) {
|
|
|
|
Serial.println( json );
|
|
|
|
}
|
|
|
|
|
|
|
|
client->text( json );
|
2018-12-26 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const char* password ) {
|
2017-10-19 11:46:47 +00:00
|
|
|
ui_title = _title;
|
2019-03-03 20:13:45 +00:00
|
|
|
this->basicAuthUsername = username;
|
|
|
|
this->basicAuthPassword = password;
|
2018-01-08 11:26:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( username == nullptr && password == nullptr ) {
|
|
|
|
basicAuth = false;
|
|
|
|
} else {
|
|
|
|
basicAuth = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = new AsyncWebServer( 80 );
|
|
|
|
ws = new AsyncWebSocket( "/ws" );
|
|
|
|
|
|
|
|
if ( !SPIFFS.begin() ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println(
|
2018-11-26 17:25:10 +00:00
|
|
|
"SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
|
2019-03-03 20:13:45 +00:00
|
|
|
"PREPARE YOUR ESP!!!!!!!" );
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:22:26 +00:00
|
|
|
return;
|
2017-11-29 13:36:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
listDir( "/", 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !SPIFFS.exists( "/index.htm" ) ) {
|
|
|
|
if ( ESPUI.verbosity ) {
|
|
|
|
Serial.println(
|
2018-11-26 17:25:10 +00:00
|
|
|
"Please read the README!!!!!!!, Make sure to "
|
2019-03-03 20:13:45 +00:00
|
|
|
"ESPUI.prepareFileSystem() once in an empty sketch" );
|
|
|
|
}
|
|
|
|
|
2017-11-29 13:36:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
ws->onEvent( onWsEvent );
|
|
|
|
server->addHandler( ws );
|
|
|
|
|
|
|
|
if ( basicAuth ) {
|
|
|
|
|
|
|
|
if ( WS_AUTHENTICATION ) {
|
|
|
|
ws->setAuthentication( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword );
|
|
|
|
}
|
|
|
|
|
|
|
|
server->serveStatic( "/", SPIFFS, "/" )
|
|
|
|
.setDefaultFile( "index.htm" )
|
|
|
|
.setAuthentication( username, password );
|
2018-12-26 12:38:38 +00:00
|
|
|
|
|
|
|
} else {
|
2019-03-03 20:13:45 +00:00
|
|
|
server->serveStatic( "/", SPIFFS, "/" )
|
|
|
|
.setDefaultFile( "index.htm" );
|
2018-12-26 12:38:38 +00:00
|
|
|
}
|
2017-10-16 13:00:53 +00:00
|
|
|
|
2017-11-13 15:10:56 +00:00
|
|
|
// Heap for general Servertest
|
2019-03-03 20:13:45 +00:00
|
|
|
server->on( "/heap", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request->send( 200, "text/plain",
|
|
|
|
String( ESP.getFreeHeap() ) + " In SPIFFSmode" );
|
|
|
|
} );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
|
|
|
server->onNotFound(
|
2019-03-03 20:13:45 +00:00
|
|
|
[]( AsyncWebServerRequest * request ) {
|
|
|
|
request->send( 404 );
|
|
|
|
} );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
|
|
|
server->begin();
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "UI Initialized" );
|
|
|
|
}
|
2018-12-26 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
void ESPUIClass::begin( const char* _title, const char* username, const char* password ) {
|
|
|
|
basicAuthUsername = username;
|
|
|
|
basicAuthPassword = password;
|
|
|
|
|
|
|
|
if ( username != nullptr && password != nullptr ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
basicAuth = true;
|
2019-03-03 20:13:45 +00:00
|
|
|
} else {
|
|
|
|
basicAuth = false;
|
2018-12-26 12:38:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 08:35:37 +00:00
|
|
|
ui_title = _title;
|
2018-12-26 12:38:38 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
server = new AsyncWebServer( 80 );
|
|
|
|
ws = new AsyncWebSocket( "/ws" );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
ws->onEvent( onWsEvent );
|
|
|
|
server->addHandler( ws );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( basicAuth && WS_AUTHENTICATION )
|
|
|
|
ws->setAuthentication( username, password );
|
2018-12-26 12:38:38 +00:00
|
|
|
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
server->on( "/", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AsyncWebServerResponse* response =
|
|
|
|
request->beginResponse_P( 200, "text/html", HTML_INDEX );
|
|
|
|
request->send( response );
|
|
|
|
} );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
|
|
|
// Javascript files
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
server->on( "/js/zepto.min.js", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +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 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
server->on( "/js/controls.js", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +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 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
server->on( "/js/slider.js", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +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 );
|
|
|
|
} );
|
2018-05-27 08:35:37 +00:00
|
|
|
|
2019-03-03 21:21:21 +00:00
|
|
|
server->on( "/js/tabbedcontent.js", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncWebServerResponse* response =
|
|
|
|
request->beginResponse_P(
|
|
|
|
200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof( JS_TABBEDCONTENT_GZIP ) );
|
|
|
|
response->addHeader( "Content-Encoding", "gzip" );
|
|
|
|
request->send( response );
|
|
|
|
} );
|
|
|
|
|
2018-05-27 08:35:37 +00:00
|
|
|
// Stylesheets
|
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
server->on( "/css/style.css", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +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
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
server->on(
|
2019-03-03 20:13:45 +00:00
|
|
|
"/css/normalize.css", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
|
|
|
return request->requestAuthentication();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Heap for general Servertest
|
2019-03-03 20:13:45 +00:00
|
|
|
server->on( "/heap", HTTP_GET, []( AsyncWebServerRequest * request ) {
|
|
|
|
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
|
2018-12-26 12:38:38 +00:00
|
|
|
return request->requestAuthentication();
|
2019-03-03 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request->send( 200, "text/plain",
|
|
|
|
String( ESP.getFreeHeap() ) + " In Memorymode" );
|
|
|
|
} );
|
2017-10-16 13:00:53 +00:00
|
|
|
|
2017-11-13 15:10:56 +00:00
|
|
|
server->onNotFound(
|
2019-03-03 20:13:45 +00:00
|
|
|
[]( AsyncWebServerRequest * request ) {
|
|
|
|
request->send( 404 );
|
|
|
|
} );
|
2017-10-16 13:00:53 +00:00
|
|
|
|
|
|
|
server->begin();
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
if ( this->verbosity ) {
|
|
|
|
Serial.println( "UI Initialized" );
|
|
|
|
}
|
|
|
|
}
|