Skip to content

feat(camera-api): reboot frozen or silent cameras, with a daily budget - #461

Merged
MateoLostanlen merged 18 commits into
developfrom
feat/camera-recovery
Nov 18, 2026
Merged

MateoLostanlen merged 18 commits into
developfrom
feat/camera-recovery

Conversation

@MateoLostanlen

Copy link
Copy Markdown
Member

Part of pyronear/roadmap#16, fixes #445 (API reboot step). Stacked on #458.

  • The stuck detector only caught a PTZ whose poses all look alike, every 30 min. It now checks every camera every 5 min for three problems: no new image, new images with the exact same pixels (frozen camera, each pose on its own image as at Epinay), and the existing turret not moving check.
  • Two checks in a row reboot the camera through its API, at most 3 attempts per 24 h and 30 min apart. Cameras that cannot reboot are only logged. Power cuts stay with the Shelly watchdog.
  • /patrol_status exposes the detected problem and the reboot attempts, for monitoring.
  • ALLOW_STILL_CAMERA=true (testing only, warned at startup) ignores frozen and not moving cameras, to work with a still PTZ at home.

@MateoLostanlen

Copy link
Copy Markdown
Member Author

Tested on pi-test (Pi 5, Debian 13, Docker 29), branch develop + #419 + #459 + #461, images built on the Pi, ALLOW_STILL_CAMERA=true, both real cameras unplugged.

Camera without fresh image, API reboot with budget: OK

  • Seen: first check is the baseline (+5 min), Camera no_fresh_image (hit 1/2) at +10 min, hit 2/2 and Rebooting camera, no_fresh_image at +15 min, then Reboot raised: ... Max retries exceeded and Reboot command rejected by camera.
  • /patrol/patrol_status shows recovery: {status: no_fresh_image, reboot_attempts_24h: 1, last_reboot_attempt: 2026-10-28T10:42:19Z}.
  • With the fake cameras of feat: fake Reolink camera driven by scenarios pyro-envdev#65 (same frame on every visit) the frozen case was ignored as designed by ALLOW_STILL_CAMERA, with the startup warning.

Not tested: frozen and not_moving on a real moving camera, and the 3 per day budget (stopped before the second attempt).

Remarks:

  • Reboot raised: logs the Reolink URL with password=... in clear. patrol.py already does it on Error at pose, but this PR adds a new place.
  • After a failed attempt hits goes back to 0, so the next hit 2/2 comes at +15 min while the cooldown is 30 min: reboot budget spent will be logged every 5 min for 15 min. Cosmetic.

MateoLostanlen and others added 17 commits November 7, 2026 08:27
…atrols

The Shelly watchdog reads /health to decide whether to power-cycle the Pi.
The healthcheck kills on every curl failure but rc 7, which looked like a hole:
a uvicorn that never binds would stay unhealthy forever. Killing on rc 7 would
loop at startup instead, as the kill runs during start_period too, and a hang
before the bind is bounded by the 10 s timeout on every camera call.
… camera

