fixed wrong time for first database entry on a day

This commit is contained in:
Ralf Bauer 2023-06-26 16:30:29 +02:00
parent e6cf6b00d3
commit d2a15f86a9
2 changed files with 41 additions and 24 deletions

View File

@ -5,6 +5,7 @@
#include "MessageOutput.h" #include "MessageOutput.h"
#include "WebApi.h" #include "WebApi.h"
#include "defaults.h" #include "defaults.h"
#include <Arduino.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <LittleFS.h> #include <LittleFS.h>
@ -51,7 +52,7 @@ bool WebApiDatabaseClass::write(float energy)
// MessageOutput.println("Energy difference > 0"); // MessageOutput.println("Energy difference > 0");
struct pvData d; struct pvData d;
d.tm_hour = old_hour; d.tm_hour = timeinfo.tm_hour - 1;
old_hour = timeinfo.tm_hour; old_hour = timeinfo.tm_hour;
d.tm_year = timeinfo.tm_year - 100; // year counting from 2000 d.tm_year = timeinfo.tm_year - 100; // year counting from 2000
d.tm_mon = timeinfo.tm_mon + 1; d.tm_mon = timeinfo.tm_mon + 1;
@ -136,37 +137,47 @@ size_t WebApiDatabaseClass::readchunk_log(uint8_t* buffer, size_t maxLen, size_t
// read chunk from database for the last 25 hours // read chunk from database for the last 25 hours
size_t WebApiDatabaseClass::readchunkHour(uint8_t* buffer, size_t maxLen, size_t index) size_t WebApiDatabaseClass::readchunkHour(uint8_t* buffer, size_t maxLen, size_t index)
{ {
time_t now; // MessageOutput.println("====================");
time(&now); // MessageOutput.println(startday.tm_year - 100);
time_t sd = now - (60 * 60 * 25); // subtract 25h // MessageOutput.println(startday.tm_mon + 1);
struct tm startday; // MessageOutput.println(startday.tm_mday);
localtime_r(&sd, &startday); // MessageOutput.println(startday.tm_hour);
if (startday.tm_year <= (2016 - 1900)) { // MessageOutput.println("====================");
return (false); // time not set
}
static bool first = true; static bool first = true;
static bool last = false; static bool last = false;
static bool valid = false; static bool valid = false;
static float oldenergy = 0.0; static float oldenergy = 0.0;
static struct tm startday;
static File f; static File f;
static bool fileopen = false;
uint8_t* pr = buffer; uint8_t* pr = buffer;
uint8_t* pre = pr + maxLen - 50; uint8_t* pre = pr + maxLen - 50;
size_t r; size_t r;
struct pvData d; struct pvData d;
if (first) { if (!fileopen) {
time_t now;
time(&now);
time_t sd = now - (60 * 60 * 25); // subtract 25h
localtime_r(&sd, &startday);
if (startday.tm_year <= (2016 - 1900)) {
return (false); // time not set
}
f = LittleFS.open(DATABASE_FILENAME, "r", false); f = LittleFS.open(DATABASE_FILENAME, "r", false);
if (!f) { if (!f) {
return (0); return (0);
} }
fileopen = true;
*pr++ = '['; *pr++ = '[';
} }
while (true) { while (true) {
r = f.read((uint8_t*)&d, sizeof(pvData)); // read from database r = f.read((uint8_t*)&d, sizeof(pvData)); // read from database
if (r <= 0) { if (r <= 0) {
if (last) { if (last) {
// MessageOutput.println("Close file");
f.close(); f.close();
fileopen = false;
first = true; first = true;
last = false; last = false;
valid = false; valid = false;
@ -176,13 +187,24 @@ size_t WebApiDatabaseClass::readchunkHour(uint8_t* buffer, size_t maxLen, size_t
*pr++ = ']'; *pr++ = ']';
return (pr - buffer); // last chunk return (pr - buffer); // last chunk
} }
if (!valid) {
if ((d.tm_year >= startday.tm_year - 100)
&& (d.tm_mon >= startday.tm_mon + 1)
&& (d.tm_mday >= startday.tm_mday)
&& (d.tm_hour >= startday.tm_hour)) {
valid = true;
MessageOutput.println(d.tm_year);
MessageOutput.println(d.tm_mon);
MessageOutput.println(d.tm_mday);
MessageOutput.println(d.tm_hour);
} else
oldenergy = d.energy;
}
if (valid) { if (valid) {
if (first) { if (first) {
first = false; first = false;
oldenergy = d.energy; } else
} else {
*pr++ = ','; *pr++ = ',';
}
int len = sprintf((char*)pr, "[%d,%d,%d,%d,%f]", int len = sprintf((char*)pr, "[%d,%d,%d,%d,%f]",
d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour,
(d.energy - oldenergy) * 1e3); (d.energy - oldenergy) * 1e3);
@ -190,15 +212,10 @@ size_t WebApiDatabaseClass::readchunkHour(uint8_t* buffer, size_t maxLen, size_t
if (len >= 0) { if (len >= 0) {
pr += len; pr += len;
} }
if (pr >= pre) if (pr >= pre) {
// MessageOutput.println("send buffer");
return (pr - buffer); // buffer full, return number of chars return (pr - buffer); // buffer full, return number of chars
} else { }
if ((d.tm_year >= startday.tm_year - 100)
&& (d.tm_mon >= startday.tm_mon + 1)
&& (d.tm_mday >= startday.tm_mday)
&& (d.tm_hour >= startday.tm_hour)
)
valid = true;
} }
} }
} }

Binary file not shown.