node-red-contrib-unifi-ultimate 1.1.13 → 1.1.14

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.
@@ -79,6 +79,7 @@ module.exports = function(RED) {
79
79
  node.isClosing = false;
80
80
  node.activeDoorbells = new Map();
81
81
  node.activeDoorbellRequests = new Map();
82
+ node.doorbellStateUpdatedAt = new Map();
82
83
  node.doorbellLogPollTimer = null;
83
84
  node.doorbellLogPollInFlight = false;
84
85
  node.doorbellLogPollCursor = 0;
@@ -243,25 +244,23 @@ module.exports = function(RED) {
243
244
 
244
245
  const normalizedEntry = entry && typeof entry === "object" ? { ...entry } : {};
245
246
  normalizedEntry.requestId = normalizeString(normalizedEntry.requestId);
247
+ normalizedEntry.updatedAt = Number(normalizedEntry.updatedAt) || Date.now();
246
248
  node.activeDoorbells.set(normalizedId, normalizedEntry);
249
+ node.doorbellStateUpdatedAt.set(normalizedId, normalizedEntry.updatedAt);
247
250
 
248
251
  if (normalizedEntry.requestId) {
249
252
  node.activeDoorbellRequests.set(normalizedEntry.requestId, normalizedId);
250
253
  }
251
254
  };
252
255
 
253
- node.clearActiveDoorbell = (deviceId, expectedRequestIds) => {
256
+ node.clearActiveDoorbell = (deviceId, expectedRequestIds, updatedAt = Date.now()) => {
254
257
  const normalizedId = normalizeString(deviceId);
255
258
  if (!normalizedId) {
256
259
  return false;
257
260
  }
258
261
 
259
262
  const current = node.activeDoorbells.get(normalizedId);
260
- if (!current) {
261
- return false;
262
- }
263
-
264
- const requestId = normalizeString(current.requestId);
263
+ const requestId = normalizeString(current && current.requestId);
265
264
  const expectedIds = Array.isArray(expectedRequestIds)
266
265
  ? expectedRequestIds.map((value) => normalizeString(value)).filter(Boolean)
267
266
  : [];
@@ -271,6 +270,16 @@ module.exports = function(RED) {
271
270
  return false;
272
271
  }
273
272
 
273
+ // Remember completions even without an active entry: a delayed
274
+ // incoming log must not re-enable cancel for a call that has ended.
275
+ node.doorbellStateUpdatedAt.set(normalizedId, Math.max(
276
+ node.doorbellStateUpdatedAt.get(normalizedId) || 0,
277
+ updatedAt
278
+ ));
279
+ if (!current) {
280
+ return false;
281
+ }
282
+
274
283
  node.activeDoorbells.delete(normalizedId);
275
284
  if (requestId && node.activeDoorbellRequests.get(requestId) === normalizedId) {
276
285
  node.activeDoorbellRequests.delete(requestId);
@@ -407,15 +416,31 @@ module.exports = function(RED) {
407
416
  const eventType = normalizeEventName(event.type);
408
417
  const logKey = normalizeEventName(event.log_key);
409
418
  const deviceId = node.extractDoorbellDeviceIdFromLogEntry(entry);
419
+ const isIncoming = eventType === DOORBELL_LOG_EVENT_REMOTE_CALL_REQUEST;
420
+ const isCompleted = eventType === DOORBELL_LOG_EVENT_DOOR_UNLOCK && logKey.includes("access.doorbell.");
421
+ const publishedMs = Number(event.published);
422
+
423
+ // Logs are historical evidence. Without a timestamp they cannot
424
+ // safely establish that a ring is still active.
425
+ if ((!isIncoming && !isCompleted) || !deviceId || !Number.isFinite(publishedMs) || publishedMs <= 0) {
426
+ return false;
427
+ }
428
+
429
+ const current = node.getActiveDoorbell(deviceId);
430
+ const lastUpdatedAt = node.doorbellStateUpdatedAt.get(deviceId) || 0;
431
+ if (publishedMs < lastUpdatedAt || (isIncoming && publishedMs === lastUpdatedAt)) {
432
+ return false;
433
+ }
410
434
 
411
435
  // Doorbell request log means a ring has just started.
412
- if (eventType === DOORBELL_LOG_EVENT_REMOTE_CALL_REQUEST) {
413
- if (!deviceId) {
436
+ if (isIncoming) {
437
+ // Polling is a fallback, not a replacement for a live call.
438
+ // Overwriting it loses the websocket request id and reduces
439
+ // its lifetime from three minutes to the 25-second log window.
440
+ if (current && current.source !== "logs") {
414
441
  return false;
415
442
  }
416
443
 
417
- const published = Number(event.published);
418
- const publishedMs = Number.isFinite(published) && published > 0 ? published : Date.now();
419
444
  const expiresAt = Math.min(
420
445
  publishedMs + DOORBELL_LOG_ACTIVE_WINDOW_MS,
421
446
  Date.now() + DOORBELL_LOG_ACTIVE_WINDOW_MS
@@ -437,11 +462,8 @@ module.exports = function(RED) {
437
462
  // Access writes call-end outcomes as access.door.unlock with a
438
463
  // doorbell-specific log key (for example "missed"). Treat those as
439
464
  // completion signals and clear the tracked ring.
440
- if (eventType === DOORBELL_LOG_EVENT_DOOR_UNLOCK && logKey.includes("access.doorbell.")) {
441
- if (!deviceId) {
442
- return false;
443
- }
444
- node.clearActiveDoorbell(deviceId);
465
+ if (isCompleted) {
466
+ node.clearActiveDoorbell(deviceId, [], publishedMs);
445
467
  return true;
446
468
  }
447
469
 
@@ -473,7 +495,13 @@ module.exports = function(RED) {
473
495
  }
474
496
 
475
497
  const logPayload = extractAccessData(response.payload);
476
- const hits = Array.isArray(logPayload && logPayload.hits) ? logPayload.hits : [];
498
+ // Access returns newest-first batches. Apply starts before their
499
+ // corresponding completions, independent of API response order.
500
+ const hits = Array.isArray(logPayload && logPayload.hits)
501
+ ? logPayload.hits.slice().sort((left, right) =>
502
+ (Number(left && left._source && left._source.event && left._source.event.published) || 0)
503
+ - (Number(right && right._source && right._source.event && right._source.event.published) || 0))
504
+ : [];
477
505
  let matchedEvents = 0;
478
506
 
479
507
  for (const entry of hits) {
@@ -558,7 +586,12 @@ module.exports = function(RED) {
558
586
  Array.from(node.activeDoorbells.entries()).forEach(([deviceId, entry]) => {
559
587
  const expiresAt = Number(entry && entry.expiresAt);
560
588
  if (Number.isFinite(expiresAt) && expiresAt > 0 && expiresAt <= now) {
561
- node.clearActiveDoorbell(deviceId);
589
+ node.clearActiveDoorbell(deviceId, [], expiresAt);
590
+ }
591
+ });
592
+ node.doorbellStateUpdatedAt.forEach((updatedAt, deviceId) => {
593
+ if (updatedAt + ACTIVE_DOORBELL_TTL_MS <= now) {
594
+ node.doorbellStateUpdatedAt.delete(deviceId);
562
595
  }
563
596
  });
564
597
  };
@@ -600,8 +633,7 @@ module.exports = function(RED) {
600
633
  node.markDoorbellCanceled = (deviceId) => {
601
634
  const normalizedId = normalizeString(deviceId);
602
635
  if (!normalizedId) {
603
- node.activeDoorbells.clear();
604
- node.activeDoorbellRequests.clear();
636
+ Array.from(node.activeDoorbells.keys()).forEach((id) => node.clearActiveDoorbell(id));
605
637
  return;
606
638
  }
607
639
 
@@ -736,6 +768,7 @@ module.exports = function(RED) {
736
768
  node.isClosing = true;
737
769
  node.activeDoorbells.clear();
738
770
  node.activeDoorbellRequests.clear();
771
+ node.doorbellStateUpdatedAt.clear();
739
772
  node.closeWebSocket();
740
773
  node.stopDoorbellLogPolling();
741
774
  } catch (error) {
@@ -1155,7 +1155,7 @@
1155
1155
  <p>For <code>Set Temporary Lock Rule</code>, choose <code>Custom Duration</code> to reveal the minutes field. Legacy <code>ended_time</code> is still available only as fallback compatibility.</p>
1156
1156
  <p><code>Receive Events</code> fetches the current state once and then subscribes to events provided by the shared UniFi Access config node. The config node maintains the official notifications WebSocket and fans out events to runtime nodes. In this mode, <code>All</code> emits every event for the selected device/door, while specific <code>Event</code> choices filter to official UniFi Access event families (for example doorbell, door unlock, DPS, emergency, schedule, and temporary unlock events). The input pin is hidden and listening starts automatically after deploy/restart.</p>
1157
1157
  <p><code>Trigger Doorbell</code> and <code>Cancel Doorbell</code> use a manual <code>Room Name</code> field when needed. The node does not currently discover Intercom directory names automatically.</p>
1158
- <p><code>Cancel Doorbell</code> uses a shared safe-cancel guard in the UniFi Access config node with a 3 minute tracking window. Tracking is updated by websocket events and a fallback system-log poller.</p>
1158
+ <p><code>Cancel Doorbell</code> only runs while the selected device has a tracked active ring. WebSocket events and <code>Trigger Doorbell</code> requests are tracked for up to 3 minutes, or until the call ends. A fallback system-log poller requires <code>view:system_log</code> permission and uses a shorter 25 second window when no live call is tracked; it does not shorten live tracking. Delayed logs cannot reactivate a completed call or clear a newer one.</p>
1159
1159
  <p>Visitors support state reads and the official visitor-status event. Access policies, schedules, holiday groups, and door groups are read-only. <code>Read Door Group Topology</code> returns the complete hierarchy documented by Access.</p>
1160
1160
  <p><code>Recent System Log</code> lists entries from the previous 24 hours, up to the first 200 results. It is read-only and requires the token's <code>view:system_log</code> permission.</p>
1161
1161
  <p><b>Emit periodically</b> repeats read-only actions at the selected interval and sends each result to the flow. It is hidden for <code>Receive Events</code> and write actions.</p>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "1.1.13",
3
+ "version": "1.1.14",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",
@@ -82,4 +82,4 @@
82
82
  "bugs": {
83
83
  "url": "https://github.com/Supergiovane/node-red-contrib-unifi-ultimate/issues"
84
84
  }
85
- }
85
+ }