chunked response working a charm

This commit is contained in:
Torsten 2021-06-30 21:36:45 +03:00
parent d2eee55953
commit a0b01f4839
3 changed files with 62 additions and 58 deletions

View File

@ -11,7 +11,7 @@ void collector_loop();
// Set delay between sensor readings. // Set delay between sensor readings.
#define DELAY 3000 //3s , longer and the web requests time-out #define DELAY 3000 //3s , longer and the web requests time-out
#define MINUTE 120 // 6 min average #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 MONTH 750 // 1 month of hourly sample
#define PER_HOUR 10 #define PER_HOUR 10
@ -28,9 +28,6 @@ protected:
void add_month(int from ) ; void add_month(int from ) ;
String one_week( float[] );
String one_month( float[] );
public: public:
float week_in[WEEK]; float week_in[WEEK];
float week_out[WEEK]; float week_out[WEEK];
@ -41,8 +38,8 @@ public:
void add_week(float in , float out) ; void add_week(float in , float out) ;
void add(float in , float out) ; void add(float in , float out) ;
String week_data(); String week_data( int at);
String month_data(); String month_data(int at);
}; };

View File

@ -98,42 +98,19 @@ void Collector::add_month(int from){
} }
String Collector::one_week(float week[]){ String Collector::week_data(int at){
String data = "["; if( at < WEEK){
data += String(week[0] , 1) ; return String(week_in[at] , 2) ;
for( int a = 1; a < WEEK ; a += 1 ) { }else{
data += "," ; return String(week_out[at - WEEK] , 2) ;
data += String(week[a] , 1) ;
} }
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 Collector::month_data(int at){
String data = "["; if( at < MONTH){
data += String(month[0] , 1); return String(month_in[at] , 2) ;
for( int a = 1; a < MONTH ; a += 1 ) { }else{
data += "," ; return String(month_out[at - MONTH] , 2) ;
data += String(month[a], 1) ;
} }
data += "]";
return data ;
}
String Collector::month_data(){
String data = "[";
data += one_month(month_in) ;
// data += "," ;
// data += one_month(month_out) ;
data += "]";
return data ;
} }

View File

@ -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>();
// 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) { void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain;charset=utf-8", "Not found"); request->send(404, "text/plain;charset=utf-8", "Not found");
} }
int week_counter = 0;
int month_counter = 0;
void server_setup(){ void server_setup(){
LittleFS.begin(); LittleFS.begin();
server.on("/weekly", HTTP_ANY, [](AsyncWebServerRequest * request) { server.on("/weekly", HTTP_ANY, [](AsyncWebServerRequest * request) {
DEBUG_OUT.println("weekly"); DEBUG_OUT.println("weekly start");
String data = collector.week_data();
request->send(200, "application/json;charset=utf-8", data); AsyncWebServerResponse *response = request->beginChunkedResponse("application/json", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
data.clear();
}); if(week_counter == 2*WEEK){ // sent all data (ret 0), reset counter
server.on("/monthly", HTTP_ANY, [](AsyncWebServerRequest * request) { week_counter = 0;
String data = collector.month_data(); return 0;
request->send(200, "application/json;charset=utf-8", data); }
data.clear(); 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){ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
DEBUG_OUT.println("Index.html"); DEBUG_OUT.println("Index.html");