diff --git a/BURST_LISTENING_TEST.md b/BURST_LISTENING_TEST.md new file mode 100644 index 0000000..53fd40a --- /dev/null +++ b/BURST_LISTENING_TEST.md @@ -0,0 +1,28 @@ +# Burst Listening iPhone PWA field test + +This feature is a foreground experiment. An installed PWA, Guided Access, or a +screen wake lock cannot guarantee microphone continuity after screen lock, +backgrounding, an incoming call, or iOS memory pressure. Keep the screen on +and watch the visible stream state. Do not count time after an interruption as +covered listening time. + +For each run, record the iPhone model, iOS version, browser/PWA mode, network, +selected test setup, start/end time, and battery levels. Export the diagnostic +JSON before closing the review page. Compare its last client audio frame, +last server heartbeat, server timeout detection, and first resumed observation. +The interval between them is uncertain; neither timestamp proves when the mic +stopped. A server timeout proves only that HTTP heartbeats stopped arriving. + +| Duration | Setup | Stimulus and checks | +| --- | --- | --- | +| 10 min | Foreground, screen on | Four spaced bangs; verify four playable clips, quiet interval, stop/review/submit. | +| 15 min | Installed Home Screen PWA, Guided Access with Display Auto-Lock Never | Four spaced bangs and two close bangs; check clip grouping, waveform, timer, battery, memory and server timeline. | +| 20 min | Foreground with optional Wake Lock | Verify auto-stop, bounded clips, upload retries, cleanup and server expiry. | +| Short runs | Manual lock, natural auto-lock, background/foreground, incoming call, low battery, network loss/reconnect | Confirm visible interruption, no automatic restart, recovered clips and uncertainty interval. | + +The shared server watchdog must be running for the independent 15-second +heartbeat-lost diagnostic. Start the provided systemd user service or run +`python manage.py watch_burst_sessions --interval 5` under the same environment +as Django. The 10/15/20-minute physical iPhone scenarios remain unverified +until tested on a device. Local browser tests cannot emulate iOS suspension or +microphone access under Guided Access. diff --git a/README.md b/README.md index d837784..aac5957 100644 --- a/README.md +++ b/README.md @@ -150,3 +150,14 @@ The service runs `/home/jorenmartijn/python/.venv-notifications/bin/python` with project as its working directory. To keep the user timer running after logout, run `sudo loginctl enable-linger jorenmartijn` once on the laptop. Check a run with `journalctl --user -u send-pending-notifications.service -n 30`. + +### Burst Listening watchdog + +Run one shared `watch_burst_sessions` process alongside Django so a suspended PWA is +marked heartbeat-lost independently of browser JavaScript. A systemd user service +is provided at `deploy/systemd/watch-burst-sessions.service`; enable it for the +same user and Python environment as Django. `--once` is available for tests and +manual checks. A one-minute cron job cannot meet the 15-second timeout target. +For a user service: copy the unit to `~/.config/systemd/user/`, run +`systemctl --user daemon-reload`, then +`systemctl --user enable --now watch-burst-sessions.service`. diff --git a/db.sqlite3 b/db.sqlite3 index 9101da1..e1d0c71 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/deploy/systemd/watch-burst-sessions.service b/deploy/systemd/watch-burst-sessions.service new file mode 100644 index 0000000..4ccf628 --- /dev/null +++ b/deploy/systemd/watch-burst-sessions.service @@ -0,0 +1,13 @@ +[Unit] +Description=BoomCast Burst Listening heartbeat watchdog +After=network.target + +[Service] +Type=simple +WorkingDirectory=/home/jorenmartijn/python/django +ExecStart=/home/jorenmartijn/python/.venv/bin/python /home/jorenmartijn/python/django/manage.py watch_burst_sessions --interval 5 +Restart=always +RestartSec=5 + +[Install] +WantedBy=default.target diff --git a/fireworks_detect/__pycache__/admin.cpython-312.pyc b/fireworks_detect/__pycache__/admin.cpython-312.pyc index 862dd98..da5be5c 100644 Binary files a/fireworks_detect/__pycache__/admin.cpython-312.pyc and b/fireworks_detect/__pycache__/admin.cpython-312.pyc differ diff --git a/fireworks_detect/__pycache__/admin.cpython-313.pyc b/fireworks_detect/__pycache__/admin.cpython-313.pyc index 571cc12..0e916e3 100644 Binary files a/fireworks_detect/__pycache__/admin.cpython-313.pyc and b/fireworks_detect/__pycache__/admin.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/models.cpython-312.pyc b/fireworks_detect/__pycache__/models.cpython-312.pyc index f6e0f57..b376f57 100644 Binary files a/fireworks_detect/__pycache__/models.cpython-312.pyc and b/fireworks_detect/__pycache__/models.cpython-312.pyc differ diff --git a/fireworks_detect/__pycache__/models.cpython-313.pyc b/fireworks_detect/__pycache__/models.cpython-313.pyc index 7e9c35c..41e0f01 100644 Binary files a/fireworks_detect/__pycache__/models.cpython-313.pyc and b/fireworks_detect/__pycache__/models.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/test_burst_sessions.cpython-313.pyc b/fireworks_detect/__pycache__/test_burst_sessions.cpython-313.pyc new file mode 100644 index 0000000..b9849c6 Binary files /dev/null and b/fireworks_detect/__pycache__/test_burst_sessions.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/urls.cpython-312.pyc b/fireworks_detect/__pycache__/urls.cpython-312.pyc index f7d2c50..d31e1fa 100644 Binary files a/fireworks_detect/__pycache__/urls.cpython-312.pyc and b/fireworks_detect/__pycache__/urls.cpython-312.pyc differ diff --git a/fireworks_detect/__pycache__/urls.cpython-313.pyc b/fireworks_detect/__pycache__/urls.cpython-313.pyc index 27cb2a7..30bf667 100644 Binary files a/fireworks_detect/__pycache__/urls.cpython-313.pyc and b/fireworks_detect/__pycache__/urls.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/views.cpython-312.pyc b/fireworks_detect/__pycache__/views.cpython-312.pyc index b6bc7e0..3a7ffcd 100644 Binary files a/fireworks_detect/__pycache__/views.cpython-312.pyc and b/fireworks_detect/__pycache__/views.cpython-312.pyc differ diff --git a/fireworks_detect/__pycache__/views.cpython-313.pyc b/fireworks_detect/__pycache__/views.cpython-313.pyc index 3352251..a213d1b 100644 Binary files a/fireworks_detect/__pycache__/views.cpython-313.pyc and b/fireworks_detect/__pycache__/views.cpython-313.pyc differ diff --git a/fireworks_detect/admin.py b/fireworks_detect/admin.py index 5fe0bf8..df4d9bb 100644 --- a/fireworks_detect/admin.py +++ b/fireworks_detect/admin.py @@ -7,10 +7,10 @@ from django.core.exceptions import PermissionDenied from django.http import HttpResponseNotAllowed from django.shortcuts import get_object_or_404, redirect from django.urls import path, reverse -from django.utils.html import format_html +from django.utils.html import format_html, format_html_join from django.utils.translation import gettext as _, gettext_lazy, ngettext -from .models import BrowserProcessingSettings, ClassificationRule, Event, ManualReportRevision, PendingEvent, Sensor +from .models import BrowserProcessingSettings, BurstCapture, BurstSession, ClassificationRule, Event, ManualReportRevision, PendingEvent, Sensor from . import views as classifier_views @@ -70,7 +70,18 @@ class PendingEventAdmin(ClassificationAdmin): actions = ("approve_selected", "deny_selected") list_display = ("original_filename", "source_type", "sensor", "sensor_owner", "captured_at", "created_at", "primary_label", "primary_score", "duration") readonly_fields = ClassificationAdmin.readonly_fields + ("sensor_owner", "captured_at", "firmware_version", "device_metadata") - fields = ClassificationAdmin.fields + ("sensor_owner", "captured_at", "firmware_version", "device_metadata", "pretty_classifier_response") + fields = ClassificationAdmin.fields + ("sensor_owner", "captured_at", "firmware_version", "device_metadata", "burst_captures", "pretty_classifier_response") + readonly_fields = ClassificationAdmin.readonly_fields + ("sensor_owner", "captured_at", "firmware_version", "device_metadata", "burst_captures") + + @admin.display(description=gettext_lazy("Burst captures")) + def burst_captures(self, obj): + if not hasattr(obj, "burst_session"): + return "—" + parts = [] + for capture in obj.burst_session.captures.all(): + url = reverse("fireworks_detect:burst_capture_audio", args=[capture.pk]) + parts.append(format_html('
{} · {}s{}
', capture.captured_at, f"{capture.duration_seconds:.1f}", " · primary" if capture.is_primary else "", url)) + return format_html("
{}
", format_html_join("", "{}", ((part,) for part in parts))) @admin.display(description=gettext_lazy("Sensor owner")) def sensor_owner(self, obj): diff --git a/fireworks_detect/management/commands/__pycache__/watch_burst_sessions.cpython-313.pyc b/fireworks_detect/management/commands/__pycache__/watch_burst_sessions.cpython-313.pyc new file mode 100644 index 0000000..13753d7 Binary files /dev/null and b/fireworks_detect/management/commands/__pycache__/watch_burst_sessions.cpython-313.pyc differ diff --git a/fireworks_detect/management/commands/watch_burst_sessions.py b/fireworks_detect/management/commands/watch_burst_sessions.py new file mode 100644 index 0000000..23ce629 --- /dev/null +++ b/fireworks_detect/management/commands/watch_burst_sessions.py @@ -0,0 +1,40 @@ +import time +from datetime import timedelta + +from django.core.management.base import BaseCommand +from django.utils import timezone + +from fireworks_detect.models import BurstSession + + +class Command(BaseCommand): + help = "Check every active Burst Listening session independently for lost HTTP heartbeats." + + def add_arguments(self, parser): + parser.add_argument("--once", action="store_true") + parser.add_argument("--interval", type=float, default=5) + + def handle(self, *args, **options): + while True: + now = timezone.now() + BurstSession.objects.filter(ended_at__lt=now - timedelta(days=7)).exclude(diagnostic_events=[]).update(diagnostic_events=[]) + for session in BurstSession.objects.filter(ended_at__isnull=True).exclude(status="expired").iterator(): + if now > session.started_at + timedelta(minutes=21): + session.status = "expired" + session.ended_at = now + kind = "watchdog_expired" + elif now > (session.last_seen_at or session.started_at) + timedelta(seconds=15) and session.timeout_at is None: + session.status = "heartbeat_lost" + session.timeout_at = now + kind = "heartbeat_lost" + else: + continue + events = session.diagnostic_events + if len(events) >= 200: + events = events[:20] + events[-179:] + events.append({"kind": kind, "server_at": now.isoformat(), "last_seen_at": session.last_seen_at.isoformat() if session.last_seen_at else None}) + session.diagnostic_events = events + session.save(update_fields=["status", "ended_at", "timeout_at", "diagnostic_events"]) + if options["once"]: + break + time.sleep(max(1, options["interval"])) diff --git a/fireworks_detect/migrations/0015_burstsession_burstcapture.py b/fireworks_detect/migrations/0015_burstsession_burstcapture.py new file mode 100644 index 0000000..36e571f --- /dev/null +++ b/fireworks_detect/migrations/0015_burstsession_burstcapture.py @@ -0,0 +1,51 @@ +# Generated by Django 6.1.1 on 2026-10-15 10:53 + +import django.db.models.deletion +import django.utils.timezone +import uuid +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fireworks_detect', '0014_fieldnotecontext_fieldnoteassociation_fieldnote_and_more'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='BurstSession', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('started_at', models.DateTimeField(default=django.utils.timezone.now)), + ('ended_at', models.DateTimeField(blank=True, null=True)), + ('maximum_minutes', models.PositiveSmallIntegerField(default=15)), + ('status', models.CharField(default='listening', max_length=24)), + ('last_seen_at', models.DateTimeField(blank=True, null=True)), + ('last_audio_at', models.DateTimeField(blank=True, null=True)), + ('last_audio_frame', models.PositiveBigIntegerField(default=0)), + ('last_sequence', models.PositiveIntegerField(default=0)), + ('timeout_at', models.DateTimeField(blank=True, null=True)), + ('diagnostic_events', models.JSONField(default=list)), + ('event', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='burst_session', to='fireworks_detect.event')), + ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='burst_sessions', to=settings.AUTH_USER_MODEL)), + ('pending_event', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='burst_session', to='fireworks_detect.pendingevent')), + ], + ), + migrations.CreateModel( + name='BurstCapture', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('captured_at', models.DateTimeField()), + ('duration_seconds', models.FloatField()), + ('audio_file', models.FileField(upload_to='fireworks_audio/%Y/%m/%d')), + ('is_primary', models.BooleanField(default=False)), + ('session', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='captures', to='fireworks_detect.burstsession')), + ], + options={ + 'ordering': ['captured_at', 'pk'], + }, + ), + ] diff --git a/fireworks_detect/migrations/__pycache__/0015_burstsession_burstcapture.cpython-313.pyc b/fireworks_detect/migrations/__pycache__/0015_burstsession_burstcapture.cpython-313.pyc new file mode 100644 index 0000000..0ef4d77 Binary files /dev/null and b/fireworks_detect/migrations/__pycache__/0015_burstsession_burstcapture.cpython-313.pyc differ diff --git a/fireworks_detect/models.py b/fireworks_detect/models.py index 983db32..5855a74 100644 --- a/fireworks_detect/models.py +++ b/fireworks_detect/models.py @@ -290,6 +290,11 @@ class PendingEvent(ClassifiedAudio): direction_source=self.direction_source, ) from .field_notes import associate + if hasattr(self, "burst_session"): + session = self.burst_session + session.pending_event = None + session.event = event + session.save(update_fields=["pending_event", "event"]) for context in self.note_contexts.select_for_update(): associate(context, event=event, actor=actor, reason="approved") self.delete() @@ -297,6 +302,11 @@ class PendingEvent(ClassifiedAudio): def deny(self): file_name = self.audio_file.name + if hasattr(self, "burst_session"): + for capture in self.burst_session.captures.all(): + if capture.audio_file.name and capture.audio_file.name != file_name: + default_storage.delete(capture.audio_file.name) + self.burst_session.captures.all().delete() self.delete() if file_name: default_storage.delete(file_name) @@ -313,6 +323,34 @@ class Event(ClassifiedAudio): verbose_name_plural = _("Events") +class BurstSession(models.Model): + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="burst_sessions") + pending_event = models.OneToOneField(PendingEvent, null=True, blank=True, on_delete=models.SET_NULL, related_name="burst_session") + event = models.OneToOneField(Event, null=True, blank=True, on_delete=models.SET_NULL, related_name="burst_session") + started_at = models.DateTimeField(default=timezone.now) + ended_at = models.DateTimeField(null=True, blank=True) + maximum_minutes = models.PositiveSmallIntegerField(default=15) + status = models.CharField(max_length=24, default="listening") + last_seen_at = models.DateTimeField(null=True, blank=True) + last_audio_at = models.DateTimeField(null=True, blank=True) + last_audio_frame = models.PositiveBigIntegerField(default=0) + last_sequence = models.PositiveIntegerField(default=0) + timeout_at = models.DateTimeField(null=True, blank=True) + diagnostic_events = models.JSONField(default=list) + + +class BurstCapture(models.Model): + session = models.ForeignKey(BurstSession, on_delete=models.CASCADE, related_name="captures") + captured_at = models.DateTimeField() + duration_seconds = models.FloatField() + audio_file = models.FileField(upload_to="fireworks_audio/%Y/%m/%d") + is_primary = models.BooleanField(default=False) + + class Meta: + ordering = ["captured_at", "pk"] + + class ManualReportRevision(models.Model): """Record edits to a session-owned manual Pending report.""" diff --git a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html index 28caeb1..7ae2a8a 100644 --- a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html +++ b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html @@ -63,11 +63,24 @@
+ +