Delete values that are older than 1 hour

This commit is contained in:
Thomas Kolb 2024-08-02 21:40:05 +02:00
parent cc6618e650
commit a83bac914e
4 changed files with 24 additions and 5 deletions

View file

@ -2,11 +2,11 @@
#include <stdint.h>
#include <vector>
#include <deque>
typedef struct {
uint64_t tstart, tend, interval;
std::vector<float> data;
std::deque<float> data;
const char *unit;
const char *format;
} timeseries_t;
@ -14,3 +14,6 @@ typedef struct {
void timeseries_init(timeseries_t *ts, uint64_t tstart, uint64_t interval, const char *unit, const char *format);
void timeseries_append(timeseries_t *ts, float value);
// remove oldest values
void timeseries_prune(timeseries_t *ts, float duration);

View file

@ -65,7 +65,7 @@ void epaper_draw_and_hibernate(epaper_callback cb, bool full_refresh)
// find minimum and maximum values in a vector. NaN is ignored.
template<typename T>
void minmax(const std::vector<T> &vec, T *min, T *max)
void minmax(const std::deque<T> &vec, T *min, T *max)
{
bool first = true;

View file

@ -157,7 +157,7 @@ static void draw_epaper_initial_callback(GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLAS
display->setCursor(0, y);
display->setFont(&FreeSans12pt7b);
display->print("Connected to WLAN: ");
display->print("WLAN verbunden: ");
y += FreeSans12pt7b.yAdvance;
display->setCursor(0, y);
@ -181,7 +181,7 @@ static void draw_epaper_initial_callback(GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLAS
display->setCursor(0, y);
display->setFont(&FreeSans12pt7b);
display->print("Initial time: ");
display->print("Synchronisierte Zeit: ");
y += FreeSans12pt7b.yAdvance;
display->setCursor(0, y);
@ -348,6 +348,8 @@ void loop(void)
if((now - lastEPaperRefresh) >= 60000) {
lastEPaperRefresh = now;
epaper_draw_and_hibernate(draw_epaper_callback, false);
timeseries_prune(&ts_scd30_humidity, 3600); // keep the last hour
}
/*

View file

@ -16,3 +16,17 @@ void timeseries_append(timeseries_t *ts, float value)
ts->data.push_back(value);
ts->tend += ts->interval;
}
void timeseries_prune(timeseries_t *ts, float duration)
{
std::deque<float>::size_type to_keep = duration / ts->interval;
if(to_keep >= ts->data.size()) {
return;
}
std::deque<float>::size_type to_delete = ts->data.size() - to_keep;
ts->data.erase(ts->data.begin(), ts->data.begin() + to_delete);
ts->tstart += to_delete * ts->interval;
}