Skip to content

feat(poll_scheduler): add cron-based poll scheduling to all connectors#2080

Open
apachler wants to merge 1 commit intothingsboard:masterfrom
apachler:feature/poll-schedule
Open

feat(poll_scheduler): add cron-based poll scheduling to all connectors#2080
apachler wants to merge 1 commit intothingsboard:masterfrom
apachler:feature/poll-schedule

Conversation

@apachler
Copy link
Copy Markdown
Contributor

Add cron-based pollSchedule support for all connectors

What

Adds an optional pollSchedule parameter that accepts cron expressions to control poll timing. This enables wall-clock-aligned polling (e.g. exactly at :00, :05, :10) and time-dependent schedules (e.g. faster polling during production, slower on weekends).

Also fixes a missing pollPeriod default in the Modbus Slave connector (config['pollPeriod']config.get('pollPeriod', 5000)), which is the only connector that crashes when pollPeriod is omitted.

Configuration

// Simple — clock-aligned every 5 minutes
{ "pollSchedule": "*/5 * * * *" }

// Multiple schedules with labels
{ "pollSchedule": [
    { "cron": "*/1 6-22 * * 1-5", "label": "day-shift" },
    { "cron": "*/15 * * * 0,6",   "label": "weekend" }
  ]
}
// Disabled (default in sample configs)
{ "pollSchedule": null }

pollSchedule takes precedence over pollPeriod when present. A value of null (or omitting the key entirely) keeps the original pollPeriod behavior unchanged.

Note: All sample configs (thingsboard_gateway/config/*.json) now ship with "pollSchedule": null so the option is visible but inactive by default. To enable, replace null with a cron string or an array of schedule objects as shown above.

Changes

  • New module: thingsboard_gateway/tb_utility/poll_scheduler.pyPollScheduler, PollScheduleEntry, compute_next_poll()
  • New dependency: croniter>=1.3.8 (depends only on python-dateutil, already in the tree)
  • Bug fix: connectors/modbus/slave.py — add missing pollPeriod default (5000ms)
  • All polling connectors: Modbus, OPC-UA, BACnet, BLE, SNMP, CAN, FTP, ODBC, Request — minimal change: instantiate PollScheduler from config, use compute_next_poll() instead of monotonic() + poll_period

Breaking changes

None.

Introduce PollScheduler, a new utility that accepts one or more cron
expressions (via croniter) and computes the next monotonic poll time.
The helper function compute_next_poll() provides a single drop-in
replacement for the existing "current_time + poll_period" pattern used
across every connector.

- Add thingsboard_gateway/tb_utility/poll_scheduler.py with
  PollScheduleEntry, PollScheduler, and compute_next_poll()
- Add unit tests in tests/unit/test_poll_scheduler.py
- Integrate PollScheduler into all connectors: bacnet, ble, can, ftp,
  modbus, odbc, opcua, request, snmp
- Add pollSchedule: null placeholder to all connector config examples
- Add croniter>=1.3.8 to requirements.txt and setup.py dependencies

When pollSchedule is null or absent the behaviour is unchanged
(pollPeriod is used as before). When a cron schedule is supplied the
next fire time is derived from the schedule instead.
@samson0v
Copy link
Copy Markdown
Contributor

Hi @apachler, thanks for your contribution!
Can we plan a review of this feature on the release v.3.8.4?

@apachler
Copy link
Copy Markdown
Contributor Author

Hi @samson0v, sure, let me know what you need from me ;)

@samson0v samson0v added this to the 3.8.4 milestone Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants