15bool reached(uint32_t now, uint32_t deadline) {
return static_cast<int32_t
>(now - deadline) >= 0; }
20 if (interval_ms == 0) {
21 entries_.erase(device_id);
24 Entry &e = entries_[device_id];
25 e.interval_ms = interval_ms;
31 const auto it = entries_.find(device_id);
32 return it != entries_.end() ? it->second.interval_ms : 0;
36 return std::any_of(entries_.begin(), entries_.end(),
37 [now](
const auto &entry) { return reached(now, entry.second.next_poll); });
41 for (
auto &[device_id, e] : entries_) {
42 if (!reached(now, e.next_poll))
51 const auto it = entries_.find(device_id);
52 if (it == entries_.end())
54 const uint32_t retry_at = now + delay_ms;
55 if (reached(it->second.next_poll, retry_at))
56 it->second.next_poll = retry_at;
60 const auto it = entries_.find(device_id);
61 if (it == entries_.end())
63 if (it->second.failures < UINT8_MAX)
64 it->second.failures++;
65 return it->second.failures >= RAIN_POLL_FAILURES_BEFORE_UNKNOWN;
69 const auto it = entries_.find(device_id);
70 if (it != entries_.end())
71 it->second.failures = 0;
75 const auto it = entries_.find(device_id);
76 if (it == entries_.end() || it->second.error_reported)
78 it->second.error_reported =
true;
83 const auto it = entries_.find(device_id);
84 return it != entries_.end() ? it->second.failures : 0;
88 const auto it = entries_.find(device_id);
89 return it != entries_.end() ? it->second.next_poll : 0;
uint8_t get_failures(const std::string &device_id) const
Consecutive unanswered polls (0 for an unscheduled device).
bool has_due(uint32_t now) const
True when any device's poll is due at now.
void set_interval(const std::string &device_id, uint32_t interval_ms, uint32_t now, uint32_t random)
Set the poll interval and schedule the first poll at rain_poll_first_delay_ms(random) after now.
std::optional< std::string > pop_due_device(uint32_t now, uint32_t random)
Return one device whose poll is due and immediately schedule its next poll at rain_poll_jittered_inte...
uint32_t get_interval(const std::string &device_id) const
Return the configured interval (0 when the device is not scheduled).
void defer(const std::string &device_id, uint32_t delay_ms, uint32_t now)
Pull the device's next poll forward to delay_ms after now. Never pushes it later.
bool first_error_reply(const std::string &device_id)
True the first time it is called for a device that answered the limitation read with an error reply,...
void on_poll_succeeded(const std::string &device_id)
Clear the unanswered-poll count.
bool on_poll_failed(const std::string &device_id)
Count one unanswered poll (saturating).
uint32_t get_next_poll(const std::string &device_id) const
Absolute millis() time of the device's next poll (0 for an unscheduled device).
uint32_t rain_poll_first_delay_ms(uint32_t random)
Delay of a device's first poll after its interval is set.
uint32_t rain_poll_jittered_interval_ms(uint32_t interval_ms, uint32_t random)
One poll interval moved by a uniform offset of up to RAIN_POLL_JITTER_PERCENT either way.
Schedule and failure bookkeeping for the opt-in rain sensor poll.