56 const uint64_t offset = (
static_cast<uint64_t
>(random) * 2 * jitter) / UINT32_MAX;
57 return static_cast<uint32_t
>(interval_ms - jitter + offset);
65 void set_interval(
const std::string &device_id, uint32_t interval_ms, uint32_t now, uint32_t random);
67 [[nodiscard]] uint32_t
get_interval(
const std::string &device_id)
const;
71 [[nodiscard]]
bool has_due(uint32_t now)
const;
77 [[nodiscard]] std::optional<std::string>
pop_due_device(uint32_t now, uint32_t random);
80 void defer(
const std::string &device_id, uint32_t delay_ms, uint32_t now);
92 [[nodiscard]] uint8_t
get_failures(
const std::string &device_id)
const;
94 [[nodiscard]] uint32_t
get_next_poll(
const std::string &device_id)
const;
99 uint32_t interval_ms{0};
100 uint32_t next_poll{0};
102 bool error_reported{
false};
105 std::map<std::string, Entry> entries_;
Per-device periodic limitation-read schedule and miss counter.
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).
static constexpr uint8_t RAIN_POLL_MAX_TRIES
Tries per poll.
uint32_t rain_poll_first_delay_ms(uint32_t random)
Delay of a device's first poll after its interval is set.
static constexpr uint8_t RAIN_POLL_FAILURES_BEFORE_UNKNOWN
Consecutive unanswered polls after which the last reading is no longer usable.
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.
static constexpr uint32_t RAIN_POLL_INITIAL_SPREAD_MS
Width of the random window added to RAIN_POLL_INITIAL_DELAY_MS, so devices do not start in step.
static constexpr uint32_t RAIN_POLL_INITIAL_DELAY_MS
Earliest first poll after boot.
static constexpr uint32_t RAIN_POLL_JITTER_PERCENT
Per-poll offset, as a percentage of the interval, applied symmetrically around it.
static constexpr uint32_t RAIN_POLL_MOVING_RETRY_MS
Delay before asking again when the device is moving.