120 lines
3.6 KiB
C++
120 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
|
|
class EnergyDayHistory {
|
|
public:
|
|
void observe(int channel, float cumulative_kwh, uint32_t day_key, int hour,
|
|
int seconds_into_hour, uint32_t timestamp) {
|
|
if (!valid_channel(channel) || !valid_day(day_key) || hour < 0 || hour >= 24 ||
|
|
seconds_into_hour < 0 || seconds_into_hour >= 3600)
|
|
return;
|
|
|
|
if (day_key != day_) {
|
|
day_ = day_key;
|
|
for (int c = 0; c < 3; ++c) {
|
|
for (int h = 0; h < 24; ++h) {
|
|
values_[c][h] = 0.0f;
|
|
coverage_[c][h] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
Sample &sample = samples_[channel];
|
|
if (!std::isfinite(cumulative_kwh) || cumulative_kwh < 0.0f) {
|
|
invalidate(channel);
|
|
return;
|
|
}
|
|
|
|
const Sample previous = sample;
|
|
sample.valid = true;
|
|
sample.total = cumulative_kwh;
|
|
sample.day = day_key;
|
|
sample.hour = hour;
|
|
sample.seconds = seconds_into_hour;
|
|
sample.timestamp = timestamp;
|
|
|
|
if (!previous.valid || timestamp <= previous.timestamp ||
|
|
cumulative_kwh < previous.total)
|
|
return;
|
|
const uint32_t elapsed = timestamp - previous.timestamp;
|
|
if (elapsed > 300)
|
|
return;
|
|
|
|
const int end_seconds = previous.seconds + static_cast<int>(elapsed);
|
|
const bool crossed_hour = end_seconds >= 3600;
|
|
// Local seconds must agree with elapsed time, even across a DST transition.
|
|
if (seconds_into_hour != end_seconds % 3600 ||
|
|
(!crossed_hour && (previous.day != day_key || previous.hour != hour)))
|
|
return;
|
|
|
|
const float delta = cumulative_kwh - previous.total;
|
|
if (!crossed_hour) {
|
|
add(channel, hour, delta, elapsed);
|
|
return;
|
|
}
|
|
|
|
const uint32_t before = static_cast<uint32_t>(3600 - previous.seconds);
|
|
const float before_delta = delta * (static_cast<float>(before) / elapsed);
|
|
// The previous day's part is deliberately discarded after the daily reset.
|
|
if (previous.day == day_)
|
|
add(channel, previous.hour, before_delta, before);
|
|
if (elapsed > before)
|
|
add(channel, hour, delta - before_delta, elapsed - before);
|
|
}
|
|
|
|
void invalidate(int channel) {
|
|
if (valid_channel(channel))
|
|
samples_[channel].valid = false;
|
|
}
|
|
|
|
uint32_t day() const { return day_; }
|
|
|
|
// Repeated DST hours share a slot, so coverage can exceed 3600 seconds.
|
|
uint32_t coverage_seconds(int channel, int hour) const {
|
|
return valid_channel(channel) && hour >= 0 && hour < 24 ? coverage_[channel][hour] : 0;
|
|
}
|
|
|
|
bool has_data(int channel, int hour) const {
|
|
return coverage_seconds(channel, hour) != 0;
|
|
}
|
|
|
|
float value(int channel, int hour) const {
|
|
return has_data(channel, hour) ? values_[channel][hour] : 0.0f;
|
|
}
|
|
|
|
private:
|
|
struct Sample {
|
|
bool valid = false;
|
|
float total = 0.0f;
|
|
uint32_t day = 0;
|
|
int hour = 0;
|
|
int seconds = 0;
|
|
uint32_t timestamp = 0;
|
|
};
|
|
|
|
static bool valid_channel(int channel) { return channel >= 0 && channel < 3; }
|
|
|
|
static bool valid_day(uint32_t key) {
|
|
const uint32_t year = key / 10000;
|
|
const uint32_t month = key / 100 % 100;
|
|
const uint32_t day = key % 100;
|
|
if (year == 0 || year > 9999 || month == 0 || month > 12 || day == 0)
|
|
return false;
|
|
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
const bool leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
|
|
return day <= static_cast<uint32_t>(days[month - 1] + (month == 2 && leap ? 1 : 0));
|
|
}
|
|
|
|
void add(int channel, int hour, float delta, uint32_t seconds) {
|
|
values_[channel][hour] += delta;
|
|
coverage_[channel][hour] += seconds;
|
|
}
|
|
|
|
uint32_t day_ = 0;
|
|
float values_[3][24] = {};
|
|
uint32_t coverage_[3][24] = {};
|
|
Sample samples_[3] = {};
|
|
};
|