- The pass only collected the frames it received, so one night frame next to
  a camera answering 204 (no fresh image, PR #458) read as "every camera
  reports night" and put the site to sleep for an hour. The site now sleeps
  only when every expected frame came back and all of them are night frames;
  one day frame still wakes it.
- After the night sleep, only the first camera was probed: in infrared or down,
  it kept the whole site asleep in daylight. Every camera is probed until one
  shows daylight, and the site stays asleep only when all of them answer night.
…at answered

A single infrared frame dropped the camera's detection history, and with it an
ongoing event: cameras flicker between colour and infrared at dusk. The history
is now dropped after 3 night frames in a row, and a camera in infrared while
the others see daylight is warned once, as it sends no heartbeat.

A camera without a fresh image never counted as night, so one dead camera kept
the site awake all night: PTZ patrols ran, autofocus ran on infrared images,
and the stuck detector spent its reboot budget overnight. Only cameras with a
fresh frame count now, and the site sleeps after 3 all-night passes. The
frozen frame case is already handled by the API, which drops it after
max_age_s. Same rule for the wake-up probe.

The identical frame log goes to DEBUG: the engine reads the API cache, which
the patrol refreshes slower than the loop runs, so the same frame twice is the
normal case. The API's stuck detector diagnoses frozen cameras.

The healthcheck killed the API after 25 s of failed checks, as the kill runs
inside the check and `retries` gives no margin. The probe now retries for about
2 min, still well under the 10 min stall limit. The image ships no curl
anymore, so the probe uses Python's HTTP client with the same retries.

Also:
- an unchanged frame no longer counts as a new night frame, so one cached
  infrared capture read on several passes cannot drop an ongoing event
- an unchanged frame still sends the camera heartbeat, so a slow URL camera
  does not look offline
… grace after reboot

- Reboot attempts were in memory with monotonic times, so every container
  restart (healthcheck, update) handed out three new attempts. They are now
  wall-clock times saved in data/camera_recovery.json (RECOVERY_STATE_FILE)
  and loaded at startup, pruned to the last 24 h.
- `Reboot raised: <exc>` logged the Reolink URL with the camera password.
  Only the exception type is logged.
- A static camera rebooted by the detector failed its next two captures while
  booting and entered the 30 min skip window. The detector now resets the
  failure count and gives it 90 s before captures count again.
- /patrol_status iterated the attempts deque while the detector appended to
  it: both sides now hold a lock.
- "reboot budget spent" is logged once per episode instead of every 5 min.
/patrol_status showed one attempt in the last 24 h but no last_reboot_attempt
once the detector restarted: derive it from the attempts loaded from disk.
@MateoLostanlen
MateoLostanlen force-pushed the fix/camera-api-stale-patrol branch from 137c461 to d26367e Compare November 7, 2026 07:36
@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.59036% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.32%. Comparing base (d3c2a9b) to head (1fcb312).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
pyroengine/core.py 97.26% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #461      +/-   ##
===========================================
+ Coverage    81.05%   84.32%   +3.27%     
===========================================
  Files            7        7              
  Lines          871      938      +67     
===========================================
+ Hits           706      791      +85     
+ Misses         165      147      -18     
Flag Coverage Δ
unittests 84.32% <97.59%> (+3.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

fe51
fe51 previously approved these changes Nov 8, 2026

@fe51 fe51 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,

One thing before or right after merge: the detector now runs on every camera, including adapters without reboot_camera (URL, RTSP, rest). When can_reboot is false, hits keeps growing and every check logs Camera X and its adapter cannot reboot it at ERROR, forever. A URL camera refreshing every 5 min or slower is seen as frozen at each check, so it would log a WARNING + ERROR every 5 min all day.

Suggestion: log it once per episode like the budget message (hits == CONSECUTIVE_HITS_BEFORE_REBOOT), or skip frozen for cameras that cannot reboot, since it is not actionable.

Base automatically changed from fix/camera-api-stale-patrol to develop November 9, 2026 07:59
@MateoLostanlen
MateoLostanlen dismissed fe51’s stale review November 9, 2026 07:59

The base branch was changed.

…eboot

A URL or REST source may serve the same cached picture for minutes, which was seen as frozen and logged an error every check.
@MateoLostanlen

Copy link
Copy Markdown
Member Author

Thanks @fe51, fixed in 1fcb312. Frozen is now ignored when the adapter cannot reboot, since a URL or REST source can serve the same cached picture for minutes, and the "cannot reboot" error is logged once per episode. no_fresh_image is still reported for those cameras. Test added.

@MateoLostanlen
MateoLostanlen requested a review from fe51 November 9, 2026 08:05

@fe51 fe51 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MateoLostanlen for taking the feedback into account, lets merge !

@MateoLostanlen
MateoLostanlen merged commit cfeedc9 into develop Nov 18, 2026
12 of 14 checks passed
@MateoLostanlen
MateoLostanlen deleted the feat/camera-recovery branch November 18, 2026 15:30
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.

Recover a stuck or unreachable camera automatically

2 participants