From a0b01f4839e96979adc34ad5e3267d2e2dbdb4fd Mon Sep 17 00:00:00 2001 From: Torsten Date: Wed, 30 Jun 2021 21:36:45 +0300 Subject: [PATCH] chunked response working a charm --- fan_control/include/collector.hpp | 9 ++-- fan_control/src/collector.cpp | 43 +++++-------------- fan_control/src/server.cpp | 68 ++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/fan_control/include/collector.hpp b/fan_control/include/collector.hpp index b436565..e9032f1 100644 --- a/fan_control/include/collector.hpp +++ b/fan_control/include/collector.hpp @@ -11,7 +11,7 @@ void collector_loop(); // Set delay between sensor readings. #define DELAY 3000 //3s , longer and the web requests time-out #define MINUTE 120 // 6 min average -#define WEEK 1000 // 4 days worth of 6min sample +#define WEEK 1500 // 7 days worth of 6min sample #define MONTH 750 // 1 month of hourly sample #define PER_HOUR 10 @@ -28,9 +28,6 @@ protected: void add_month(int from ) ; - String one_week( float[] ); - String one_month( float[] ); - public: float week_in[WEEK]; float week_out[WEEK]; @@ -41,8 +38,8 @@ public: void add_week(float in , float out) ; void add(float in , float out) ; - String week_data(); - String month_data(); + String week_data( int at); + String month_data(int at); }; diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp index edc33fd..2a87be7 100644 --- a/fan_control/src/collector.cpp +++ b/fan_control/src/collector.cpp @@ -98,42 +98,19 @@ void Collector::add_month(int from){ } -String Collector::one_week(float week[]){ - String data = "["; - data += String(week[0] , 1) ; - for( int a = 1; a < WEEK ; a += 1 ) { - data += "," ; - data += String(week[a] , 1) ; +String Collector::week_data(int at){ + if( at < WEEK){ + return String(week_in[at] , 2) ; + }else{ + return String(week_out[at - WEEK] , 2) ; } - data += "]"; - return data ; } -String Collector::week_data(){ - String data = "["; - data += one_week(week_in) ; - data += "," ; - // data += one_week(week_out) ; - // data += "]"; - return data; -} -String Collector::one_month(float month[]){ - String data = "["; - data += String(month[0] , 1); - for( int a = 1; a < MONTH ; a += 1 ) { - data += "," ; - data += String(month[a], 1) ; +String Collector::month_data(int at){ + if( at < MONTH){ + return String(month_in[at] , 2) ; + }else{ + return String(month_out[at - MONTH] , 2) ; } - data += "]"; - return data ; -} - -String Collector::month_data(){ - String data = "["; - data += one_month(month_in) ; -// data += "," ; -// data += one_month(month_out) ; - data += "]"; - return data ; } \ No newline at end of file diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index 43ef711..fafbf79 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -21,35 +21,65 @@ String getContentType(String filename){ } - -// AsyncCallbackJsonWebHandler* monthly = new AsyncCallbackJsonWebHandler("/monthly"); -// monthly.onRequest( [](AsyncWebServerRequest *request, JsonVariant &json) { -// const JsonArray& root_array = json.as(); -// JsonArray nested = root_array.createNestedArray(); -// copyArray(collector.month_in , MONTH , nested); -// nested = root_array.createNestedArray(); -// copyArray(collector.month_out , MONTH , nested); -// }); - void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain;charset=utf-8", "Not found"); } +int week_counter = 0; +int month_counter = 0; + void server_setup(){ LittleFS.begin(); server.on("/weekly", HTTP_ANY, [](AsyncWebServerRequest * request) { - DEBUG_OUT.println("weekly"); - String data = collector.week_data(); - request->send(200, "application/json;charset=utf-8", data); - data.clear(); - }); - server.on("/monthly", HTTP_ANY, [](AsyncWebServerRequest * request) { - String data = collector.month_data(); - request->send(200, "application/json;charset=utf-8", data); - data.clear(); + DEBUG_OUT.println("weekly start"); + + AsyncWebServerResponse *response = request->beginChunkedResponse("application/json", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + + if(week_counter == 2*WEEK){ // sent all data (ret 0), reset counter + week_counter = 0; + return 0; + } + String to_send = ""; + if(week_counter == 0 ) to_send = "[["; + else if(week_counter == WEEK) to_send = "],["; + else to_send += ","; + to_send += collector.week_data(week_counter); + if(week_counter == (2*WEEK - 1)) to_send += "]]"; + week_counter++; + //send the data, copy to buffer + to_send.getBytes(buffer, maxLen); + return to_send.length(); + }); + request->send(response); + DEBUG_OUT.println("weekly end"); }); + server.on("/monthly", HTTP_ANY, [](AsyncWebServerRequest * request) { + DEBUG_OUT.println("monthly start"); + + AsyncWebServerResponse *response = request->beginChunkedResponse("application/json", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + + if(month_counter == 2*MONTH){ // sent all data (ret 0), reset counter + month_counter = 0; + return 0; + } + String to_send = ""; + if(month_counter == 0 ) to_send = "[["; + else if(month_counter == MONTH) to_send = "],["; + else to_send += ","; + // getting the acual data, just one point at a time (for simplicity, hope speed is ok) + to_send += collector.month_data(month_counter); + if(month_counter == (2*MONTH - 1)) to_send += "]]"; + month_counter++; + //send the data, copy to buffer + to_send.getBytes(buffer, maxLen); + return to_send.length(); + }); + request->send(response); + DEBUG_OUT.println("monthly done"); + + }); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ DEBUG_OUT.println("Index.html");