checkpoint, switching machine. on the way to chunked response

This commit is contained in:
Torsten Ruger 2021-06-30 12:20:20 +03:00
parent e28df2e41a
commit d2eee55953
5 changed files with 71 additions and 23 deletions

View File

@ -16,7 +16,7 @@
<h1> Monthly </h1> <h1> Monthly </h1>
<div class="ct-chart chart2"></div> <div class="ct-chart chart2"></div>
<script > <script >
var chart;
function load_chart( url , clazz ){ function load_chart( url , clazz ){
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
@ -24,7 +24,7 @@
console.log(this.responseText); console.log(this.responseText);
var series = JSON.parse(this.responseText); var series = JSON.parse(this.responseText);
var data = { series: series }; var data = { series: series };
new Chartist.Line(clazz, data , {high: 30 , low: -25}); chart = new Chartist.Line(clazz, data , {high: 30 , low: -25});
} }
} }
xhttp.open("GET", url, true); xhttp.open("GET", url, true);

View File

@ -28,6 +28,9 @@ 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];
@ -38,6 +41,9 @@ 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 month_data();
}; };
extern Collector collector; extern Collector collector;

View File

@ -19,7 +19,6 @@ lib_deps =
ESP8266WiFi ESP8266WiFi
ottowinter/ESPAsyncWebServer-esphome @ 1.2.7 ottowinter/ESPAsyncWebServer-esphome @ 1.2.7
adafruit/DHT sensor library adafruit/DHT sensor library
bblanchon/ArduinoJson @ ^6.18.0
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 115200 upload_speed = 115200

View File

@ -97,3 +97,43 @@ 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) ;
}
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) ;
}
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

@ -2,9 +2,6 @@
#include "collector.hpp" #include "collector.hpp"
#include "serial.hpp" #include "serial.hpp"
#include "AsyncJson.h"
#include "ArduinoJson.h"
#include <FS.h> #include <FS.h>
#define DEBUG_OUT Serial #define DEBUG_OUT Serial
@ -24,29 +21,35 @@ String getContentType(String filename){
} }
AsyncCallbackJsonWebHandler* weekly = new AsyncCallbackJsonWebHandler("/weekly", [](AsyncWebServerRequest *request, JsonVariant &json) {
const JsonArray& root_array = json.as<JsonArray>(); // AsyncCallbackJsonWebHandler* monthly = new AsyncCallbackJsonWebHandler("/monthly");
JsonArray nested = root_array.createNestedArray(); // monthly.onRequest( [](AsyncWebServerRequest *request, JsonVariant &json) {
copyArray(collector.week_in , WEEK , nested); // const JsonArray& root_array = json.as<JsonArray>();
nested = root_array.createNestedArray(); // JsonArray nested = root_array.createNestedArray();
copyArray(collector.week_out , WEEK , nested); // copyArray(collector.month_in , MONTH , nested);
}); // nested = root_array.createNestedArray();
AsyncCallbackJsonWebHandler* monthly = new AsyncCallbackJsonWebHandler("/monthly", [](AsyncWebServerRequest *request, JsonVariant &json) { // copyArray(collector.month_out , MONTH , nested);
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", "Not found"); request->send(404, "text/plain;charset=utf-8", "Not found");
} }
void server_setup(){ void server_setup(){
LittleFS.begin(); LittleFS.begin();
server.addHandler(weekly);
server.addHandler(monthly); 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();
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
DEBUG_OUT.println("Index.html"); DEBUG_OUT.println("Index.html");