Přeskočit obsah

Pattern 20: Scenario List

When to Use

Your integration needs to know which correlation scenarios can notify it — for example, to offer them in the external system's own configuration or to show a scenario name in its log. The framework keeps the list current for you.

Declare the capability in adapter-registration.yaml:

capabilities:
  scenarios: true

Without it, IScenarioCatalog is not registered and resolving it from DI fails.


Which Scenarios You See

A scenario is on the list when it has a Send command action with the scenario notify command aimed at one of your adapter's devices.

Scenario On the list?
A notify action targets one of your devices Yes
The scenario is disabled Yes, with Enabled = false
A notify action targets several of your devices Yes, once; DeviceIds holds all of them
The action sends a command other than notify No
The notify action targets another adapter's device No; other adapters' device IDs are never exposed
The notify action uses source device as its target No; the target is known only when the scenario fires, but the notify command still arrives
The scenario does not compile No; the server logs a warning

The list is ordered by scenario name.


IScenarioCatalog

Pq.Adapters.Framework.Capabilities.Scenarios.IScenarioCatalog is available through DI once the capability is declared. There is one instance per adapter process, shared by all devices.

Member Description
Task<IReadOnlyList<ScenarioInfo>> GetScenarios(Guid deviceId, CancellationToken) Scenarios that notify this device, disabled ones included
Task<IReadOnlyList<ScenarioInfo>> GetScenarios(CancellationToken) Scenarios that notify any of your devices, disabled ones included
event EventHandler? Changed Raised after the list was reloaded

ScenarioInfo is a record: Id (Guid), Name (string), Enabled (bool), DeviceIds (Guid[], your devices the scenario notifies), Tags (string[]). Disabled scenarios are listed too; filter on Enabled yourself if you only want active ones.

Tags are the free-form labels operators put on a scenario in PQ. They are stored lowercase and sorted, and the array is empty when the scenario has none. Use them to pick out the scenarios that concern your integration, for example only those an operator tagged for it:

var mine = (await catalog.GetScenarios(deviceId, ct)).Where(s => s.Enabled && s.Tags.Contains("milestone"));

Adding or removing a tag is a change to the scenario, so it reloads the list and raises Changed like a rename does; a scenario that loses the tag you filter on is simply absent from the next filtered list. ScenarioInfo is a record, but == compares DeviceIds and Tags by reference: compare them by content.

Usually you want the per-device overload: each device that represents an external system sends that system only its own scenarios.

Lifecycle

  • Lazy. Nothing happens until the first GetScenarios call. That call subscribes to scenario changes and loads the list from the PQ server.
  • Kept current. Whenever a scenario is created, renamed or otherwise changed, deleted, enabled or disabled, the framework reloads the whole list and raises Changed. Call GetScenarios again to read it. This also covers a notify action added to or removed from a scenario: the scenario appears on or disappears from the list.
  • Failure. If the first load fails, GetScenarios throws; the next call retries. If a reload fails, the previous list is kept and the next scenario change retries.
  • Limitation. A change made while the adapter is disconnected from the message bus is picked up with the next scenario change, not on reconnect.

Example

Push the device's list to its external system after connecting and after every change. Subscribe from a long-lived service (not from a Thing), and unsubscribe when it stops — the catalog lives for the whole process and would otherwise keep your handler alive. Keep only the device ID, not the Thing.

public sealed class ScenarioPublisher(IScenarioCatalog catalog, Protocol protocol, Guid deviceId) : IDisposable
{
    public async Task Start(CancellationToken ct)
    {
        catalog.Changed += OnChanged;
        await Push(ct);
    }

    public void Dispose() => catalog.Changed -= OnChanged;

    private async void OnChanged(object? sender, EventArgs e)
    {
        try
        {
            await Push(CancellationToken.None);
        }
        catch (Exception ex)
        {
            // log: the next change pushes the full list again
        }
    }

    private async Task Push(CancellationToken ct)
    {
        var scenarios = await catalog.GetScenarios(deviceId, ct);
        await protocol.SendScenarioList(scenarios.Where(s => s.Enabled), ct);
    }
}

Protocol.SendScenarioList stands for your own protocol call. Always send the full list: it is small, and a full list makes a missed or repeated change harmless.


Scenario Notify Command

To let PQ tell your external system that a scenario fired, support the framework command pq.command.scenario.notify. List it on the device type that represents the external system:

device_types:
  partner_system:
    commands:
      - pq.command.scenario.notify

In the scenario editor, the operator adds a Send command action, picks that device and the Notify Scenario command. There is nothing else to fill in: the server sets every parameter from the scenario that fired.

Parameter Type Content
ScenarioName string Scenario name as it is when the scenario fires
ScenarioId Guid? Scenario identifier
Persons EntityRef[]? Every distinct person named by the events that fired the scenario
Devices EntityRef[]? Every distinct device named by the events that fired the scenario

EntityRef (Pq.Domain.Api.Adapter) is a record: Id (Guid), Name (string). For a scheduled or manual run there are no firing events, so both lists are empty.

The command does not need capabilities: scenarios — that flag is only for the scenario list. Write the handler like any other:

public async Task<DeviceCommandResult> Notify(
    PartnerSystem system,
    Scenario.Notify command,
    Protocol protocol,
    CancellationToken ct)
{
    await protocol.SendScenarioFired(command.ScenarioName, command.Persons ?? [], command.Devices ?? [], ct);
    return DeviceCommandResult.Succeeded();
}

Protocol.SendScenarioFired stands for your own protocol call.