node-red-contrib-unifi-ultimate 1.1.11 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -126,22 +126,6 @@ Things you can do:
126
126
  - Play or stop a siren, control relay outputs, and test speaker or siren sound.
127
127
  - Read and select Protect Arm Profiles, then arm or disarm the local Alarm Manager from the NVR control.
128
128
 
129
- ### Automatic Cerebrum / KNX AI camera adapter
130
-
131
- Every configured **Unifi Protect Config** instance automatically publishes its cameras through the shared `node-red.knx-ai.camera-adapters.v1` runtime contract. Cerebrum and KNX AI can discover the installed provider without a Protect Device node, camera selector, or intermediate wire. They can then:
132
-
133
- - send a current camera snapshot to the chat channel;
134
- - describe a fresh snapshot with the configured vision-capable LLM;
135
- - subscribe to motion, line-crossing, intrusion-zone and loiter-zone events;
136
- - filter smart events by classifications such as people, animals, vehicles, faces, license plates and packages, and by an exact Protect line or zone;
137
- - search recorded events with time, camera and detection filters, follow explicit pagination, and retrieve the thumbnail belonging to an exact returned event.
138
-
139
- Recorded history requires the optional **History user/password** in the Protect config node. Use a dedicated local UniFi OS account that can view the required cameras. The Integration API key remains responsible for the official live-event and current-snapshot endpoints; the local account opens only the separate recorded-history session. Credentials and session cookies stay in the Node-RED credential/config node and are never published to Cerebrum, KNX AI, flows or chat context.
140
-
141
- Other camera packages can implement the same catalog, event, snapshot and recorded-history methods without adding vendor-specific code to Cerebrum.
142
-
143
-
144
-
145
129
  <br/>
146
130
  <br/>
147
131
  <p align="left">
@@ -83,6 +83,95 @@ function createSnapshotError({ camera, response, retriedWithStandardQuality }) {
83
83
  return error;
84
84
  }
85
85
 
