diff --git a/include/WebApi_database.h b/include/WebApi_database.h index 27b7cbd8..4d32271f 100644 --- a/include/WebApi_database.h +++ b/include/WebApi_database.h @@ -12,7 +12,7 @@ public: void loop(); bool write(float energy); - struct Data { + struct pvData { uint8_t tm_year; uint8_t tm_mon; uint8_t tm_mday; @@ -22,6 +22,8 @@ public: private: void onDatabase(AsyncWebServerRequest* request); - + static size_t readchunk(uint8_t* buffer, size_t maxLen, size_t index); + static size_t readchunk1(uint8_t* buffer, size_t maxLen, size_t index); + AsyncWebServer* _server; }; diff --git a/src/WebApi_database.cpp b/src/WebApi_database.cpp index 6994710b..0b872c2d 100644 --- a/src/WebApi_database.cpp +++ b/src/WebApi_database.cpp @@ -48,7 +48,7 @@ bool WebApiDatabaseClass::write(float energy) return (false); // MessageOutput.println("Energy difference > 0"); - struct Data d; + struct pvData d; d.tm_hour = old_hour; old_hour = timeinfo.tm_hour; d.tm_year = timeinfo.tm_year - 100; // year counting from 2000 @@ -70,63 +70,73 @@ bool WebApiDatabaseClass::write(float energy) MessageOutput.println("Failed to append to database."); return (false); } - f.write((const uint8_t*)&d, sizeof(Data)); + f.write((const uint8_t*)&d, sizeof(pvData)); f.close(); // MessageOutput.println("Write data point."); return (true); } +// read chunk from database +size_t WebApiDatabaseClass::readchunk(uint8_t* buffer, size_t maxLen, size_t index) +{ + static bool first = true; + static bool last = false; + static File f; + uint8_t* pr = buffer; + uint8_t* pre = pr + maxLen - 50; + size_t r; + struct pvData d; + + if (first) { + f = LittleFS.open(DATABASE_FILENAME, "r", false); + if (!f) { + return (0); + } + *pr++ = '['; + } + while (true) { + r = f.read((uint8_t*)&d, sizeof(pvData)); // read from database + if (r <= 0) { + if (last) { + f.close(); + first = true; + last = false; + return (0); // end transmission + } + last = true; + *pr++ = ']'; + return (pr - buffer); // last chunk + } + if (first) { + first = false; + } else { + *pr++ = ','; + } + int len = sprintf((char*)pr, "[%d,%d,%d,%d,%f]", + d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.energy); + if (len >= 0) { + pr += len; + } + if (pr >= pre) + return (pr - buffer); // buffer full, return number of chars + } +} + +size_t WebApiDatabaseClass::readchunk1(uint8_t* buffer, size_t maxLen, size_t index) +{ + size_t x = readchunk(buffer, maxLen, index); + MessageOutput.println("----------"); + MessageOutput.println(maxLen); + MessageOutput.println(x); + return(x); +} + void WebApiDatabaseClass::onDatabase(AsyncWebServerRequest* request) { if (!WebApi.checkCredentialsReadonly(request)) { return; } - - AsyncWebServerResponse* response = request->beginChunkedResponse("application/json", - [](uint8_t* buffer, size_t maxLen, size_t index) -> size_t { - static bool first = true; - static bool last = false; - static File f; - uint8_t* pr = buffer; - uint8_t* pre = pr + maxLen - 30; - size_t r; - struct Data d; - - if (first) { - f = LittleFS.open(DATABASE_FILENAME, "r", false); - if (!f) { - return (0); - } - *pr++ = '['; - } - while(true) { - r = f.read((uint8_t*)&d, sizeof(Data)); // read from database - if (r <= 0) { - if (last) { - f.close(); - first = true; - last = false; - return (0); // end transmission - } - last = true; - *pr++ = ']'; - return (pr - buffer); // last chunk - } - if (first) { - first = false; - } else { - *pr++ = ','; - } - int len = sprintf((char*)pr, "[%d,%d,%d,%d,%f]", - d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.energy); - if (len >= 0) { - pr += len; - } - if (pr >= pre) - return (pr - buffer); // buffer full, return number of chars - } - }); - + AsyncWebServerResponse* response = request->beginChunkedResponse("application/json", readchunk); request->send(response); } diff --git a/webapp/src/components/BarChart.vue b/webapp/src/components/BarChart.vue index 230f6de0..dcc0e28d 100644 --- a/webapp/src/components/BarChart.vue +++ b/webapp/src/components/BarChart.vue @@ -1,11 +1,9 @@ @@ -20,9 +18,9 @@ export const type_col = 'ColumnChart'; export const options_col = { height: 300, chartArea: { - top: 25, - width: '85%', - height: '80%' + top: 25, + width: '85%', + height: '80%' }, bar: { groupWidth: '100%' diff --git a/webapp/src/components/CalendarChart.vue b/webapp/src/components/CalendarChart.vue index 794602ea..c7e21344 100644 --- a/webapp/src/components/CalendarChart.vue +++ b/webapp/src/components/CalendarChart.vue @@ -1,12 +1,8 @@ diff --git a/webapp/src/views/HomeView.vue b/webapp/src/views/HomeView.vue index efa45bcf..4b5693c5 100644 --- a/webapp/src/views/HomeView.vue +++ b/webapp/src/views/HomeView.vue @@ -16,7 +16,7 @@ - +