Add air quality history charts
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/web_server_base/web_server_base.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace esphome::air_quality_web {
|
||||
@@ -34,6 +36,23 @@ class AirQualityWeb : public PollingComponent {
|
||||
protected:
|
||||
static esp_err_t handle_request_(httpd_req_t *request);
|
||||
std::string snapshot_();
|
||||
esp_err_t send_history_(httpd_req_t *request);
|
||||
|
||||
static constexpr uint32_t HISTORY_DURATION_MS = 36000000;
|
||||
static constexpr uint32_t HISTORY_INTERVAL_MS = 60000;
|
||||
static constexpr size_t HISTORY_CAPACITY = 601;
|
||||
struct HistorySample {
|
||||
uint32_t timestamp;
|
||||
float co2;
|
||||
float temperature;
|
||||
float humidity;
|
||||
};
|
||||
static_assert(sizeof(HistorySample) == 16, "History samples must remain compact");
|
||||
struct HistorySnapshot {
|
||||
uint32_t timestamp;
|
||||
size_t count;
|
||||
HistorySample samples[HISTORY_CAPACITY];
|
||||
};
|
||||
|
||||
AirQualityServer server_;
|
||||
std::string mdns_hostname_;
|
||||
@@ -41,6 +60,11 @@ class AirQualityWeb : public PollingComponent {
|
||||
sensor::Sensor *temperature_{nullptr};
|
||||
sensor::Sensor *humidity_{nullptr};
|
||||
Mutex snapshot_mutex_;
|
||||
HistorySample history_[HISTORY_CAPACITY]{};
|
||||
size_t history_head_{0};
|
||||
size_t history_count_{0};
|
||||
uint32_t last_sample_ms_{0};
|
||||
bool has_sample_{false};
|
||||
std::string json_{"{\"co2\":null,\"temperature\":null,\"humidity\":null}"};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user