Skip to content

[scanner] Extract motion event state machine from MotionScanner.scan() - #292

Draft
Breakthrough wants to merge 1 commit into
issue-285from
refactor/event-state-machine
Draft

Breakthrough wants to merge 1 commit into
issue-285from
refactor/event-state-machine

Conversation

@Breakthrough

Copy link
Copy Markdown
Owner

Followup refactor stacked on #287 (retarget to main once #287 merges).

Extract the motion event detection state machine out of MotionScanner.scan() into a pure-Python MotionEventTracker class (plus CompletedEvent and EventStep) inside dvr_scan/scanner.py — strictly behavior-preserving, with no new submodules and no changes to video_input.py, wallclock.py, or _offset_lock (lock cleanup deferred to a separate followup PR).

Summary of Changes

  • dvr_scan/scanner.py:
    • Added MotionEventTracker, CompletedEvent, and EventStep with zero threading, OpenCV, or video I/O dependencies so event-detection logic can be unit-tested directly with synthetic (timecode, score, frame) sequences.
    • Centralizes min_event_len, pre/post/merge-tail frame buffering, single _padded_end() calculation (replacing all 3 duplicated formulas), decoupled merge_window / time_post_event handling, max_event_len splitting (_split_event_if_needed), max_events early stop, per-event high_score / high_frame tracking & reset, explicit buffer release (clear_buffers() in finish(), on max_events, and in scan()'s finally:), and finish() EOF flush.
    • Simplified MotionScanner.scan() to focus on thread lifecycle, detector/filter/overlay execution, progress bar updates, and queue I/O, using a shared _enqueue_frames() helper and guarding handle_completed_event() against asynchronous cancellation.
  • tests/test_event_tracker.py:
    • Added 34 fast synthetic unit tests covering min_event_len, exact threshold boundaries, pre/post padding & clamping, decoupled merge_window, max_event_len splitting, max_events early stop, VFR non-uniform timestamps, frame_skip, highscore tracking/reset, EOF flushing, and buffer release.

Verification

  • ruff format dvr_scan/ tests/ and ruff check dvr_scan/ tests/ pass.
  • python scripts/check_ascii.py passes.
  • python -m pytest -x -vv: 458 passed, 4 skipped.
  • Independent review pass (including 5,000 randomized differential trials against issue-285 / 8d490b9) confirmed zero event-span/ordering regressions; all flagged issues (post-scan frame buffer retention, mid-frame cancellation guard, copy-on-read events, docstring/helper/test polish) were fixed before pushing.

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