1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 14:20:53 +00:00

Use ArduinoJson 6.10.0

This commit is contained in:
Lukas Bachschwell 2019-03-24 15:44:27 +01:00
parent fd12f7ad2a
commit b9a087c169

View File

@ -233,7 +233,8 @@ void ESPUIClass::prepareFileSystem() {
writeFile("/js/zepto.min.js", JS_ZEPTO); writeFile("/js/zepto.min.js", JS_ZEPTO);
writeFile("/js/controls.js", JS_CONTROLS); writeFile("/js/controls.js", JS_CONTROLS);
writeFile( "/js/slider.js", JS_SLIDER );; writeFile("/js/slider.js", JS_SLIDER);
;
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT); writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
if (this->verbosity) { if (this->verbosity) {
@ -290,8 +291,7 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.println("JSON Data Sent to Client!"); Serial.println("JSON Data Sent to Client!");
} }
} } break;
break;
case WS_EVT_DATA: { case WS_EVT_DATA: {
String msg = ""; String msg = "";
@ -377,8 +377,7 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
Serial.println("Malformated message from the websocket"); Serial.println("Malformated message from the websocket");
} }
} }
} } break;
break;
default: default:
break; break;
@ -388,9 +387,9 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
uint16_t ESPUIClass::addControl(ControlType type, const char* label, uint16_t ESPUIClass::addControl(ControlType type, const char* label,
String value, ControlColor color, String value, ControlColor color,
uint16_t parentControl, uint16_t parentControl,
void ( *callback )( Control*, int ) void (*callback)(Control*, int)) {
) { Control* control =
Control* control = new Control( type, label, callback, value, color, parentControl ); new Control(type, label, callback, value, color, parentControl);
if (this->controls == nullptr) { if (this->controls == nullptr) {
this->controls = control; this->controls = control;
@ -415,43 +414,47 @@ int ESPUIClass::graph( const char* label, ControlColor color ) {
return addControl(ControlType::Graph, label, "", color); return addControl(ControlType::Graph, label, "", color);
} }
// TODO: this still needs a range setting
int ESPUIClass::slider(const char* label, void (*callback)(Control*, int), int ESPUIClass::slider(const char* label, void (*callback)(Control*, int),
ControlColor color, String value) { ControlColor color, String value) {
return addControl( ControlType::Slider, label, "", color, Control::noParent, callback ); return addControl(ControlType::Slider, label, "", color, Control::noParent,
callback);
} }
int ESPUIClass::button(const char* label, void (*callback)(Control*, int), int ESPUIClass::button(const char* label, void (*callback)(Control*, int),
ControlColor color, String value) { ControlColor color, String value) {
return addControl( ControlType::Button, label, value, color, Control::noParent, callback ); return addControl(ControlType::Button, label, value, color, Control::noParent,
callback);
} }
int ESPUIClass::switcher(const char* label, bool startState, int ESPUIClass::switcher(const char* label, bool startState,
void (*callback)(Control*, int), ControlColor color) { void (*callback)(Control*, int), ControlColor color) {
return addControl( ControlType::Switcher, label, "", color, Control::noParent, callback ); return addControl(ControlType::Switcher, label, "", color, Control::noParent,
callback);
} }
int ESPUIClass::pad(const char* label, bool center, int ESPUIClass::pad(const char* label, bool center,
void (*callback)(Control*, int), ControlColor color) { void (*callback)(Control*, int), ControlColor color) {
if (center) { if (center) {
return addControl( ControlType::PadWithCenter, label, "", color, Control::noParent, callback ); return addControl(ControlType::PadWithCenter, label, "", color,
Control::noParent, callback);
} else { } else {
return addControl( ControlType::Pad, label, "", color, Control::noParent, callback ); return addControl(ControlType::Pad, label, "", color, Control::noParent,
callback);
} }
} }
// TODO: min and max need to be saved, they also need to be sent to the frontend
int ESPUIClass::number(const char* label, void (*callback)(Control*, int), int ESPUIClass::number(const char* label, void (*callback)(Control*, int),
ControlColor color, int number, int min, int max) { ControlColor color, int number, int min, int max) {
return addControl( ControlType::Number, label, String( number ), color, Control::noParent, callback ); return addControl(ControlType::Number, label, String(number), color,
Control::noParent, callback);
} }
int ESPUIClass::text(const char* label, void (*callback)(Control*, int), int ESPUIClass::text(const char* label, void (*callback)(Control*, int),
ControlColor color, String value) { ControlColor color, String value) {
return addControl( ControlType::Text, label, value, color, Control::noParent, callback ); return addControl(ControlType::Text, label, value, color, Control::noParent,
callback);
} }
Control* ESPUIClass::getControl(uint16_t id) { Control* ESPUIClass::getControl(uint16_t id) {
Control* control = this->controls; Control* control = this->controls;
@ -467,20 +470,23 @@ Control* ESPUIClass::getControl( uint16_t id ) {
} }
void ESPUIClass::updateControl(Control* control, int clientId) { void ESPUIClass::updateControl(Control* control, int clientId) {
if ( control ) { if (!control) {
return;
}
String json; String json;
DynamicJsonBuffer jsonBuffer( 2000 ); DynamicJsonDocument document(2000);
JsonObject& root = jsonBuffer.createObject(); JsonObject root = document.to<JsonObject>();
root["type"] = (int)control->type + ControlType::UpdateOffset; root["type"] = (int)control->type + ControlType::UpdateOffset;
root["value"] = control->value; root["value"] = control->value;
root["id"] = control->id; root["id"] = control->id;
root["color"] = (int)control->color; root["color"] = (int)control->color;
root.printTo( json ); serializeJson(document, json);
if (clientId > 0) { if (clientId > 0) {
// This is a hacky workaround because ESPAsyncWebServer does not have a function // This is a hacky workaround because ESPAsyncWebServer does not have a
// like this and it's clients array is private // function like this and it's clients array is private
int tryId = 0; int tryId = 0;
for (int count = 0; count < this->ws->count();) { for (int count = 0; count < this->ws->count();) {
@ -506,7 +512,6 @@ void ESPUIClass::updateControl( Control* control, int clientId ) {
this->ws->textAll(json); this->ws->textAll(json);
} }
} }
}
void ESPUIClass::updateControl(uint16_t id, int clientId) { void ESPUIClass::updateControl(uint16_t id, int clientId) {
Control* control = getControl(id); Control* control = getControl(id);
@ -515,17 +520,20 @@ void ESPUIClass::updateControl( uint16_t id, int clientId ) {
updateControl(control, clientId); updateControl(control, clientId);
} else { } else {
if (this->verbosity) { if (this->verbosity) {
Serial.println( String( "Error: There is no control with ID " ) + String( id ) ); Serial.println(String("Error: There is no control with ID ") +
String(id));
} }
} }
} }
void ESPUIClass::updateControl(Control* control, String value, int clientId) { void ESPUIClass::updateControl(Control* control, String value, int clientId) {
if ( control ) { if (!control) {
return;
}
control->value = value; control->value = value;
updateControl(control, clientId); updateControl(control, clientId);
} }
}
void ESPUIClass::updateControl(uint16_t id, String value, int clientId) { void ESPUIClass::updateControl(uint16_t id, String value, int clientId) {
Control* control = getControl(id); Control* control = getControl(id);
@ -534,14 +542,13 @@ void ESPUIClass::updateControl( uint16_t id, String value, int clientId ) {
updateControl(control, value, clientId); updateControl(control, value, clientId);
} else { } else {
if (this->verbosity) { if (this->verbosity) {
Serial.println( String( "Error: There is no control with ID " ) + String( id ) ); Serial.println(String("Error: There is no control with ID ") +
String(id));
} }
} }
} }
void ESPUIClass::print( uint16_t id, String value ) { void ESPUIClass::print(uint16_t id, String value) { updateControl(id, value); }
updateControl( id, value );
}
void ESPUIClass::updateLabel(uint16_t id, String value) { void ESPUIClass::updateLabel(uint16_t id, String value) {
updateControl(id, value); updateControl(id, value);
@ -575,22 +582,18 @@ sent as one blob at the beginning. Therefore a new type is used as well
*/ */
void ESPUIClass::jsonDom(AsyncWebSocketClient* client) { void ESPUIClass::jsonDom(AsyncWebSocketClient* client) {
String json; String json;
DynamicJsonBuffer jsonBuffer( 8000 ); DynamicJsonDocument document(8000);
JsonObject& root = jsonBuffer.createObject(); document["type"] = (int)UI_INITIAL_GUI;
root["type"] = ( int )UI_INITIAL_GUI; JsonArray items = document.createNestedArray("controls");
JsonArray& items = jsonBuffer.createArray();
Control* control = this->controls; Control* control = this->controls;
{ JsonObject titleItem = items.createNestedObject();
JsonObject& item = jsonBuffer.createObject(); titleItem["type"] = (int)UI_TITLE;
item["type"] = ( int )UI_TITLE; titleItem["label"] = String(ui_title);
item["label"] = String( ui_title );
items.add( item );
}
while (control != nullptr) { while (control != nullptr) {
JsonObject& item = jsonBuffer.createObject(); JsonObject item = items.createNestedObject();
item["id"] = String(control->id); item["id"] = String(control->id);
item["type"] = (int)control->type; item["type"] = (int)control->type;
@ -602,7 +605,8 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
item["parentControl"] = String(control->parentControl); item["parentControl"] = String(control->parentControl);
} }
// special case for selects: to preselect an option, you have to add "selected" to <option> // special case for selects: to preselect an option, you have to add
// "selected" to <option>
if (control->type == ControlType::Option) { if (control->type == ControlType::Option) {
if (ESPUI.getControl(control->parentControl)->value == control->value) { if (ESPUI.getControl(control->parentControl)->value == control->value) {
item["selected"] = "selected"; item["selected"] = "selected";
@ -611,14 +615,11 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
} }
} }
items.add( item );
control = control->next; control = control->next;
} }
// Send as one big bunch // Send as one big bunch
root["controls"] = items; serializeJson(document, json);
root.printTo( json );
if (this->verbosity >= Verbosity::VerboseJSON) { if (this->verbosity >= Verbosity::VerboseJSON) {
Serial.println(json); Serial.println(json);
@ -627,7 +628,8 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
client->text(json); client->text(json);
} }
void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const char* password ) { void ESPUIClass::beginSPIFFS(const char* _title, const char* username,
const char* password) {
ui_title = _title; ui_title = _title;
this->basicAuthUsername = username; this->basicAuthUsername = username;
this->basicAuthPassword = password; this->basicAuthPassword = password;
@ -669,7 +671,6 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
server->addHandler(ws); server->addHandler(ws);
if (basicAuth) { if (basicAuth) {
if (WS_AUTHENTICATION) { if (WS_AUTHENTICATION) {
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword); ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
} }
@ -679,13 +680,13 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
.setAuthentication(username, password); .setAuthentication(username, password);
} else { } else {
server->serveStatic( "/", SPIFFS, "/" ) server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
.setDefaultFile( "index.htm" );
} }
// Heap for general Servertest // Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
@ -694,9 +695,7 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
}); });
server->onNotFound( server->onNotFound(
[]( AsyncWebServerRequest * request ) { [](AsyncWebServerRequest* request) { request->send(404); });
request->send( 404 );
} );
server->begin(); server->begin();
@ -705,7 +704,8 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
} }
} }
void ESPUIClass::begin( const char* _title, const char* username, const char* password ) { void ESPUIClass::begin(const char* _title, const char* username,
const char* password) {
basicAuthUsername = username; basicAuthUsername = username;
basicAuthPassword = password; basicAuthPassword = password;
@ -723,12 +723,11 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
ws->onEvent(onWsEvent); ws->onEvent(onWsEvent);
server->addHandler(ws); server->addHandler(ws);
if ( basicAuth && WS_AUTHENTICATION ) if (basicAuth && WS_AUTHENTICATION) ws->setAuthentication(username, password);
ws->setAuthentication( username, password );
server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
@ -740,49 +739,53 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Javascript files // Javascript files
server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response = request->beginResponse_P(
request->beginResponse_P(
200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP)); 200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response =
request->beginResponse_P( request->beginResponse_P(200, "application/javascript",
200, "application/javascript", JS_CONTROLS_GZIP, sizeof( JS_CONTROLS_GZIP ) ); JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response = request->beginResponse_P(
request->beginResponse_P(
200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP)); 200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on( "/js/tabbedcontent.js", HTTP_GET, []( AsyncWebServerRequest * request ) { server->on("/js/tabbedcontent.js", HTTP_GET,
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth &&
!request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response = request->beginResponse_P(
request->beginResponse_P( 200, "application/javascript", JS_TABBEDCONTENT_GZIP,
200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof( JS_TABBEDCONTENT_GZIP ) ); sizeof(JS_TABBEDCONTENT_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
@ -790,12 +793,12 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Stylesheets // Stylesheets
server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response = request->beginResponse_P(
request->beginResponse_P(
200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP)); 200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
@ -803,12 +806,13 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
server->on( server->on(
"/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest* request) { "/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth &&
!request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse* response = request->beginResponse_P(
request->beginResponse_P(
200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP)); 200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
@ -816,7 +820,8 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Heap for general Servertest // Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
@ -825,9 +830,7 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
}); });
server->onNotFound( server->onNotFound(
[]( AsyncWebServerRequest * request ) { [](AsyncWebServerRequest* request) { request->send(404); });
request->send( 404 );
} );
server->begin(); server->begin();