mirror of
https://github.com/BetterCorp/BetterFrame.git
synced 2026-05-26 19:06:34 +00:00
feat(events): dedup ONVIF events within 2s window (Hikvision double-fire fix)
This commit is contained in:
parent
a92e927b3b
commit
a414f98c56
1 changed files with 22 additions and 0 deletions
|
|
@ -275,6 +275,9 @@ function registerPairingRoutes(
|
|||
|
||||
// ---- Kiosk routes (require Bearer kiosk key) --------------------------------
|
||||
|
||||
// Event deduplication cache: key → last-seen timestamp (ms).
|
||||
const eventDedupCache = new Map<string, number>();
|
||||
|
||||
function registerKioskRoutes(
|
||||
app: H3,
|
||||
repo: Repository,
|
||||
|
|
@ -530,6 +533,25 @@ function registerKioskRoutes(
|
|||
|
||||
if (!body?.topic) throw createError({ statusCode: 400, statusMessage: "topic required" });
|
||||
|
||||
// Dedup: Hikvision cameras send duplicate ONVIF events within ~1s.
|
||||
// Key = kiosk_id:camera_id:topic:source_keys_hash. Window = 2s.
|
||||
const dedupKey = `${kiosk.id}:${body.camera_id ?? 0}:${body.topic}:${JSON.stringify(body.payload?.["source"] ?? "")}`;
|
||||
const now = Date.now();
|
||||
if (eventDedupCache.has(dedupKey)) {
|
||||
const lastSeen = eventDedupCache.get(dedupKey)!;
|
||||
if (now - lastSeen < 2000) {
|
||||
return { ok: true, event_id: null, deduplicated: true };
|
||||
}
|
||||
}
|
||||
eventDedupCache.set(dedupKey, now);
|
||||
// Trim cache periodically (prevent unbounded growth).
|
||||
if (eventDedupCache.size > 10_000) {
|
||||
const cutoff = now - 5000;
|
||||
for (const [k, v] of eventDedupCache) {
|
||||
if (v < cutoff) eventDedupCache.delete(k);
|
||||
}
|
||||
}
|
||||
|
||||
const eventId = repo.insertEvent({
|
||||
source_kiosk_id: kiosk.id,
|
||||
source_camera_id: body.camera_id ?? null,
|
||||
|
|
|
|||
Loading…
Reference in a new issue