Přeskočit obsah

PQ0004 — Status poll should implement IStatusPoller

The adapter polls a Thing through a static PollStatus method, or implements IPollableStatus on the Thing by hand. Both are deprecated.

// reported — found by name and signature only
public static class StatusPolling
{
    public static Task PollStatus(Panel panel, Protocol protocol, CancellationToken ct) =>
        protocol.RequestStatus(panel, ct);
}

// reported — the Thing polls itself and gets a service locator
public partial class Panel : IPollableStatus
{
    public Task PollStatus(IServiceProvider serviceProvider, CancellationToken ct) => ...;
}

Both still work. The scheduler polls them exactly as before. The warning says the code uses a path that has a better replacement.

Why it is reported

A typo is silent. The generator finds a static PollStatus by its name and its parameters. Name it PolStatus, or put the CancellationToken first, and the generator does not find it. The adapter builds, and the poll never runs.

Services come from a service locator. The deprecated paths resolve their services on every poll. IPollableStatus hands the Thing an IServiceProvider, which hides what the poll depends on.

No state between polls. A static method has nowhere to keep per-panel state. Adapters work around it with static dictionaries keyed by panel or protocol.

How to fix it

Move the method to a class that implements IStatusPoller<TThing>. The services go to the constructor, the Thing and the token stay in PollStatus:

public sealed class StatusPolling(Protocol protocol) : IStatusPoller<Panel>
{
    public Task PollStatus(Panel panel, CancellationToken ct) =>
        protocol.RequestStatus(panel, ct);
}

For a hand-written IPollableStatus: remove the interface from the Thing, keep the work in an internal method of the Thing, and call it from a poller:

internal sealed class PanelStatusPoller : IStatusPoller<Panel>
{
    public Task PollStatus(Panel panel, CancellationToken ct) => panel.UpdateStatus(ct);
}

The generator adds the StatusPoll function to the Thing type for you. A StatusPoll: entry in adapter-registration.yaml is needed only for a non-default interval.

The scheduler creates one poller per polled Thing when the Thing connects, and drops it when the Thing disconnects. Do not store the Thing in the poller: it arrives with every call. See Pattern 12: Status Polling.

Severity

Warning. Adapters in this repository list PQ0004 in WarningsNotAsErrors, so a project with TreatWarningsAsErrors still builds while it waits for its move to IStatusPoller.

If you disagree with a report

Do not suppress it. A wrong report is a bug in the check — report it with the code that triggered it.