Front door¶
Six names. If you are writing your first app, this is the whole
API — App declares it, @app.on_detection is the rule, and
event.alert() is what reaches an operator. Everything else on
this site is for the app that outgrows them.
opennvr_app_sdk.App
¶
App(
app_id: str,
*,
name: str | None = None,
version: str = "0.1.0",
category: str = "analytics",
summary: str = "",
requires_tasks: Sequence[str] | None = None,
consume_tier0: bool = True,
**manifest_kwargs: Any,
)
A whole OpenNVR app: identity, config, rules, surfaces, lifecycle.
Construct one at module scope, decorate handlers on it, and call
:meth:run from __main__. The NATS loop, alert dispatch, the
contract server, registry self-registration, live config, the CLI
and signal handling are inherited from :class:~.detector.Detector,
which this compiles to.
Constructor arguments beyond the ones below are passed to
:class:~.manifest.AppManifest, so anything the catalog understands
(description, use_cases, pricing, requires_scopes,
provides, author …) is available without leaving the facade.
Fields the decorators derive — params, emits,
state_schema, actions, has_ui, entitlement — are
refused here, with a pointer to the decorator that owns them.
Source code in opennvr_app_sdk/facade.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |
config
property
¶
config: Any
The parsed config of the running app. event.config is the
same object and is what a rule should normally use.
store
property
¶
store: dict[str, Any]
A plain dict the app can keep counters and recent items in.
Its contents are merged into GET /state, so declaring
app.metric("alerted") and doing app.store["alerted"] += 1
is a complete dashboard.
nvr
property
¶
nvr
The platform client, built once from this app's own config
and credential (:class:~.client.OpenNVR).
publisher
property
¶
publisher
The domain-event publisher for this app
(:class:~.domain_events.DomainEventPublisher). event.publish
is the usual way in.
param
¶
param(
name: str,
type_: Any = str,
*,
default: Any = None,
description: str = "",
per_camera: bool = False,
required: bool = False,
suggestions: Sequence[str] | None = None,
) -> "App"
Declare one operator-settable knob.
It becomes a manifest param (so the catalog renders a form
field), a field on the generated config dataclass (so
config.yml fills it), and an attribute on event.config.
Bind a rule filter to it with "$name". Chainable.
Source code in opennvr_app_sdk/facade.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
zone
¶
zone(name: str, description: str = '') -> 'App'
Declare a zone the operator draws on each camera.
@app.on_detection(..., zone="driveway") declares it too;
call this to give it a description the operator will see in the
catalog's geometry editor. Chainable.
Source code in opennvr_app_sdk/facade.py
638 639 640 641 642 643 644 645 646 | |
emits
¶
emits(
name: str,
*,
severity: str = "medium",
description: str = "",
) -> "App"
Declare an alert kind for the catalog. Optional — one is derived per rule otherwise. Chainable.
Source code in opennvr_app_sdk/facade.py
648 649 650 651 652 653 654 | |
publishes
¶
publishes(schema: str) -> 'App'
Declare a domain event this app publishes, so it appears in the app's AsyncAPI document. Chainable.
Source code in opennvr_app_sdk/facade.py
656 657 658 659 660 661 | |
on_detection
¶
on_detection(
*labels: str,
camera: str | Sequence[str] | None = None,
zone: str | None = None,
min_confidence: Any = None,
dwell: Any = 0.0,
cooldown: Any = 0.0,
forget: Any = None,
severity: str = "medium",
emits: str | None = None,
) -> Callable[
[Callable[[DetectionEvent], Any]],
Callable[[DetectionEvent], Any],
]
Register a rule. The handler is called once per detection
that passes every filter, with a :class:DetectionEvent.
labels
Class labels to react to ("person", "car"). None
given ⇒ every label.
camera
One camera id or a list; omitted ⇒ every camera.
zone
Only fire inside this zone. Declaring one adds a per-camera
geometry.polygon param of that name, which is how the
operator knows which zones to draw and where each one goes.
min_confidence
Floor on detector confidence. No hidden default: omit it and
every detection reaches the rule.
dwell
Seconds the object must have satisfied THIS rule's filters
continuously before the handler runs, and then only once per
presence episode — the loitering pattern without the state
machine. The clock starts when the filters first match, so a
zone rule times presence in the zone.
cooldown
Minimum seconds between handler calls for the same object.
What stops a parked car alerting on every frame.
forget
Seconds without a sighting that end a presence episode and
re-arm dwell. Defaults to
max(DEFAULT_ABSENCE_S, dwell) — long enough that a few
dropped frames are not mistaken for the object leaving.
severity
Default severity for alerts this rule fires, and the
severity of the alert type derived for the manifest.
emits
Name of the alert type this rule fires. Defaults to the
handler's name, slugified; declare it when the name matters
(it is part of the catalog listing, so a later rename of the
function would otherwise change the app's public contract).
The handler may fire alerts with event.alert(...), return an
:class:~.alerts.Alert (or a list), or return nothing.
Source code in opennvr_app_sdk/facade.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
on_event
¶
on_event() -> Callable[
[Callable[..., Any]], Callable[..., Any]
]
Escape hatch: see every inference event whole.
The handler is called as fn(camera_id, detections, event) —
the triple :meth:Detector.on_detections receives — and may
return alerts to fire. Use it for rules about the frame rather
than about one object (crowding, absence, ratios).
Source code in opennvr_app_sdk/facade.py
759 760 761 762 763 764 765 766 767 768 769 770 771 | |
state
¶
state() -> Callable[
[Callable[[], dict[str, Any]]],
Callable[[], dict[str, Any]],
]
Register what GET /state returns — the live data the
catalog renders through the tiles declared below.
Anything in :attr:store is included automatically, so an app
that only keeps counters needs no @app.state at all.
Source code in opennvr_app_sdk/facade.py
775 776 777 778 779 780 781 782 783 784 785 786 | |
metric
¶
metric(
path: str,
*,
label: str | None = None,
description: str = "",
) -> "App"
A single number from /state, shown as a stat chip.
Source code in opennvr_app_sdk/facade.py
788 789 790 791 | |
gauge
¶
gauge(
path: str,
*,
label: str | None = None,
min: float = 0.0,
max: float = 100.0,
warn: float | None = None,
danger: float | None = None,
unit: str = "",
description: str = "",
) -> "App"
A number between bounds, shown as a bar — amber past warn,
red past danger.
Source code in opennvr_app_sdk/facade.py
793 794 795 796 797 798 799 800 | |
table
¶
table(
path: str,
*,
label: str | None = None,
columns: Sequence[str] = (),
description: str = "",
) -> "App"
A list from /state, shown as a table.
Source code in opennvr_app_sdk/facade.py
802 803 804 805 806 | |
log
¶
log(
path: str,
*,
label: str | None = None,
limit: int = 20,
description: str = "",
) -> "App"
A recent-events feed, newest first.
Source code in opennvr_app_sdk/facade.py
808 809 810 811 | |
gallery
¶
gallery(
path: str,
*,
label: str | None = None,
limit: int = 12,
description: str = "",
) -> "App"
A thumbnail wall — plate crops, doorbell snapshots. Entries
are {image|url, label, time}; image may be a data URI.
Source code in opennvr_app_sdk/facade.py
813 814 815 816 817 | |
action
¶
action(
name: str,
*,
label: str | None = None,
params: Sequence[Param] = (),
description: str = "",
confirm: bool = False,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Register an operator verb — a button in the catalog with a
form generated from params.
The handler is called with the declared params as keyword
arguments, defaults filled in. Raise ValueError for params
you reject (the operator sees the message); return anything
JSON-serializable.
Actions are reached only through core's proxy, which is
user-JWT only: an action is always invoked by a person, and
:func:~.usercontext.current_user tells you which one.
Source code in opennvr_app_sdk/facade.py
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | |
ui
¶
ui() -> Callable[[Callable[[], str]], Callable[[], str]]
Register an HTML dashboard, served at GET /ui and
rendered sandboxed inside the catalog. Return a string.
Source code in opennvr_app_sdk/facade.py
869 870 871 872 873 874 875 876 877 | |
on_license
¶
on_license() -> Callable[
[Callable[[str], Any]], Callable[[str], Any]
]
Register the licence gate for a paid app.
Declaring it sets entitlement="license_key": the catalog
collects a key from the administrator and refuses to enable the
app until this function says it is good. Return an
:class:~.contract.Entitlement, or a bool for the simple case.
OpenNVR takes no part in the transaction.
Source code in opennvr_app_sdk/facade.py
879 880 881 882 883 884 885 886 887 888 889 890 891 892 | |
on_setup
¶
on_setup() -> Callable[
[Callable[[Any], Any]], Callable[[Any], Any]
]
Run once with the parsed config before any event — open a database, build a client, load a denylist.
Source code in opennvr_app_sdk/facade.py
896 897 898 899 900 901 902 903 904 | |
on_config
¶
on_config() -> Callable[
[Callable[[dict], Any]], Callable[[dict], Any]
]
Run when an operator changes the app's config, which core re-delivers without a restart. Called with the new config dict; make it idempotent — the first call usually restates what boot already applied. Zones are re-read for you either way.
Source code in opennvr_app_sdk/facade.py
906 907 908 909 910 911 912 913 914 915 916 | |
on_shutdown
¶
on_shutdown() -> Callable[
[Callable[[], Any]], Callable[[], Any]
]
Run on the way out (SIGINT / SIGTERM), after the event loop stops — close a database, flush a buffer. The SDK's own resources are closed for you.
Source code in opennvr_app_sdk/facade.py
918 919 920 921 922 923 924 925 926 927 | |
manifest
¶
manifest() -> AppManifest
The :class:~.manifest.AppManifest this app compiles to —
declared fields plus everything the decorators imply.
Source code in opennvr_app_sdk/facade.py
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |
config_class
¶
config_class() -> type
The dataclass config.yml is loaded into:
:class:~.config.BaseAppConfig plus one field per zone and per
declared param.
Source code in opennvr_app_sdk/facade.py
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | |
load_config
¶
load_config(path: str) -> Any
Load path into :meth:config_class, defaulting the NATS
subject to the inference broadcast.
Source code in opennvr_app_sdk/facade.py
1049 1050 1051 1052 1053 1054 1055 | |
detector_class
¶
detector_class() -> type[Detector]
Compile the app to a :class:~.detector.Detector subclass.
This is the whole trick: the facade is a code generator with one output. Anything that accepts a Detector — the test helpers, the camera agent's runtime monitors, a custom runner — accepts this.
Source code in opennvr_app_sdk/facade.py
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 | |
build
¶
build(config: Any, dispatcher: AlertDispatcher) -> Detector
Instantiate the compiled detector directly — for tests and for embedding an app in another process.
Source code in opennvr_app_sdk/facade.py
1070 1071 1072 1073 | |
run
¶
run(argv: list[str] | None = None) -> int
Parse the CLI, load the config, and run until signalled. The return value is the process exit code::
if __name__ == "__main__":
raise SystemExit(app.run())
Source code in opennvr_app_sdk/facade.py
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 | |
opennvr_app_sdk.DetectionEvent
¶
DetectionEvent(
*,
detection: dict[str, Any],
camera: str,
raw: dict[str, Any],
config: Any,
zones: dict[str, Zone],
owner: "App",
state: KeyedState | None = None,
record: Any = None,
severity: str = "medium",
)
One detection, in context — what a @app.on_detection handler
is called with.
The object is deliberately flat: what a rule asks about is an
attribute or a one-word method, and the raw envelope stays available
as :attr:raw for anything the facade does not model.
Coordinates are NORMALIZED (0–1 of the frame) throughout, matching
the platform's NormalizedBBox wire shape, so a rule written
against one camera resolution works on all of them.
Source code in opennvr_app_sdk/facade.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
track_id
property
¶
track_id: str | None
Tracker identity for this object, when the adapter emits one.
Present ⇒ dwell and cooldown follow the OBJECT. Absent ⇒ they fall back to the (camera, label) pair, which is coarser but still keeps a parked car from re-alerting on every frame.
zone
property
¶
zone: str | None
Name of the first zone containing the detection, or None
when it is outside every zone.
dwell_s
property
¶
dwell_s: float
Seconds this object has continuously satisfied THIS rule.
The clock starts when the rule's filters first match, so
zone="driveway", dwell=30 means thirty seconds in the
driveway — not thirty seconds on camera followed by one frame
in the driveway. A gap longer than forget= ends the episode.
detections
property
¶
detections: list[dict[str, Any]]
Every detection in the same inference event, this one included — for rules that need company ("a person AND a car").
correlation_id
property
¶
correlation_id: str
The platform's id for this inference. Thread it onto anything you emit and the whole causal chain — inference, audit line, evidence frame, alert — joins up in the timeline.
adapter
property
¶
adapter: str
Which detector produced this — an adapter name, or tier0
for the always-on detector every camera runs.
nvr
property
¶
nvr
The platform client, built from this app's own config and
credential: event.nvr.cameras(), .timeline.search(...),
.state.set(...), .ai.infer(...). See
:class:~.client.OpenNVR.
in_zone
¶
in_zone(name: str | None = None) -> bool
True when the detection's centre falls inside a zone the
operator drew. With no name, true when it falls inside ANY
of this app's zones.
A zone with no polygon drawn yet is False, not an error: zones are operator config and a camera may simply not have one.
Source code in opennvr_app_sdk/facade.py
258 259 260 261 262 263 264 265 266 267 268 | |
count
¶
count(label: str | None = None) -> int
How many objects of label (or of any label) the same
event carried.
Source code in opennvr_app_sdk/facade.py
321 322 323 324 325 326 327 328 329 330 | |
snapshot
¶
snapshot() -> bytes | None
The current frame from this event's camera, as JPEG bytes.
Source code in opennvr_app_sdk/facade.py
355 356 357 | |
alert
¶
alert(
title: str,
description: str = "",
*,
severity: str | None = None,
evidence: dict[str, Any] | None = None,
tags: Iterable[str] | None = None,
camera_id: str | None = None,
) -> Alert
Fire an alert — the thing a human sees.
Everything the platform needs is filled in from the event:
camera, correlation id, label, confidence, track, zone and
dwell. severity defaults to the rule's, so the manifest's
emits block and the alerts actually fired cannot disagree.
The alert is dispatched whether or not you return it; the return value is there so a handler can adjust it first.
Source code in opennvr_app_sdk/facade.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
publish
¶
publish(
schema: str,
payload: dict[str, Any],
*,
camera_id: str | None = None,
) -> bool
Publish a contracted domain event — the thing another APP
sees. The envelope, the producer (app:<id>), the camera and
the correlation id are filled in; schema and payload are
yours (docs/EVENT_CONTRACTS.md).
Source code in opennvr_app_sdk/facade.py
409 410 411 412 413 414 415 416 417 418 | |
publish_typed
¶
publish_typed(
payload: Any, *, camera_id: str | None = None
) -> bool
As :meth:publish, from a typed payload
(PlateRecognized(...)) — the schema comes from the class, so
a missing required field fails here rather than in someone
else's app.
Source code in opennvr_app_sdk/facade.py
420 421 422 423 424 425 426 427 428 | |
remember
¶
remember(**values: Any) -> None
Stash values on this object's presence record — readable on
the next event for the same object via :meth:recall.
Source code in opennvr_app_sdk/facade.py
432 433 434 435 436 | |
recall
¶
recall(name: str, default: Any = None) -> Any
Read back what :meth:remember stored for this object.
Source code in opennvr_app_sdk/facade.py
438 439 440 441 442 | |
opennvr_app_sdk.Alert
dataclass
¶
Alert(
title: str,
description: str,
camera_id: str,
severity: str = "high",
source: AlertSource = AlertSource(),
correlation_id: str | None = None,
evidence: dict[str, Any] = dict(),
tags: list[str] = list(),
alert_type: str | None = None,
images: dict[str, str] = dict(),
alert_id: str = (
lambda: f"alrt_{uuid.uuid4().hex[:12]}"
)(),
fired_at: str = (lambda: _utcnow_iso())(),
)
A single fired alert.
Maps 1:1 to the §11.5 alert wire shape: alert_id, fired_at,
title, description, severity, source, camera_id,
correlation_id, evidence, tags.
Severity levels are operator-visible — low / medium /
high / critical per the design doc.
to_wire
¶
to_wire() -> dict[str, Any]
Serialize to the §11.5 JSON shape.
Source code in opennvr_app_sdk/alerts.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
opennvr_app_sdk.AppManifest
dataclass
¶
AppManifest(
id: str,
name: str,
version: str,
category: str,
summary: str = "",
requires_tasks: list[str] = list(),
requires_adapters: list[str] = list(),
requires_scopes: list[str] = list(),
provides: list[str] = list(),
subscribes: str | None = None,
params: list[Param] = list(),
emits: list[AlertType] = list(),
state_schema: list[StateView] = list(),
overlay: bool = False,
camera_picker: bool = True,
actions: list[Action] = list(),
has_ui: bool = False,
ui_mode: str = "internal",
ui_url: str = "",
description: str = "",
author: str = "",
website: str = "",
license: str = "",
use_cases: list[str] = list(),
contact: str = "",
pricing: str = "free",
price_note: str = "",
entitlement: str = "none",
)
The static identity + schema of one app.
subscribes is the NATS subject pattern for InferenceSubscriber
apps (None for FrameApps that drive inference themselves).
requires_tasks names adapter task types the app depends on,
e.g. ["object_detection"]. requires_adapters (RFC-0002
Phase 3, decision 7) names the specific KAI-C adapters that must be
PROVISIONED with the app — the installer ups them alongside the app
and refcounts them across apps on uninstall. Adapters the standard
stack already ships (yolov8) are reused, not listed.
to_dict
¶
to_dict() -> dict[str, Any]
The GET /manifest payload (and the manifest_json
snapshot the app registry stores).
Source code in opennvr_app_sdk/manifest.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
opennvr_app_sdk.Param
dataclass
¶
Param(
name: str,
type: Any,
default: Any = None,
per_camera: bool = False,
description: str = "",
required: bool = False,
suggestions: list[str] = list(),
label: str = "",
group: str = "",
advanced: bool = False,
choices: list[Any] = list(),
)
One typed, declarative config knob.
per_camera=True marks params the catalog collects per camera
(zones, tripwires) rather than once per app.
opennvr_app_sdk.setting
¶
setting(name: str) -> Setting
Bind a rule filter to a config value: dwell=setting("dwell_s").
dwell="$dwell_s" means the same thing.
Source code in opennvr_app_sdk/facade.py
141 142 143 144 | |