Skip to content

StatusPoll

Overview

Periodic device status polling. When a device does not push status changes on its own (no event stream, or an unreliable one), the framework calls the adapter on a fixed interval so it can read the current state of the device and update the relevant Things. The adapter implements the read-and-publish logic; the framework owns the scheduling.

  • Added in: PQ Framework 2.0
  • Namespace: Pq.Adapters.Framework

When to Use

  • Devices with no asynchronous event/notification channel (poll-only protocols)
  • Request/response protocols where state must be queried explicitly
  • Supplementing an event stream with a periodic reconciliation read
  • Detecting silent state changes (e.g., a relay toggled outside the system)

If the device reliably pushes every state change, prefer the event stream and skip this function.

States

None. StatusPoll does not expose status states of its own. The adapter updates the states of other functions (Door, Reader, Input, etc.) with the polled values.

Actions

None. Polling is driven by the framework scheduler on the configured interval; there are no action methods to invoke.

Properties

Property Type Default Range Description
poll_interval_seconds int 30 5–3600 Seconds between status polls

YAML Example

device_types:
  - type_id: AccessController
    name: "Access Control Panel"
    functions:
      StatusPoll:
        poll_interval_seconds: 15
      ConnectionBase:
      Door:
      Input:

Code Usage

The framework invokes a status poller on the configured interval. Write a class that implements IStatusPoller<TThing> — the source generator finds it, adds this function to TThing and registers the poller with the scheduler. Read the device state, then commit the resolved values to the relevant Things in batch updates.

internal sealed class ControllerStatusPoller(Protocol protocol) : IStatusPoller<AccessController>
{
    // invoked by the framework on each poll tick
    public async Task PollStatus(AccessController controller, CancellationToken ct)
    {
        var snapshot = await protocol.ReadStatus(ct);

        foreach (var door in controller.InStartupOrder<ControllerDoor>())
        {
            var batch = door.StatusBatch();
            batch.Set(snapshot.IsSecured(door.Address) ? DoorState.Secured : DoorState.Unsecured);
            await batch.Commit(ct); // one publish for all changes
        }
    }
}

Contract: services come through the constructor; the polled Thing comes as the PollStatus parameter on every call. The scheduler creates one poller per polled Thing when it connects and drops it when it disconnects, so fields may keep state between polls; never store the Thing itself. One poller per Thing type (PQ0005). The deprecated static PollStatus method and a hand-written IPollableStatus still work and report PQ0004.

Notes

  • Framework-Scheduled: You do not start your own timer; the framework calls the handler at poll_interval_seconds
  • Cover All States: Poll every status-bearing function the protocol exposes for each polled element — a partition has armed/alarm/trouble, a door has its own set (see RULE-051 in compliance rules)
  • Batch Updates: Collect all polled values per Thing and commit them together to minimize message traffic
  • Snapshot Semantics: Commit scopes to exactly the functions you called Set(...) on; Set(state) selects its function from the state enum's type. Functions you did not set are left untouched — siblings are never cleared
  • Connection Lifecycle: The scheduler polls only connected Things; no manual online guard is needed
  • Interval Trade-off: Shorter intervals detect changes faster but increase device and network load; choose per device capacity
  • Reconciliation: Even with an event stream, a slow poll can correct missed or dropped events

See Also

  • Functions: HistoryPoll - Periodic event/history retrieval
  • Functions: ConnectionBase - Connection lifecycle
  • Functions: Door - Access control door