Skip to content

feat(onvif): event notes and tags from what the camera detected (zmc 1.39.57) - #292

Open
SteveGilvarry wants to merge 3 commits into
ZoneMinder:masterfrom
SteveGilvarry:feat/onvif-event-tags
Open

SteveGilvarry wants to merge 3 commits into
ZoneMinder:masterfrom
SteveGilvarry:feat/onvif-event-tags

Conversation

@SteveGilvarry

Copy link
Copy Markdown
Member

Stacked on #291, which is stacked on #290. Only e2e1a93 is new here; the diff will show all three until those merge.

This ports ZoneMinder #7712 (fec099942, "ONVIF event notes and tags from what the camera detected") to zm-api's own ONVIF event listener. It also fixes a bug that stopped that listener from recording any events on standard ZoneMinder databases.

Notes and tags

onvif::event_words ports ONVIFEventNote, ONVIFAddTagNames and ONVIFEventTags from src/zm_monitor_onvif.cpp. Its tests are ZoneMinder's own cases from tests/zm_onvif_event_note.cpp, and they all pass.

Camera Note (was the raw topic) Tags
Reolink MyRuleDetector/PeopleDetect person
Tapo TPSmartEventDetector/TPSmartEvent IsPeople (MyTPSmartEventDetectorRule) person
Ajax ObjectDetection/Object ClassTypes=Human Vehicle (AjaxObjectRule) person, vehicle
Plain motion CellMotionDetector/Motion Motion (no object named: the topic's last segment)

How the listener uses them:

  • Notes: every event the listener opens now gets the new note. As in ZoneMinder, filters matching the old raw-topic notes need updating.
  • Tags: only on monitors with onvif_event_tags on. Words are matched case-insensitively against AI_Object_Classes names and aliases, which are seeded by feat(db): mirror ZoneMinder 1.39.42-57 #290's camera_classes.sql and editable through feat(api): ZoneMinder 1.39.42-57 features: cron jobs, scheduled actions, new columns #291.
  • Loading names: names and aliases load once when the listener starts, as zmc does. On a database without Aliases, it falls back to tagging with the topic.
  • When tags are applied: when an event opens, and again when a later "still active" message for the same event names something new (ClassTypes Human, then Human Vehicle). Each tag goes on an event only once.
  • Storage: repo::events_tags::tag_event_by_name finds or creates the tag, with the name cut to 64 characters, and links it once, as Event::AddTag does. It tolerates another writer, such as zmc, having created the tag or link first.

Fix: the listener never saved events on strict-mode MySQL

The listener's event insert left out Events.StateId, which is NOT NULL with no default. On a strict-mode MySQL, which is ZoneMinder's default, every ONVIF alarm event failed with error 1364 ("Field 'StateId' doesn't have a default value"). It now records the active run state, or 0, as zmc and zm-api's events API already do. The insert moves into insert_alarm_event so that a database test exercises it.

Testing

  • cargo fmt --check, cargo clippy --all-targets --all-features -D warnings: clean
  • cargo test --all-features: 1452 passed, including ZoneMinder's note and tag cases plus tracker and Rule unit tests.
  • DB-backed suite on a 1.39.57 test database: 613 passed. The new it_onvif_event_tags takes a Tapo-style alarm through insert_alarm_event, checks the note, matches tags against the seeded aliases, and checks that tags and links are each created once.
  • Not tested end to end over the network. zm-api's ONVIF transport refuses loopback targets as an SSRF guard, so a fake camera inside the test can't be reached. Each layer is tested instead, and I didn't want to add a test bypass to that guard.

🤖 Generated with Claude Code

SteveGilvarry and others added 3 commits November 29, 2026 19:10
Thirteen upstream updates (1.39.42-46, 49, 51-57; the other versions in
that range changed no schema) are mirrored as SeaORM migrations, vendored
into db/legacy for the bridge's raw chain, and the cutover moves to
1.39.57. zm_create.sql.in, controls.sql, AI_Models.sql and the new
camera_classes.sql come from ZoneMinder fc2bb717f; zm_config.sql is
regenerated from the same commit.

Schema: MonitorActions.Schedule and a Schedule trigger (42), CronJobs (43),
Monitors.Go2RTCDisableBackChannel (45), Tags.Name to utf8mb4_bin on MySQL
(51), Zones.ObjectCoords (54), Filters.AutoTag/AutoTagId (55),
Users.Language varchar(16) (56), Monitors.ONVIF_Event_Tags and
AI_Object_Classes.Aliases (57). Data: HeroSpeed controls (44), the stock
Update DiskSpace filter removed (46), preset url and control fixes (49,
53), the Camera dataset and starter aliases (57). 1.39.52's clean-up of
upgraded schemas is mirrored with its guards; on a schema this crate
creates every step is already true.

Postgres needed two things MySQL does not: MonitorPresets.ControlId is a
varchar, so 1.39.53's control-id subqueries are cast there; and the
AI_Datasets sequence is resynced before the Camera insert, because the
COCO seed's explicit id left it behind. New helpers: mysql_column_type,
resync_sequence. The "schema already current" stamp now also requires the
1.39.43/55/57 markers.

The API does not expose the new columns yet; the entities and endpoints
follow separately.

Verified locally: schema-parity (57 tables, seeds), upgrade-parity from
1.34.26, 1.36.35 and 1.38.3, postgres-schema (756 columns); the 13
mirrors re-run on an already-migrated schema change nothing, and run
cleanly with no change on a copy of a database ZoneMinder's own zmupdate.pl
brought to 1.39.57. cargo test: 1434 passed; schema_gate DB tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ns, new columns

Follows the schema mirror (ZoneMinder#290) with what the new columns are for.

Scheduled tasks and scheduled monitor actions (1.39.42/43). ZoneMinder runs
both from a marked block of the web user's crontab, rebuilt from the tables
whenever its web UI saves one; a row saved only to the database never runs.
service::crontab ports CronTab.php, Command.php and the cronLine() methods:
the same schedule and command checks, ZM_ALLOWED_COMMANDS (no list: no
restriction; a list: the program must be on it and nothing may chain), and
a splice that rewrites only ZoneMinder's block and keeps every other line
byte for byte. Its unit tests are ZoneMinder's own PHP cases.

- /api/v3/cron-jobs: CRUD behind Feature::System (View to read, Edit to
  change: a job runs its command as the web user). Create and update answer
  with the job and the crontab outcome; POST /api/v3/cron-jobs/crontab
  rebuilds on demand. A command outside ZM_ALLOWED_COMMANDS is a 403 naming
  what is allowed.
- Monitor actions take trigger_on Schedule with a validated five-field
  schedule (stored empty otherwise, as ZoneMinder does); a change to a
  scheduled action rebuilds the crontab, and so does deleting a monitor that
  scheduled actions involve.
- The rebuild only writes when zm-api runs as ZM_WEB_USER. As another account
  it would write a second block into its own crontab and every item would run
  twice, so it is skipped with a warning and reported as skipped.

New columns through the API: monitors go2rtc_disable_back_channel and
onvif_event_tags; zones object_coords (ZoneMinder's zone-editor pattern, a 400
otherwise); filters auto_tag/auto_tag_id (the tag must exist; counts as an
automatic action for the every-monitor rule); AI object classes aliases
(comma-separated words); user language up to 16. Entities gain the fields and
a CronJobs entity.

openapi.json refreshed: new endpoints and optional fields only.

Tests: 1447 passed (cargo test --all-features), 611 DB-backed passed
(APP_PROFILE=test-db, --include-ignored) on a test database loaded at
1.39.57, including new it_cron_jobs, it_zm_1_39_57_fields and a scheduled
action case in it_monitor_actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1.39.57)

Ports ZoneMinder #7712 (fec099942) to zm-api's own ONVIF listener.

onvif::event_words is ONVIFEventNote, ONVIFAddTagNames and ONVIFEventTags:
- The note names the detection instead of quoting the raw topic: namespace
  prefixes stripped, a leading RuleEngine/ dropped, inactive / empty /
  per-object-id items left out, active items reduced to their name, the
  Source Rule in parentheses ("TPSmartEventDetector/TPSmartEvent IsPeople
  (MyTPSmartEventDetectorRule)").
- On monitors with ONVIF_Event_Tags on, the event is tagged with every object
  class the alarm names, matched case-insensitively against AI_Object_Classes
  names and aliases (an alias wins over a class name), else with the topic's
  last segment. Words come from active item names (IsPeople -> People), item
  values (ClassTypes "Human Vehicle") and the topic's last segment.
Its tests are ZoneMinder's tests/zm_onvif_event_note.cpp cases.

The listener loads the names and aliases once when it starts, as zmc does
(falling back to topic tags on a database without Aliases), tags an event
when it opens and again when a later "still active" message for it names
something new, each tag once per event. repo::events_tags::tag_event_by_name
finds or creates the tag (cut to 64 characters) and links it once, as zmc's
Event::AddTag does, tolerating a writer that got there first.

Fix: the listener's event insert left out Events.StateId (NOT NULL, no
default), so on a strict-mode MySQL, ZoneMinder's default, every ONVIF
alarm event failed to save. It now records the active run state or 0, as
zmc and the events API do. The insert moves to insert_alarm_event so the
DB test exercises it.

spawn_monitor_event_listener takes a ListenedMonitor instead of separate
monitor arguments.

Tests: 1452 passed (cargo test --all-features); 613 DB-backed passed on a
1.39.57 test database, including it_onvif_event_tags. Not driven end to
end over the network: the ONVIF transport refuses loopback targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant