mirror of
https://github.com/bdring/Grbl_Esp32.git
synced 2025-09-02 10:53:01 +02:00
Fixed JSON encoding for config
WebUI no longer crashes on IP address encoding
This commit is contained in:
@@ -115,9 +115,7 @@ namespace Configuration {
|
|||||||
|
|
||||||
void JsonGenerator::item(const char* name, IPAddress& value) {
|
void JsonGenerator::item(const char* name, IPAddress& value) {
|
||||||
enter(name);
|
enter(name);
|
||||||
#ifdef LATER
|
_encoder.begin_webui(name, _currentPath, "A", value.toString().c_str());
|
||||||
// Encode IP address
|
|
||||||
#endif
|
|
||||||
_encoder.end_object();
|
_encoder.end_object();
|
||||||
leave();
|
leave();
|
||||||
}
|
}
|
||||||
|
@@ -99,6 +99,13 @@ namespace WebUI {
|
|||||||
grbl_send(_client, data);
|
grbl_send(_client, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESPResponseStream::print(const char data) {
|
||||||
|
char text[2];
|
||||||
|
text[0] = data;
|
||||||
|
text[1] = '\0';
|
||||||
|
print(text);
|
||||||
|
}
|
||||||
|
|
||||||
void ESPResponseStream::flush() {
|
void ESPResponseStream::flush() {
|
||||||
#if defined(ENABLE_HTTP) && defined(ENABLE_WIFI)
|
#if defined(ENABLE_HTTP) && defined(ENABLE_WIFI)
|
||||||
if (_webserver) {
|
if (_webserver) {
|
||||||
|
@@ -33,6 +33,7 @@ namespace WebUI {
|
|||||||
ESPResponseStream(uint8_t client, bool byid = true);
|
ESPResponseStream(uint8_t client, bool byid = true);
|
||||||
ESPResponseStream();
|
ESPResponseStream();
|
||||||
|
|
||||||
|
void print(const char data);
|
||||||
void print(const char* data);
|
void print(const char* data);
|
||||||
void println(const char* data);
|
void println(const char* data);
|
||||||
void flush();
|
void flush();
|
||||||
|
@@ -5,12 +5,24 @@
|
|||||||
#include "JSONEncoder.h"
|
#include "JSONEncoder.h"
|
||||||
|
|
||||||
namespace WebUI {
|
namespace WebUI {
|
||||||
// Constructor that supplies a default falue for "pretty"
|
// Constructor. If _pretty is true, newlines are
|
||||||
JSONencoder::JSONencoder() : JSONencoder(false) {}
|
// inserted into the JSON string for easy reading.
|
||||||
|
JSONencoder::JSONencoder(bool pretty, ESPResponseStream* s) : pretty(pretty), level(0), str(""), stream(s) { count[level] = 0; }
|
||||||
|
|
||||||
// Constructor. If _pretty is true, newlines are
|
// Constructor. If _pretty is true, newlines are
|
||||||
// inserted into the JSON string for easy reading.
|
// inserted into the JSON string for easy reading.
|
||||||
JSONencoder::JSONencoder(bool pretty) : pretty(pretty), level(0), str("") { count[level] = 0; }
|
JSONencoder::JSONencoder(bool pretty) : JSONencoder(pretty, nullptr) {}
|
||||||
|
|
||||||
|
// Constructor that supplies a default falue for "pretty"
|
||||||
|
JSONencoder::JSONencoder() : JSONencoder(false) {}
|
||||||
|
|
||||||
|
void JSONencoder::add(char c) {
|
||||||
|
if (stream) {
|
||||||
|
stream->print(c);
|
||||||
|
} else {
|
||||||
|
str += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Private function to add commas between
|
// Private function to add commas between
|
||||||
// elements as needed, omitting the comma
|
// elements as needed, omitting the comma
|
||||||
@@ -38,7 +50,11 @@ namespace WebUI {
|
|||||||
// Private function to add a name enclosed with quotes.
|
// Private function to add a name enclosed with quotes.
|
||||||
void JSONencoder::quoted(const char* s) {
|
void JSONencoder::quoted(const char* s) {
|
||||||
add('"');
|
add('"');
|
||||||
str.concat(s);
|
if (stream) {
|
||||||
|
stream->print(s);
|
||||||
|
} else {
|
||||||
|
str.concat(s);
|
||||||
|
}
|
||||||
add('"');
|
add('"');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,9 +125,7 @@ namespace WebUI {
|
|||||||
// Ends an object with }.
|
// Ends an object with }.
|
||||||
void JSONencoder::end_object() {
|
void JSONencoder::end_object() {
|
||||||
dec_level();
|
dec_level();
|
||||||
if (count[level + 1] > 1) {
|
line();
|
||||||
line();
|
|
||||||
}
|
|
||||||
add('}');
|
add('}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "ESPResponse.h"
|
||||||
|
|
||||||
// Class for creating JSON-encoded strings.
|
// Class for creating JSON-encoded strings.
|
||||||
|
|
||||||
namespace WebUI {
|
namespace WebUI {
|
||||||
@@ -7,17 +9,18 @@ namespace WebUI {
|
|||||||
private:
|
private:
|
||||||
static const int MAX_JSON_LEVEL = 16;
|
static const int MAX_JSON_LEVEL = 16;
|
||||||
|
|
||||||
bool pretty;
|
bool pretty;
|
||||||
int level;
|
int level;
|
||||||
String str;
|
String str;
|
||||||
int count[MAX_JSON_LEVEL];
|
int count[MAX_JSON_LEVEL];
|
||||||
void add(char c) { str += c; }
|
void add(char c);
|
||||||
void comma_line();
|
void comma_line();
|
||||||
void comma();
|
void comma();
|
||||||
void quoted(const char* s);
|
void quoted(const char* s);
|
||||||
void inc_level();
|
void inc_level();
|
||||||
void dec_level();
|
void dec_level();
|
||||||
void line();
|
void line();
|
||||||
|
ESPResponseStream* stream;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// If you don't set _pretty it defaults to false
|
// If you don't set _pretty it defaults to false
|
||||||
@@ -26,6 +29,9 @@ namespace WebUI {
|
|||||||
// Constructor; set _pretty true for pretty printing
|
// Constructor; set _pretty true for pretty printing
|
||||||
JSONencoder(bool pretty);
|
JSONencoder(bool pretty);
|
||||||
|
|
||||||
|
// Constructor; set _pretty true for pretty printing
|
||||||
|
JSONencoder(bool pretty, ESPResponseStream* s);
|
||||||
|
|
||||||
// begin() starts the encoding process.
|
// begin() starts the encoding process.
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
|
@@ -564,7 +564,7 @@ namespace WebUI {
|
|||||||
|
|
||||||
#ifdef ENABLE_WIFI
|
#ifdef ENABLE_WIFI
|
||||||
static Error listAPs(char* parameter, AuthenticationLevel auth_level) { // ESP410
|
static Error listAPs(char* parameter, AuthenticationLevel auth_level) { // ESP410
|
||||||
JSONencoder j(espresponse->client() != CLIENT_WEBUI);
|
JSONencoder j(espresponse->client() != CLIENT_WEBUI, espresponse);
|
||||||
j.begin();
|
j.begin();
|
||||||
j.begin_array("AP_LIST");
|
j.begin_array("AP_LIST");
|
||||||
// An initial async scanNetworks was issued at startup, so there
|
// An initial async scanNetworks was issued at startup, so there
|
||||||
@@ -595,7 +595,7 @@ namespace WebUI {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
j.end_array();
|
j.end_array();
|
||||||
webPrint(j.end());
|
j.end();
|
||||||
if (espresponse->client() != CLIENT_WEBUI) {
|
if (espresponse->client() != CLIENT_WEBUI) {
|
||||||
espresponse->println("");
|
espresponse->println("");
|
||||||
}
|
}
|
||||||
@@ -620,14 +620,18 @@ namespace WebUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Error listSettings(char* parameter, AuthenticationLevel auth_level) { // ESP400
|
static Error listSettings(char* parameter, AuthenticationLevel auth_level) { // ESP400
|
||||||
JSONencoder j(espresponse->client() != CLIENT_WEBUI);
|
JSONencoder j(espresponse->client() != CLIENT_WEBUI, espresponse);
|
||||||
j.begin();
|
j.begin();
|
||||||
j.begin_array("EEPROM");
|
j.begin_array("EEPROM");
|
||||||
|
|
||||||
Configuration::JsonGenerator gen(j);
|
Configuration::JsonGenerator gen(j);
|
||||||
config->group(gen);
|
config->group(gen);
|
||||||
j.end_array();
|
j.end_array();
|
||||||
webPrint(j.end());
|
j.end();
|
||||||
|
if (espresponse->client() != CLIENT_WEBUI) {
|
||||||
|
espresponse->println("");
|
||||||
|
}
|
||||||
|
|
||||||
return Error::Ok;
|
return Error::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -829,7 +833,7 @@ namespace WebUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Error listLocalFilesJSON(char* parameter, AuthenticationLevel auth_level) { // No ESP command
|
static Error listLocalFilesJSON(char* parameter, AuthenticationLevel auth_level) { // No ESP command
|
||||||
JSONencoder j(espresponse->client() != CLIENT_WEBUI);
|
JSONencoder j(espresponse->client() != CLIENT_WEBUI, espresponse);
|
||||||
j.begin();
|
j.begin();
|
||||||
j.begin_array("files");
|
j.begin_array("files");
|
||||||
listDirJSON(SPIFFS, "/", 4, &j);
|
listDirJSON(SPIFFS, "/", 4, &j);
|
||||||
@@ -837,7 +841,7 @@ namespace WebUI {
|
|||||||
j.member("total", SPIFFS.totalBytes());
|
j.member("total", SPIFFS.totalBytes());
|
||||||
j.member("used", SPIFFS.usedBytes());
|
j.member("used", SPIFFS.usedBytes());
|
||||||
j.member("occupation", String(100 * SPIFFS.usedBytes() / SPIFFS.totalBytes()));
|
j.member("occupation", String(100 * SPIFFS.usedBytes() / SPIFFS.totalBytes()));
|
||||||
webPrint(j.end());
|
j.end();
|
||||||
if (espresponse->client() != CLIENT_WEBUI) {
|
if (espresponse->client() != CLIENT_WEBUI) {
|
||||||
webPrintln("");
|
webPrintln("");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user