Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

frigate-glance: the card counts down once its objects have left, not when Frigate ends the review

NickLDe63ba832be6b
diff --git a/addons/frigate-glance/README.md b/addons/frigate-glance/README.md
index f63a9599f..d5c5ef3a4 100644
--- a/addons/frigate-glance/README.md
+++ b/addons/frigate-glance/README.md
@@ -73,7 +73,8 @@ and `TestCardReceiver` shows a test card over whatever is playing.
| `severity` | `alerts` | `alerts`, or `alerts_and_detections` |
| `corner` | `top-right` | `top-left`, `bottom-left`, `bottom-right` |
| `cardScale` | `100` | card size in percent (50 to 200), picture and text together |
-| `dismissSeconds` | `8` | seconds the card stays after the event ends (1 to 120) |
+| `dismissSeconds` | `8` | seconds the card stays once the alert's objects have left (1 to 120); an object coming back stops the countdown |
+| `dismissOnStationary` | `true` | a parked object counts as left, as in Peek |
| `snoozeMinutes` | `60` | length of a snooze (5 to 720) |
| `liveVideo` | `true` | live picture from go2rtc's RTSP port 8554; `false` refreshes a snapshot every second |
| `streams` | `{}` | camera to the go2rtc stream for live video, when Frigate's own live stream is too big |
diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardController.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardController.java
index f85994701..ea44fbe08 100644
--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardController.java
+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardController.java
@@ -11,8 +11,11 @@ import java.util.function.LongSupplier;
/**
* When a card shows, changes and leaves. One card at a time: a new matching review replaces it. It leaves
- * dismissSeconds after its review ends, and at the latest 120 s after its last news, so a lost "end"
- * message can never leave a card on screen. Not thread-safe: the service calls it from one thread.
+ * dismissSeconds after its objects have all gone (ended, or parked when dismissOnStationary), as Peek does:
+ * Frigate ends the review itself only after its cutoff, some 30 to 40 s later. An object that comes back
+ * while the card counts down stops the countdown. The review's "end" starts it too, for lost event
+ * messages, and the card leaves at the latest 120 s after its last news, so a lost "end" can never leave it
+ * on screen. Not thread-safe: the service calls it from one thread.
*/
public final class CardController {
static final long HARD_LIMIT_MS = 120_000;
@@ -25,13 +28,18 @@ public final class CardController {
private final ReviewFilter filter;
private final Map<String, String> displayNames;
private final int dismissSeconds;
+ private final boolean dismissOnStationary;
private final LongSupplier clock;
private Review shown;
private final Map<String, Detection> detections = new LinkedHashMap<>();
private long deadline;
- /** True once the shown card is counting down to leaving (its review ended, or a test card). */
+ /** True once the shown card is counting down to leaving (its objects left, its review ended, or a test card). */
private boolean ending;
+ /** Frigate ended the shown review (or it is a test card): no object can stop the countdown any more. */
+ private boolean reviewEnded;
+ /** The shown review's objects Frigate stopped tracking. */
+ private final Set<String> endedIds = new java.util.HashSet<>();
private long snoozedUntil;
/** Reviews that had a card which then left (dismissed, ended, replaced): their updates never reopen one. */
private final java.util.Set<String> closed = java.util.Collections.newSetFromMap(
@@ -43,11 +51,12 @@ public final class CardController {
});
public CardController(CardView view, ReviewFilter filter, Map<String, String> displayNames, int dismissSeconds,
- LongSupplier clock) {
+ boolean dismissOnStationary, LongSupplier clock) {
this.view = view;
this.filter = filter;
this.displayNames = displayNames;
this.dismissSeconds = dismissSeconds;
+ this.dismissOnStationary = dismissOnStationary;
this.clock = clock;
}
@@ -62,30 +71,66 @@ public final class CardController {
if (shown != null) closed.add(shown.id);
shown = r;
detections.clear();
+ endedIds.clear();
deadline = now + quietLimit(r);
ending = false;
+ reviewEnded = false;
+ settle(now);
view.show(model());
} else if (isShown && "update".equals(r.type)) {
shown = r;
- deadline = now + quietLimit(r);
- ending = false;
+ if (!ending) deadline = now + quietLimit(r);
+ settle(now);
view.update(model());
} else if (isShown && "end".equals(r.type)) {
shown = r;
- deadline = now + dismissSeconds * 1000L;
+ if (!ending) deadline = now + dismissSeconds * 1000L;
ending = true;
+ reviewEnded = true;
view.update(model());
}
}
+ /** Starts the countdown when the review's objects have all gone, and stops it when one is back. */
+ private void settle(long now) {
+ if (reviewEnded) return;
+ boolean gone = objectsGone();
+ if (gone && !ending) {
+ ending = true;
+ deadline = now + dismissSeconds * 1000L;
+ } else if (!gone && ending) {
+ ending = false;
+ deadline = now + quietLimit(shown);
+ }
+ }
+
+ /**
+ * Every object the review lists has ended or parked, and at least one was seen. One listed but not yet
+ * heard of counts as present: its event may come after the review update that names it.
+ */
+ private boolean objectsGone() {
+ if (shown.detectionIds.isEmpty()) return false;
+ for (String id : shown.detectionIds) {
+ if (endedIds.contains(id)) continue;
+ Detection d = detections.get(id);
+ if (d == null || !(dismissOnStationary && d.stationary)) return false;
+ }
+ return true;
+ }
+
private static long quietLimit(Review r) {
return r.camera.startsWith(ReviewFilter.REPLAY_PREFIX) ? REPLAY_QUIET_MS : HARD_LIMIT_MS;
}
public void onDetection(Detection d) {
if (shown == null || !shown.detectionIds.contains(d.id)) return;
- if (d.ended) detections.remove(d.id);
- else detections.put(d.id, d);
+ if (d.ended) {
+ detections.remove(d.id);
+ endedIds.add(d.id);
+ } else {
+ detections.put(d.id, d);
+ }
+ settle(clock.getAsLong());
view.update(model());
}
@@ -98,8 +143,10 @@ public final class CardController {
String after = "{\"id\":\"" + TEST_ID + "\",\"camera\":\"" + camera + "\",\"severity\":\"alert\",\"data\":{}}";
shown = Review.parse("{\"type\":\"new\",\"after\":" + after + "}");
detections.clear();
+ endedIds.clear();
deadline = clock.getAsLong() + dismissSeconds * 1000L + TEST_EXTRA_MS;
ending = true;
+ reviewEnded = true;
view.show(model());
}
diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/Detection.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/Detection.java
index 1181eb0ca..68a6ce453 100644
--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/Detection.java
+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/Detection.java
@@ -19,9 +19,12 @@ public final class Detection {
public final List<String> zones;
/** Frigate stopped tracking it ("end"): it has left the picture, so its box goes. */
public final boolean ended;
+ /** Frigate counts it as parked: still in the picture, but no longer moving. */
+ public final boolean stationary;
private Detection(JSONObject a, Map<String, int[]> detectSize, boolean ended) {
this.ended = ended;
+ stationary = a.optBoolean("stationary", false);
id = a.optString("id", "");
camera = a.optString("camera", "");
label = a.optString("label", "");
diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceConfig.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceConfig.java
index b9c7cb2f9..f0715b394 100644
--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceConfig.java
+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceConfig.java
@@ -37,6 +37,8 @@ public final class GlanceConfig {
/** Card size in percent of the standard card (picture, text and bar together). */
public final int cardScale;
public final boolean liveVideo, boxes;
+ /** A parked object counts as gone, as in Peek: a car that stops does not keep the card up. */
+ public final boolean dismissOnStationary;
/** A third card key opens the camera full screen; off by default. */
public final boolean fullscreen;
public final String fullscreenKey;
@@ -61,6 +63,7 @@ public final class GlanceConfig {
cardScale = clamp(o.optInt("cardScale", 100), 50, 200);
liveVideo = o.optBoolean("liveVideo", true);
boxes = o.optBoolean("boxes", true);
+ dismissOnStationary = o.optBoolean("dismissOnStationary", true);
dismissKey = key(o, "dismissKey", "KEYCODE_DPAD_LEFT");
snoozeKey = key(o, "snoozeKey", "KEYCODE_DPAD_RIGHT");
if (dismissKey.equals(snoozeKey)) throw new IllegalArgumentException("dismissKey and snoozeKey are the same key");
@@ -126,6 +129,7 @@ public final class GlanceConfig {
o.put("cardScale", cardScale);
o.put("liveVideo", liveVideo);
o.put("boxes", boxes);
+ o.put("dismissOnStationary", dismissOnStationary);
o.put("dismissKey", dismissKey);
o.put("snoozeKey", snoozeKey);
o.put("fullscreen", fullscreen);
diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java
index fa1c74e06..cdd5477cd 100644
--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java
+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java
@@ -432,7 +432,8 @@ public class GlanceService extends Service {
}
long snoozed = controller == null ? 0 : controller.snoozedUntil();
KeyService.reloadKeys(this);
- controller = new CardController(card, new ReviewFilter(c), c.cameras, c.dismissSeconds, SystemClock::elapsedRealtime);
+ controller = new CardController(card, new ReviewFilter(c), c.cameras, c.dismissSeconds,
+ c.dismissOnStationary, SystemClock::elapsedRealtime);
if (snoozed > 0) controller.snooze((int) Math.max(1, (snoozed - SystemClock.elapsedRealtime()) / 60_000));
String test = pendingTest;
pendingTest = null;
diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java
index 7c430a01e..fe083fb2f 100644
--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java
+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java
@@ -205,9 +205,11 @@ public class SettingsActivity extends Activity {
store.saveSetting("cardScale", SettingsModel.next(SettingsModel.CARD_SCALES, cfg.cardScale))));
row("Corner", "Which corner of the screen the card slides into.", CORNER_NAMES.get(c.corner),
() -> save(cfg -> store.saveSetting("corner", SettingsModel.next(SettingsModel.CORNERS, cfg.corner))));
- row("Card stays", "How long the card stays once the event has ended, while the bar on top runs down.",
+ row("Card stays", "How long the card stays once the objects have left, while the bar on top runs down.",
c.dismissSeconds + " s", () -> save(cfg -> store.saveSetting(
"dismissSeconds", SettingsModel.next(SettingsModel.DISMISS_SECONDS, cfg.dismissSeconds))));
+ row("Parked objects", "A car or person that stops moving counts as gone, so the card starts counting down.",
+ onOff(c.dismissOnStationary), () -> save(cfg -> store.saveSetting("dismissOnStationary", !cfg.dismissOnStationary)));
row("Live video", "Moving picture from the camera. Off, or while another app plays sound, a snapshot every second instead.",
onOff(c.liveVideo), () -> save(cfg -> store.saveSetting("liveVideo", !cfg.liveVideo)));
row("Boxes", "Outlines around the objects Frigate detected, gone when the object leaves.", onOff(c.boxes), () -> save(cfg -> store.saveSetting("boxes", !cfg.boxes)));
diff --git a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardControllerTest.java b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardControllerTest.java
index 9f02490a4..42eb1556a 100644
--- a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardControllerTest.java
+++ b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardControllerTest.java
@@ -36,7 +36,7 @@ public class CardControllerTest {
view = new FakeView();
now = 1_000_000L;
GlanceConfig config = GlanceConfig.parse("{\"mqtt\":\"mqtt://h:1883\",\"frigateUrl\":\"https://h:8971\"}");
- c = new CardController(view, new ReviewFilter(config), Map.of("porch", "Front door"), 8, () -> now);
+ c = new CardController(view, new ReviewFilter(config), Map.of("porch", "Front door"), 8, true, () -> now);
}
private static Review rev(String type, String id, String camera, String severity) {
@@ -44,9 +44,18 @@ public class CardControllerTest {
}
private static Review revWithDetection(String id, String eventId) {
+ return revWithDetections("new", id, eventId);
+ }
+
+ private static Review revWithDetections(String type, String id, String... eventIds) {
String after = "{\"id\":\"" + id + "\",\"camera\":\"porch\",\"severity\":\"alert\",\"data\":{\"detections\":[\""
- + eventId + "\"],\"objects\":[\"person\"],\"zones\":[\"front_yard\"]}}";
- return Review.parse("{\"type\":\"new\",\"after\":" + after + "}");
+ + String.join("\",\"", eventIds) + "\"],\"objects\":[\"person\"],\"zones\":[\"front_yard\"]}}";
+ return Review.parse("{\"type\":\"" + type + "\",\"after\":" + after + "}");
+ }
+
+ private static Detection event(String type, String eventId, boolean stationary) {
+ return Detection.parse("{\"type\":\"" + type + "\",\"after\":{\"id\":\"" + eventId + "\",\"camera\":\"porch\","
+ + "\"label\":\"person\",\"stationary\":" + stationary + "}}", Map.of());
}
private static Detection det(String eventId, float[] box) {
@@ -309,4 +318,81 @@ public class CardControllerTest {
c.dismiss();
assertEquals(null, c.shownCamera());
}
+
+ @Test
+ public void lastObjectLeavingStartsCountdownBeforeTheReviewEnds() {
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("update", "ev1", false));
+ assertEquals(0, view.lastModel.countdownMs);
+ c.onDetection(event("end", "ev1", false));
+ assertEquals(8000, view.lastModel.countdownMs);
+ now += 8001;
+ c.tick();
+ assertFalse(c.isShowing());
+ }
+
+ @Test
+ public void countdownWaitsForEveryObject() {
+ c.onReview(revWithDetections("new", "r1", "ev1", "ev2"));
+ c.onDetection(event("update", "ev1", false));
+ c.onDetection(event("update", "ev2", false));
+ c.onDetection(event("end", "ev1", false));
+ assertEquals(0, view.lastModel.countdownMs);
+ c.onDetection(event("end", "ev2", false));
+ assertEquals(8000, view.lastModel.countdownMs);
+ }
+
+ @Test
+ public void parkedObjectStartsCountdownAndMovingStopsIt() {
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("update", "ev1", true));
+ assertEquals(8000, view.lastModel.countdownMs);
+ now += 5000;
+ c.onDetection(event("update", "ev1", false));
+ assertEquals(0, view.lastModel.countdownMs);
+ now += 5000;
+ c.tick();
+ assertTrue(c.isShowing());
+ }
+
+ @Test
+ public void parkedObjectKeepsCardWhenSwitchedOff() {
+ GlanceConfig config = GlanceConfig.parse("{\"mqtt\":\"mqtt://h:1883\",\"frigateUrl\":\"https://h:8971\"}");
+ c = new CardController(view, new ReviewFilter(config), Map.of(), 8, false, () -> now);
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("update", "ev1", true));
+ assertEquals(0, view.lastModel.countdownMs);
+ }
+
+ @Test
+ public void newObjectNamedByAReviewUpdateStopsCountdown() {
+ // Its event can come before the review update that lists it, and is dropped: the update alone must do.
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("end", "ev1", false));
+ now += 3000;
+ c.onDetection(event("new", "ev2", false));
+ c.onReview(revWithDetections("update", "r1", "ev1", "ev2"));
+ assertEquals(0, view.lastModel.countdownMs);
+ assertTrue(c.isShowing());
+ c.onDetection(event("end", "ev2", false));
+ assertEquals(8000, view.lastModel.countdownMs);
+ }
+
+ @Test
+ public void reviewUpdateDuringCountdownDoesNotRestartIt() {
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("end", "ev1", false));
+ now += 3000;
+ c.onReview(revWithDetections("update", "r1", "ev1"));
+ assertEquals(5000, view.lastModel.countdownMs);
+ }
+
+ @Test
+ public void reviewEndDoesNotRestartARunningCountdown() {
+ c.onReview(revWithDetection("r1", "ev1"));
+ c.onDetection(event("end", "ev1", false));
+ now += 3000;
+ c.onReview(revWithDetections("end", "r1", "ev1"));
+ assertEquals(5000, view.lastModel.countdownMs);
+ }
}