86
+ const PROTECT_CAMERA_EVENT_DEDUPE_TTL_MS = 60 * 60 * 1000;
87
+ const PROTECT_CAMERA_EVENT_DEDUPE_MAX_ENTRIES = 2048;
88
+
89
+ function cloneProviderMetadataWithoutRaw(value) {
90
+ if (Array.isArray(value)) return value.map(cloneProviderMetadataWithoutRaw);
91
+ if (!value || typeof value !== "object") return value;
92
+ const clone = {};
93
+ Object.entries(value).forEach(([key, nested]) => {
94
+ if (key === "raw") return;
95
+ clone[key] = cloneProviderMetadataWithoutRaw(nested);
96
+ });
97
+ return clone;
98
+ }
99
+
100
+ function sortedSemanticValues(values) {
101
+ return Array.from(new Set((Array.isArray(values) ? values : [])
102
+ .map((value) => String(value === undefined || value === null ? "" : value).trim())
103
+ .filter(Boolean)))
104
+ .sort();
105
+ }
106
+
107
+ function createProtectCameraEventDeduper({
108
+ ttlMs = PROTECT_CAMERA_EVENT_DEDUPE_TTL_MS,
109
+ maxEntries = PROTECT_CAMERA_EVENT_DEDUPE_MAX_ENTRIES
110
+ } = {}) {
111
+ const entries = new Map();
112
+ const normalizedTtl = Math.max(1000, Number(ttlMs) || PROTECT_CAMERA_EVENT_DEDUPE_TTL_MS);
113
+ const normalizedMaxEntries = Math.max(1, Math.floor(Number(maxEntries) || PROTECT_CAMERA_EVENT_DEDUPE_MAX_ENTRIES));
114
+ let lastPrunedAt = 0;
115
+
116
+ function prune(now, force = false) {
117
+ if (!force && entries.size < normalizedMaxEntries && now - lastPrunedAt < Math.min(normalizedTtl, 60000)) return;
118
+ entries.forEach((entry, key) => {
119
+ if (now - entry.seenAt > normalizedTtl) entries.delete(key);
120
+ });
121
+ lastPrunedAt = now;
122
+ }
123
+
124
+ return {
125
+ shouldForward(event, sourceEvent, now = Date.now()) {
126
+ if (!event || typeof event !== "object") return false;
127
+ const checkedAt = Number.isFinite(Number(now)) ? Number(now) : Date.now();
128
+ prune(checkedAt);
129
+ const eventId = String(event.eventId || "").trim();
130
+ const key = eventId
131
+ ? `id:${eventId}`
132
+ : `fallback:${event.cameraId || event.nativeCameraId || ""}:${event.eventType || ""}:${event.at || ""}`;
133
+ const rawEnd = sourceEvent && sourceEvent.end !== undefined && sourceEvent.end !== null
134
+ ? String(sourceEvent.end)
135
+ : null;
136
+ const fingerprint = JSON.stringify({
137
+ source: event.source || "",
138
+ controllerId: event.controllerId || "",
139
+ controllerName: event.controllerName || "",
140
+ cameraId: event.cameraId || "",
141
+ nativeCameraId: event.nativeCameraId || "",
142
+ cameraName: event.cameraName || "",
143
+ eventId,
144
+ eventType: event.eventType || "",
145
+ at: event.at || "",
146
+ active: event.active === true,
147
+ end: rawEnd,
148
+ scopeId: event.scopeId || "",
149
+ scopeName: event.scopeName || "",
150
+ scopeIds: sortedSemanticValues(event.scopeIds),
151
+ objectTypes: sortedSemanticValues(event.objectTypes)
152
+ });
153
+ const previous = entries.get(key);
154
+ const isDuplicate = previous
155
+ && previous.fingerprint === fingerprint
156
+ && checkedAt - previous.seenAt <= normalizedTtl;
157
+
158
+ // Refresh insertion order even for duplicates so a noisy active event
159
+ // remains suppressed until it has actually gone quiet for the TTL.
160
+ entries.delete(key);
161
+ entries.set(key, { fingerprint, seenAt: checkedAt });
162
+ if (entries.size > normalizedMaxEntries) {
163
+ const oldestKey = entries.keys().next().value;
164
+ if (oldestKey !== undefined) entries.delete(oldestKey);
165
+ }
166
+ return !isDuplicate;
167
+ },
168
+ clear() {
169
+ entries.clear();
170
+ lastPrunedAt = 0;
171
+ }
172
+ };
173
+ }
174
+
86
175
  module.exports = function(RED) {
87
176
  const knxAiCameraRegistry = getKnxAiCameraRegistry();
88
177
  knxAiCameraRegistry.registerAdapter({
@@ -116,6 +205,7 @@ module.exports = function(RED) {
116
205
  node.protectHistorySessionHeaders = null;
117
206
  node.protectHistoryAuthentication = null;
118
207
  node.protectHistoryPaginationGuard = createProtectHistoryPaginationGuard();
208
+ node.protectCameraEventDeduper = createProtectCameraEventDeduper();
119
209
 
120
210
  node.getApiKey = () => node.credentials && node.credentials.apiKey;
121
211
 
@@ -458,7 +548,7 @@ module.exports = function(RED) {
458
548
  return {
459
549
  data: response.payload,
460
550
  mediaType,
461
- camera,
551
+ camera: cloneProviderMetadataWithoutRaw(camera),
462
552
  statusCode: response.statusCode
463
553
  };
464
554
  };
@@ -537,7 +627,7 @@ module.exports = function(RED) {
537
627
  events: payload
538
628
  });
539
629
  return {
540
- events: continuation.duplicatePage ? [] : selected,
630
+ events: continuation.duplicatePage ? [] : selected.map(cloneProviderMetadataWithoutRaw),
541
631
  from: normalized.from,
542
632
  to: normalized.to,
543
633
  offset: normalized.offset,
@@ -788,8 +878,10 @@ module.exports = function(RED) {
788
878
  controllerName: node.name || node.host || node.id
789
879
  });
790
880
  if (!event) return;
881
+ if (!node.protectCameraEventDeduper.shouldForward(event, item)) return;
882
+ const publicEvent = cloneProviderMetadataWithoutRaw(event);
791
883
  node.knxAiCameraListeners.forEach((listener) => {
792
- try { listener(event); } catch (error) { }
884
+ try { listener(publicEvent); } catch (error) { }
793
885
  });
794
886
  }).catch((error) => {
795
887
  node.warn(`KNX AI camera event adapter failed: ${error && error.message ? error.message : error}`);
@@ -805,8 +897,9 @@ module.exports = function(RED) {
805
897
  packageName: "node-red-contrib-unifi-ultimate",
806
898
  controllerId: node.id,
807
899
  controllerName: node.name || node.host || node.id,
900
+ eventRetention: "none",
808
901
  capabilities: ["camera_catalog", "snapshot", "motion", "smart_events", "zones", "lines"].concat(historyCapabilities),
809
- listCameras: (options) => node.listKnxAiCameras(options),
902
+ listCameras: async (options) => (await node.listKnxAiCameras(options)).map(cloneProviderMetadataWithoutRaw),
810
903
  takeSnapshot: (request) => node.takeKnxAiCameraSnapshot(request),
811
904
  subscribe(listener) {
812
905
  if (typeof listener !== "function") return () => { };
@@ -833,6 +926,7 @@ module.exports = function(RED) {
833
926
  node.knxAiCameraListeners.clear();
834
927
  node.protectHistorySessionHeaders = null;
835
928
  node.protectHistoryPaginationGuard.clear();
929
+ node.protectCameraEventDeduper.clear();
836
930
  node.removeClient(knxAiBridgeClient);
837
931
  node.closeWebSockets();
838
932
  } catch (error) {
@@ -26,8 +26,10 @@ function normalizeSearchText(value) {
26
26
  // This is the vendor-neutral KNX AI camera adapter contract. Keep this registry
27
27
  // shape aligned with KNX Ultimate: an adapter describes package capabilities,
28
28
  // while each configured controller registers a provider implementing
29
- // listCameras(), takeSnapshot(), and subscribe(). The shared Symbol avoids any
30
- // direct package dependency or flow wiring.
29
+ // listCameras(), takeSnapshot(), and subscribe(). Optional queryEvents() and
30
+ // takeEventSnapshot() expose recorded evidence on demand. Providers declaring
31
+ // eventRetention: "none" keep their live feed transient in consumers. The
32
+ // shared Symbol avoids any direct package dependency or flow wiring.
31
33
  function getKnxAiCameraRegistry() {
32
34
  const existing = globalThis[KNX_AI_CAMERA_REGISTRY_KEY];
33
35
  if (existing && existing.version === 1 && existing.adapters instanceof Map && existing.providers instanceof Map) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",