Add MQTT features: routing, device control, discovery, notifications, sleep tracking - #38
Closed
stuart22 wants to merge 22 commits into
Closed
Add MQTT features: routing, device control, discovery, notifications, sleep tracking#38stuart22 wants to merge 22 commits into
stuart22 wants to merge 22 commits into
Conversation
The upstream container reconnects to Nanit's cloud websocket after disconnections but never re-requests the camera to push its RTMP stream, leaving the RTMP server running with no publisher indefinitely. Root cause: StreamState and StreamRequestState are not reset on websocket disconnect, so the reconnection handler sees the stream as "Alive" and skips the re-request. Changes: - Reset StreamState and StreamRequestState on websocket disconnect - Add /health HTTP endpoint (port 8080) for stream liveness checks - Add Docker HEALTHCHECK (3min interval, 2 retries) for auto-recovery - Fix nil pointer bug in GetIsWebsocketAlive (checked wrong field) - Add GitHub Actions CI to build and push to GHCR - Update README with fork attribution and changelog
Buster is EOL and its apt repositories are no longer available, causing build failures.
Give the Go reconnection fix 10 minutes to recover the stream before reporting unhealthy. The health endpoint tracks how long the stream has been down and returns healthy during the grace period. After the grace period, the HEALTHCHECK kills PID 1 so restart: unless-stopped restarts the container. Timeline on stream drop: - 0-10min: Go fix retries (30s, 2min backoff) — health reports OK - 10-16min: Grace expired, HEALTHCHECK fails twice, container restarts
Covers PersistentBroadcaster, go2rtc bundling for UniFi Protect, multi-camera MQTT routing, device control, auto-discovery, notification events, and sleep tracking.
Wrapper script that: - Generates go2rtc config from session.json baby UIDs/names - Starts go2rtc in background for RTSP/ONVIF output - Starts nanit as main process - Forwards SIGTERM/SIGINT to both processes - Exits if either process dies go2rtc is gated behind NANIT_RTSP_ENABLED (default: true) and requires a session.json file to be present.
- Add go2rtc v1.9.8 download stage (multi-arch: amd64/arm64) - Copy go2rtc binary to /app/bin/go2rtc in runtime image - Switch ENTRYPOINT from nanit binary to entrypoint.sh wrapper - Expose port 8554 for RTSP go2rtc reads from the local RTMP server and exposes RTSP/ONVIF endpoints, enabling UniFi Protect integration.
…ions Adapted from scgreenhalgh/home_assistant_nanit. Keeps subscribers connected when the camera's RTMP publisher disconnects and reconnects. Timestamps are remapped to be monotonically increasing across reconnections. Simplified from the reference: no hot standby, source switching, or stall detection.
…ions Adapted from scgreenhalgh/home_assistant_nanit. Keeps subscribers connected when the camera's RTMP publisher disconnects and reconnects. Timestamps are remapped to be monotonically increasing across reconnections. Simplified from the reference: no hot standby, source switching, or stall detection.
Replace per-connection broadcaster with a persistent broadcaster per baby UID. Subscribers now stay connected across publisher reconnections. Remove old broadcaster.go (superseded by PersistentBroadcaster).
Split StartRTMPServer into NewRTMPServer + Run so callers can hold a reference to the server for querying broadcaster state.
Add PersistentBroadcaster for stream continuity across reconnections
Bundle go2rtc for RTSP/ONVIF output
…diefan#33) - Connection registry routes MQTT commands to correct camera by baby UID - Manager registry tracks websocket connection managers per baby - Global MQTT handler registration (moved from per-connection to app-level) - Handler signatures now include babyUID for proper multi-camera routing - ForceReconnect() with cooldown skip for failed command recovery - Pending retry queue replays failed commands after reconnection - New env vars: NANIT_MQTT_RESET_WHEN_FAILED, NANIT_WEBSOCKET_TIMEOUT
- Extend protobuf schema with brightness (Settings), duration/soundtrack (Playback) - Add MQTT subscriptions for brightness, playback (start/stop), and volume commands - Route all commands through connection registry for multi-camera support - JSON payloads for brightness, playback, and volume topics
- Publish discovery configs on MQTT connect for each registered baby - Sensors: temperature, humidity, last motion/sound, stream URL - Binary sensors: night, stream active, camera online, crying, standing, left bed, alert zone, temp/humidity/breathing alerts, low battery, asleep, in bed - Switches: night light, standby mode - Gated behind NANIT_MQTT_DISCOVERY env var (default true when MQTT enabled)
- New notification package with event poller, deduplication, and publisher - Polls Nanit REST API for events with jitter and exponential backoff - Publishes events to MQTT: motion, sound, crying, standing, left bed, alert zone, temperature/humidity/breathing alerts, camera online/offline - New env vars: NANIT_NOTIFICATIONS_ENABLED, NANIT_NOTIFICATIONS_POLL_INTERVAL, NANIT_NOTIFICATIONS_MAX_BACKOFF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
All features gated behind env vars (
NANIT_MQTT_ENABLED,NANIT_NOTIFICATIONS_ENABLED). See README for full env var table.Test plan
go build ./cmd/nanit/)NANIT_MQTT_ENABLED=trueandNANIT_MQTT_BROKER_URLset