node-red-contrib-unifi-ultimate 1.1.21 → 1.1.24

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.
@@ -27,6 +27,8 @@ const { LICENSE_PLATE_EVENT_TYPES, extractLicensePlates, isLicensePlateEvent } =
27
27
  const { readAlarmState, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError } = require("./utils/unifi-protect-alarm");
28
28
  const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
29
29
  const LPR_FOLLOW_UP_DELAYS_MS = [150, 350, 750, 1500, 3000];
30
+ const LPR_OPEN_EVENT_POLL_MS = 1000;
31
+ const LPR_OPEN_EVENT_MAX_MS = 60 * 1000;
30
32
 
31
33
  function resolveDeviceType(configuredDeviceType) {
32
34
  return String(configuredDeviceType || "").trim();
@@ -603,8 +605,17 @@ module.exports = function(RED) {
603
605
  }
604
606
 
605
607
  function scheduleLicensePlateFollowUp(state) {
606
- if (!isLicensePlateStateActive(state) || state.timer || state.followUpIndex >= LPR_FOLLOW_UP_DELAYS_MS.length) return;
607
- const delay = LPR_FOLLOW_UP_DELAYS_MS[state.followUpIndex++];
608
+ if (!isLicensePlateStateActive(state) || state.timer) return;
609
+ let delay;
610
+ if (state.followUpIndex < LPR_FOLLOW_UP_DELAYS_MS.length) {
611
+ delay = LPR_FOLLOW_UP_DELAYS_MS[state.followUpIndex++];
612
+ } else if (!state.emitted.size && !state.latestEvent.end && Date.now() < state.pollUntil) {
613
+ // Protect can read the plate well after the vehicle is detected.
614
+ // Keep looking while the event is open instead of waiting for its end.
615
+ delay = LPR_OPEN_EVENT_POLL_MS;
616
+ } else {
617
+ return;
618
+ }
608
619
  state.timer = setTimeout(() => {
609
620
  state.timer = null;
610
621
  if (!isLicensePlateStateActive(state)) return;
@@ -627,6 +638,7 @@ module.exports = function(RED) {
627
638
  // Do not keep retrying a credentials/permission failure.
628
639
  // A later real event can retry after configuration recovers.
629
640
  state.followUpIndex = LPR_FOLLOW_UP_DELAYS_MS.length;
641
+ state.pollUntil = 0;
630
642
  if (error.code === "UNIFI_PROTECT_HISTORY_CREDENTIALS_REQUIRED") {
631
643
  if (lprCredentialsWarningSent) continue;
632
644
  lprCredentialsWarningSent = true;
@@ -647,6 +659,7 @@ module.exports = function(RED) {
647
659
  }
648
660
  }).catch((error) => {
649
661
  state.followUpIndex = LPR_FOLLOW_UP_DELAYS_MS.length;
662
+ state.pollUntil = 0;
650
663
  if (isLicensePlateStateActive(state)) reportLicensePlateError(error);
651
664
  }).finally(() => {
652
665
  state.inFlight = null;
@@ -663,7 +676,8 @@ module.exports = function(RED) {
663
676
  // classification later. Output still requires actual OCR metadata.
664
677
  const vehicleCandidate = LICENSE_PLATE_EVENT_TYPES.includes(item.type)
665
678
  && Array.isArray(item.smartDetectTypes) && item.smartDetectTypes.includes("vehicle");
666
- if ((!isLicensePlateEvent(item) && !vehicleCandidate) || !item.id) return;
679
+ // Later updates of a tracked event may carry only the changed fields.
680
+ if (!item.id || (!isLicensePlateEvent(item) && !vehicleCandidate && !lprEventStates.has(item.id))) return;
667
681
  const now = Date.now();
668
682
  lprEventStates.forEach((state, id) => {
669
683
  if (now - state.lastSeen > 60 * 60 * 1000) forgetLicensePlateEvent(id);
@@ -672,7 +686,7 @@ module.exports = function(RED) {
672
686
  if (!state) {
673
687
  if (lprEventStates.size >= 256) forgetLicensePlateEvent(lprEventStates.keys().next().value);
674
688
  state = { emitted: new Map(), pending: null, inFlight: null, timer: null, lastSeen: now,
675
- liveVersion: 0, followUpIndex: 0, latestEvent: item };
689
+ liveVersion: 0, followUpIndex: 0, pollUntil: now + LPR_OPEN_EVENT_MAX_MS, latestEvent: item };
676
690
  lprEventStates.set(item.id, state);
677
691
  }
678
692
  state.lastSeen = now;
@@ -30,7 +30,12 @@ function extractLicensePlates(event) {
30
30
  thumbnails.forEach((thumbnail) => {
31
31
  if (!thumbnail || typeof thumbnail !== "object") return;
32
32
  if (thumbnail.type === "licensePlate" || (thumbnail.type === "vehicle" && hasPlateType)) {
33
- addPlate(thumbnail.name);
33
+ // `name` is the OCR of a single frame and is often truncated or
34
+ // misread ("4II0"); `matchedName` is the plate Protect settles on
35
+ // across the whole track and shows in its own UI.
36
+ const attributes = thumbnail.attributes && typeof thumbnail.attributes === "object" ? thumbnail.attributes : {};
37
+ const matchedName = typeof attributes.matchedName === "string" ? attributes.matchedName.trim() : "";
38
+ addPlate(matchedName || thumbnail.name);
34
39
  }
35
40
  });
36
41
  return Array.from(plates.values());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "1.1.21",
3
+ "version": "1.1.24",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",