Files
2026-10-10 16:03:44 +02:00

78 lines
3.1 KiB
C++

#pragma once
#include <ctime>
#include <cstdint>
#include <initializer_list>
// Calendar policy only: no Wi-Fi, Arduino or hardware dependency. TZ is Amsterdam.
struct BoomScheduleConfig {
uint16_t weekdayStart = 12 * 60, weekdayEnd = 60;
uint16_t weekendStart = 12 * 60, weekendEnd = 2 * 60;
uint16_t overrideMinutes = 30;
bool enabled = false;
bool modemSleep = true;
bool valid() const {
return weekdayStart < 1440 && weekdayEnd < 1440 && weekendStart < 1440 &&
weekendEnd < 1440 && overrideMinutes >= 1 && overrideMinutes <= 1440;
}
};
enum class BoomOperatingState { MONITORING, SCHEDULED_IDLE, SCHEDULED_SLEEP, MANUAL_OVERRIDE };
namespace BoomSchedule {
// Repeated autumn hour: earliest start, latest end. Missing spring hour:
// libc normalises forward. Each interval remains anchored to its starting day.
inline time_t boundary(tm day, int minute, bool end) {
day.tm_hour = minute / 60; day.tm_min = minute % 60; day.tm_sec = 0;
time_t chosen = -1;
for (int dst = 0; dst <= 1; ++dst) {
tm candidate = day; candidate.tm_isdst = dst;
time_t value = mktime(&candidate);
tm roundtrip{}; localtime_r(&value, &roundtrip);
if (roundtrip.tm_year == day.tm_year && roundtrip.tm_yday == day.tm_yday &&
roundtrip.tm_hour == day.tm_hour && roundtrip.tm_min == day.tm_min) {
if (chosen == -1 || (end ? value > chosen : value < chosen)) chosen = value;
}
}
if (chosen != -1) return chosen;
day.tm_isdst = 0; // Amsterdam spring gap: move forward by the skipped hour
return mktime(&day);
}
inline tm dayAt(time_t now, int offset) {
tm day{}; localtime_r(&now, &day);
day.tm_mday += offset; day.tm_hour = 12; day.tm_min = day.tm_sec = 0; day.tm_isdst = -1;
mktime(&day);
return day;
}
inline void interval(const BoomScheduleConfig& c, time_t now, int offset, time_t& start, time_t& end) {
tm day = dayAt(now, offset);
bool weekend = day.tm_wday == 0 || day.tm_wday == 6;
int first = weekend ? c.weekendStart : c.weekdayStart;
int last = weekend ? c.weekendEnd : c.weekdayEnd;
start = boundary(day, first, false);
if (last <= first) day = dayAt(now, offset + 1); // equal means a full calendar day
end = boundary(day, last, true);
}
inline bool active(const BoomScheduleConfig& c, time_t now) {
if (!c.enabled || !c.valid()) return true;
for (int offset = -1; offset <= 0; ++offset) {
time_t start, end; interval(c, now, offset, start, end);
if (now >= start && now < end) return true;
}
return false;
}
inline time_t nextTransition(const BoomScheduleConfig& c, time_t now) {
if (!c.enabled || !c.valid()) return 0;
time_t next = 0;
for (int offset = -1; offset <= 8; ++offset) {
time_t start, end; interval(c, now, offset, start, end);
for (time_t candidate : {start, end}) {
if (candidate > now && (!next || candidate < next) &&
active(c, candidate - 1) != active(c, candidate)) next = candidate;
}
}
return next;
}
inline bool shouldMonitor(const BoomScheduleConfig& c, time_t now, bool trusted, bool overrideActive) {
return overrideActive || !trusted || active(c, now);
}
}