# `ExCEP.Gateway.SubscriptionManager`

Data Subscription rule engine per CSG CEP TS2.2 Section 8.7 & 8.8.

Implements the exact precedence rules:
1. Model-level subscription (`AllDev == "1"`): applies to all devices of that model
   (both existing and newly onboarded), overwriting prior subscriptions for that `{model, service_id}`.
2. Device-level subscription (`AllDev == "0"`): applies only to the listed `DeviceList`,
   overwriting prior subscriptions for each `{model, service_id, device_id}`.

# `rule`

```elixir
@type rule() :: %{
  model: String.t(),
  service_id: String.t(),
  spont?: boolean(),
  period_sec: non_neg_integer(),
  all_data?: boolean(),
  data_list: [String.t()]
}
```

# `t`

```elixir
@type t() :: %ExCEP.Gateway.SubscriptionManager{
  device_rules: %{required({String.t(), String.t(), String.t()}) =&gt; rule()},
  model_rules: %{required({String.t(), String.t()}) =&gt; rule()}
}
```

# `apply_subscriptions`

```elixir
@spec apply_subscriptions(t(), term()) :: {t(), [map()]}
```

Applies a `DataSubs` `Subs` list and returns `{updated_manager, resp_subs_list}`.

# `new`

```elixir
@spec new() :: t()
```

# `query_subscriptions`

```elixir
@spec query_subscriptions(t(), [map()]) :: [map()]
```

Handles `GetDataSubs` queries and returns the `Subs` array for `GetDataSubsResp`.

# `resolve`

```elixir
@spec resolve(t(), String.t(), String.t(), String.t()) :: rule() | nil
```

Resolves the effective subscription rule for `{model, service_id, device_id}`.

# `should_report_spont?`

```elixir
@spec should_report_spont?(t(), String.t(), String.t(), String.t(), String.t()) ::
  boolean()
```

Returns `true` if a spontaneous change for `data_refer` should be uploaded.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
