diff --git a/Grbl_Esp32/src/Configuration/JsonGenerator.cpp b/Grbl_Esp32/src/Configuration/JsonGenerator.cpp index f240296d..50fd4d52 100644 --- a/Grbl_Esp32/src/Configuration/JsonGenerator.cpp +++ b/Grbl_Esp32/src/Configuration/JsonGenerator.cpp @@ -115,9 +115,7 @@ namespace Configuration { void JsonGenerator::item(const char* name, IPAddress& value) { enter(name); -#ifdef LATER -// Encode IP address -#endif + _encoder.begin_webui(name, _currentPath, "A", value.toString().c_str()); _encoder.end_object(); leave(); } diff --git a/Grbl_Esp32/src/WebUI/ESPResponse.cpp b/Grbl_Esp32/src/WebUI/ESPResponse.cpp index ce128152..29f49d2d 100644 --- a/Grbl_Esp32/src/WebUI/ESPResponse.cpp +++ b/Grbl_Esp32/src/WebUI/ESPResponse.cpp @@ -99,6 +99,13 @@ namespace WebUI { grbl_send(_client, data); } + void ESPResponseStream::print(const char data) { + char text[2]; + text[0] = data; + text[1] = '\0'; + print(text); + } + void ESPResponseStream::flush() { #if defined(ENABLE_HTTP) && defined(ENABLE_WIFI) if (_webserver) { diff --git a/Grbl_Esp32/src/WebUI/ESPResponse.h b/Grbl_Esp32/src/WebUI/ESPResponse.h index eea5a506..4c07d97f 100644 --- a/Grbl_Esp32/src/WebUI/ESPResponse.h +++ b/Grbl_Esp32/src/WebUI/ESPResponse.h @@ -33,6 +33,7 @@ namespace WebUI { ESPResponseStream(uint8_t client, bool byid = true); ESPResponseStream(); + void print(const char data); void print(const char* data); void println(const char* data); void flush(); diff --git a/Grbl_Esp32/src/WebUI/JSONEncoder.cpp b/Grbl_Esp32/src/WebUI/JSONEncoder.cpp index 77a539a6..e907fca5 100644 --- a/Grbl_Esp32/src/WebUI/JSONEncoder.cpp +++ b/Grbl_Esp32/src/WebUI/JSONEncoder.cpp @@ -5,12 +5,24 @@ #include "JSONEncoder.h" namespace WebUI { - // Constructor that supplies a default falue for "pretty" - JSONencoder::JSONencoder() : JSONencoder(false) {} + // Constructor. If _pretty is true, newlines are + // 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 // 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 // elements as needed, omitting the comma @@ -38,7 +50,11 @@ namespace WebUI { // Private function to add a name enclosed with quotes. void JSONencoder::quoted(const char* s) { add('"'); - str.concat(s); + if (stream) { + stream->print(s); + } else { + str.concat(s); + } add('"'); } @@ -109,9 +125,7 @@ namespace WebUI { // Ends an object with }. void JSONencoder::end_object() { dec_level(); - if (count[level + 1] > 1) { - line(); - } + line(); add('}'); } diff --git a/Grbl_Esp32/src/WebUI/JSONEncoder.h b/Grbl_Esp32/src/WebUI/JSONEncoder.h index 03140015..13b6657a 100644 --- a/Grbl_Esp32/src/WebUI/JSONEncoder.h +++ b/Grbl_Esp32/src/WebUI/JSONEncoder.h @@ -1,5 +1,7 @@ #pragma once +#include "ESPResponse.h" + // Class for creating JSON-encoded strings. namespace WebUI { @@ -7,17 +9,18 @@ namespace WebUI { private: static const int MAX_JSON_LEVEL = 16; - bool pretty; - int level; - String str; - int count[MAX_JSON_LEVEL]; - void add(char c) { str += c; } - void comma_line(); - void comma(); - void quoted(const char* s); - void inc_level(); - void dec_level(); - void line(); + bool pretty; + int level; + String str; + int count[MAX_JSON_LEVEL]; + void add(char c); + void comma_line(); + void comma(); + void quoted(const char* s); + void inc_level(); + void dec_level(); + void line(); + ESPResponseStream* stream; public: // If you don't set _pretty it defaults to false @@ -26,6 +29,9 @@ namespace WebUI { // Constructor; set _pretty true for pretty printing JSONencoder(bool pretty); + // Constructor; set _pretty true for pretty printing + JSONencoder(bool pretty, ESPResponseStream* s); + // begin() starts the encoding process. void begin(); diff --git a/Grbl_Esp32/src/WebUI/WebSettings.cpp b/Grbl_Esp32/src/WebUI/WebSettings.cpp index 0f3553a7..708209f9 100644 --- a/Grbl_Esp32/src/WebUI/WebSettings.cpp +++ b/Grbl_Esp32/src/WebUI/WebSettings.cpp @@ -564,7 +564,7 @@ namespace WebUI { #ifdef ENABLE_WIFI 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_array("AP_LIST"); // An initial async scanNetworks was issued at startup, so there @@ -595,7 +595,7 @@ namespace WebUI { break; } j.end_array(); - webPrint(j.end()); + j.end(); if (espresponse->client() != CLIENT_WEBUI) { espresponse->println(""); } @@ -620,14 +620,18 @@ namespace WebUI { } 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_array("EEPROM"); Configuration::JsonGenerator gen(j); config->group(gen); j.end_array(); - webPrint(j.end()); + j.end(); + if (espresponse->client() != CLIENT_WEBUI) { + espresponse->println(""); + } + return Error::Ok; } @@ -829,7 +833,7 @@ namespace WebUI { } 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_array("files"); listDirJSON(SPIFFS, "/", 4, &j); @@ -837,7 +841,7 @@ namespace WebUI { j.member("total", SPIFFS.totalBytes()); j.member("used", SPIFFS.usedBytes()); j.member("occupation", String(100 * SPIFFS.usedBytes() / SPIFFS.totalBytes())); - webPrint(j.end()); + j.end(); if (espresponse->client() != CLIENT_WEBUI) { webPrintln(""); }