1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 09:10:54 +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/controls.js", JS_CONTROLS);
writeFile( "/js/slider.js", JS_SLIDER );;
writeFile("/js/slider.js", JS_SLIDER);
;
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
if (this->verbosity) {
@ -290,8 +291,7 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
if (ESPUI.verbosity) {
Serial.println("JSON Data Sent to Client!");
}
}
break;
} break;
case WS_EVT_DATA: {
String msg = "";
@ -377,8 +377,7 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
Serial.println("Malformated message from the websocket");
}
}
}
break;
} break;
default:
break;
@ -388,9 +387,9 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
uint16_t ESPUIClass::addControl(ControlType type, const char* label,
String value, ControlColor color,
uint16_t parentControl,
void ( *callback )( Control*, int )
) {
Control* control = new Control( type, label, callback, value, color, parentControl );
void (*callback)(Control*, int)) {
Control* control =
new Control(type, label, callback, value, color, parentControl);
if (this->controls == nullptr) {
this->controls = control;
@ -415,43 +414,47 @@ int ESPUIClass::graph( const char* label, ControlColor color ) {
return addControl(ControlType::Graph, label, "", color);
}
// TODO: this still needs a range setting
int ESPUIClass::slider(const char* label, void (*callback)(Control*, int),
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),
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,
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,
void (*callback)(Control*, int), ControlColor color) {
if (center) {
return addControl( ControlType::PadWithCenter, label, "", color, Control::noParent, callback );
return addControl(ControlType::PadWithCenter, label, "", color,
Control::noParent, callback);
} 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),
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),
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* control = this->controls;
@ -467,20 +470,23 @@ Control* ESPUIClass::getControl( uint16_t id ) {
}
void ESPUIClass::updateControl(Control* control, int clientId) {
if ( control ) {
if (!control) {
return;
}
String json;
DynamicJsonBuffer jsonBuffer( 2000 );
JsonObject& root = jsonBuffer.createObject();
DynamicJsonDocument document(2000);
JsonObject root = document.to<JsonObject>();
root["type"] = (int)control->type + ControlType::UpdateOffset;
root["value"] = control->value;
root["id"] = control->id;
root["color"] = (int)control->color;
root.printTo( json );
serializeJson(document, 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
// 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();) {
@ -506,7 +512,6 @@ void ESPUIClass::updateControl( Control* control, int clientId ) {
this->ws->textAll(json);
}
}
}
void ESPUIClass::updateControl(uint16_t id, int clientId) {
Control* control = getControl(id);
@ -515,17 +520,20 @@ void ESPUIClass::updateControl( uint16_t id, int clientId ) {
updateControl(control, clientId);
} else {
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) {
if ( control ) {
if (!control) {
return;
}
control->value = value;
updateControl(control, clientId);
}
}
void ESPUIClass::updateControl(uint16_t id, String value, int clientId) {
Control* control = getControl(id);
@ -534,14 +542,13 @@ void ESPUIClass::updateControl( uint16_t id, String value, int clientId ) {
updateControl(control, value, clientId);
} else {
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 ) {
updateControl( id, value );
}
void ESPUIClass::print(uint16_t id, String value) { updateControl(id, value); }
void ESPUIClass::updateLabel(uint16_t id, String 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) {
String json;
DynamicJsonBuffer jsonBuffer( 8000 );
JsonObject& root = jsonBuffer.createObject();
root["type"] = ( int )UI_INITIAL_GUI;
JsonArray& items = jsonBuffer.createArray();
DynamicJsonDocument document(8000);
document["type"] = (int)UI_INITIAL_GUI;
JsonArray items = document.createNestedArray("controls");
Control* control = this->controls;
{
JsonObject& item = jsonBuffer.createObject();
item["type"] = ( int )UI_TITLE;
item["label"] = String( ui_title );
items.add( item );
}
JsonObject titleItem = items.createNestedObject();
titleItem["type"] = (int)UI_TITLE;
titleItem["label"] = String(ui_title);
while (control != nullptr) {
JsonObject& item = jsonBuffer.createObject();
JsonObject item = items.createNestedObject();
item["id"] = String(control->id);
item["type"] = (int)control->type;
@ -602,7 +605,8 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
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 (ESPUI.getControl(control->parentControl)->value == control->value) {
item["selected"] = "selected";
@ -611,14 +615,11 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
}
}
items.add( item );
control = control->next;
}
// Send as one big bunch
root["controls"] = items;
root.printTo( json );
serializeJson(document, json);
if (this->verbosity >= Verbosity::VerboseJSON) {
Serial.println(json);
@ -627,7 +628,8 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
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;
this->basicAuthUsername = username;
this->basicAuthPassword = password;
@ -669,7 +671,6 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
server->addHandler(ws);
if (basicAuth) {
if (WS_AUTHENTICATION) {
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
}
@ -679,13 +680,13 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
.setAuthentication(username, password);
} else {
server->serveStatic( "/", SPIFFS, "/" )
.setDefaultFile( "index.htm" );
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
}
// Heap for general Servertest
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();
}
@ -694,9 +695,7 @@ void ESPUIClass::beginSPIFFS( const char* _title, const char* username, const ch
});
server->onNotFound(
[]( AsyncWebServerRequest * request ) {
request->send( 404 );
} );
[](AsyncWebServerRequest* request) { request->send(404); });
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;
basicAuthPassword = password;
@ -723,12 +723,11 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
ws->onEvent(onWsEvent);
server->addHandler(ws);
if ( basicAuth && WS_AUTHENTICATION )
ws->setAuthentication( username, password );
if (basicAuth && WS_AUTHENTICATION) ws->setAuthentication(username, password);
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();
}
@ -740,49 +739,53 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Javascript files
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();
}
AsyncWebServerResponse* response =
request->beginResponse_P(
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 ) ) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication();
}
AsyncWebServerResponse* response =
request->beginResponse_P(
200, "application/javascript", JS_CONTROLS_GZIP, sizeof( JS_CONTROLS_GZIP ) );
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 ) ) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication();
}
AsyncWebServerResponse* response =
request->beginResponse_P(
AsyncWebServerResponse* response = request->beginResponse_P(
200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on( "/js/tabbedcontent.js", HTTP_GET, []( AsyncWebServerRequest * request ) {
if ( ESPUI.basicAuth && !request->authenticate( ESPUI.basicAuthUsername, ESPUI.basicAuthPassword ) ) {
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 ) );
AsyncWebServerResponse* response = request->beginResponse_P(
200, "application/javascript", JS_TABBEDCONTENT_GZIP,
sizeof(JS_TABBEDCONTENT_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
@ -790,12 +793,12 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Stylesheets
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();
}
AsyncWebServerResponse* response =
request->beginResponse_P(
AsyncWebServerResponse* response = request->beginResponse_P(
200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
@ -803,12 +806,13 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
server->on(
"/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();
}
AsyncWebServerResponse* response =
request->beginResponse_P(
AsyncWebServerResponse* response = request->beginResponse_P(
200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
@ -816,7 +820,8 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
// Heap for general Servertest
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();
}
@ -825,9 +830,7 @@ void ESPUIClass::begin( const char* _title, const char* username, const char* pa
});
server->onNotFound(
[]( AsyncWebServerRequest * request ) {
request->send( 404 );
} );
[](AsyncWebServerRequest* request) { request->send(404); });
server->begin();