node-red-contrib-unifi-ultimate 1.0.6 → 1.0.8

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
@@ -75,6 +75,7 @@ Use **Network** nodes to work with:
75
75
  Things you can do:
76
76
 
77
77
  - Check whether a device or phone is connected to your network.
78
+ - Re-send the current presence state at a fixed interval with the Presence node's **Resend** field (e.g. to keep a dashboard or home-automation system in sync).
78
79
  - Count how many clients are currently online.
79
80
  - Create guest Wi-Fi vouchers.
80
81
  - Read CPU usage, memory, and uptime from a switch or access point.
@@ -145,7 +146,7 @@ Every node has **two outputs**:
145
146
 
146
147
  When an error occurs, the node status turns red and the error message comes out of the second output. Connect it to a **debug** node to see what happened, or wire it to any notification logic in your flow.
147
148
 
148
- **Repeat periodically** — for read actions, you can tick *Emit periodically* in the node editor to have the node send the result automatically at a fixed interval, without needing an Inject node.
149
+ **Repeat periodically** — for read actions, you can tick *Emit periodically* in the node editor to have the node send the result automatically at a fixed interval, without needing an Inject node. The **Presence Detection** node offers a similar **Resend (s)** field: set it above `0` to re-emit the last known presence value on that cadence (even when unchanged). Resent messages carry `msg.eventName = "repeat"` so you can tell them apart from real state changes; leave it at `0` to disable.
149
150
 
150
151
  ## Example Flows
151
152
 
@@ -2753,23 +2753,23 @@ module.exports = function(RED) {
2753
2753
  };
2754
2754
 
2755
2755
  node.fetchClientsWithAttachment = async () => {
2756
- let clients = [];
2757
- let devices = [];
2758
- try {
2759
- clients = await node.fetchDevices("client");
2760
- } catch (error) {
2761
- clients = [];
2762
- }
2756
+ // Build the picker list from the two authoritative legacy sources:
2757
+ // - stat/device -> each UniFi device's own uplink (parent switch + port)
2758
+ // - stat/sta -> each wired client's sw_mac + sw_port
2759
+ // The parent switch identity/name comes from the official device list,
2760
+ // so both clients and UniFi devices (APs, LTE failover, downstream
2761
+ // switches) are listed by name with the correct parent port.
2762
+ let officialDevices = [];
2763
2763
  try {
2764
- devices = await node.fetchDevices("device");
2764
+ officialDevices = await node.fetchDevices("device");
2765
2765
  } catch (error) {
2766
- devices = [];
2766
+ officialDevices = [];
2767
2767
  }
2768
2768
 
2769
- const deviceNameByScopedId = new Map();
2770
2769
  const deviceNameByKey = new Map();
2771
2770
  const deviceScopedIdByKey = new Map();
2772
- devices.forEach((entry) => {
2771
+ const siteIds = new Set();
2772
+ officialDevices.forEach((entry) => {
2773
2773
  const item = entry && typeof entry === "object" && !Array.isArray(entry)
2774
2774
  ? entry
2775
2775
  : null;
@@ -2782,13 +2782,14 @@ module.exports = function(RED) {
2782
2782
  if (!scopedId) {
2783
2783
  return;
2784
2784
  }
2785
+ if (siteId) {
2786
+ siteIds.add(siteId);
2787
+ }
2785
2788
  const summary = summarizeDevice("device", item);
2786
2789
  const deviceName = normalizeString(summary.name || resourceId || scopedId);
2787
- deviceNameByScopedId.set(scopedId, deviceName);
2788
2790
  [
2789
2791
  item.id,
2790
2792
  item.deviceId,
2791
- item.device_id,
2792
2793
  item.macAddress,
2793
2794
  item.mac_address,
2794
2795
  item.mac,
@@ -2807,215 +2808,105 @@ module.exports = function(RED) {
2807
2808
  });
2808
2809
  });
2809
2810
 
2810
- function resolveAttachedDeviceId(attachment) {
2811
- const rawKey = normalizeString(attachment && attachment.resourceId).toLowerCase();
2812
- const compactKey = normalizeIdentifierKey(attachment && attachment.resourceId);
2813
- return deviceScopedIdByKey.get(rawKey)
2811
+ function resolveParentDevice(parentMac, fallbackName) {
2812
+ const rawKey = normalizeString(parentMac).toLowerCase();
2813
+ const compactKey = normalizeIdentifierKey(parentMac);
2814
+ const scopedId = deviceScopedIdByKey.get(rawKey)
2814
2815
  || deviceScopedIdByKey.get(compactKey)
2815
- || encodeScopedDeviceId(attachment && attachment.siteId, attachment && attachment.resourceId);
2816
- }
2817
-
2818
- function resolveAttachedDeviceName(attachment, attachedDeviceId) {
2819
- const rawKey = normalizeString(attachment && attachment.resourceId).toLowerCase();
2820
- const compactKey = normalizeIdentifierKey(attachment && attachment.resourceId);
2821
- return firstNonEmptyString([
2822
- deviceNameByScopedId.get(attachedDeviceId),
2816
+ || "";
2817
+ const name = firstNonEmptyString([
2823
2818
  deviceNameByKey.get(rawKey),
2824
2819
  deviceNameByKey.get(compactKey),
2825
- attachment && attachment.resourceId
2820
+ fallbackName,
2821
+ parentMac
2826
2822
  ]);
2823
+ return { scopedId, name };
2827
2824
  }
2828
2825
 
2829
- const attachedClients = clients
2830
- .map((entry) => {
2831
- const item = entry && typeof entry === "object" && !Array.isArray(entry)
2832
- ? entry
2833
- : null;
2834
- if (!item) {
2835
- return null;
2836
- }
2837
-
2838
- const fallbackClientId = firstNonEmptyString([
2839
- item.id,
2840
- item.clientId,
2841
- item.deviceId,
2842
- item.macAddress
2843
- ]);
2844
- const attachment = resolveClientUplinkAttachment(item, fallbackClientId);
2845
- if (!attachment) {
2846
- return null;
2847
- }
2848
-
2849
- const clientSummary = summarizeDevice("client", item);
2850
- const attachedDeviceId = resolveAttachedDeviceId(attachment);
2851
- if (!attachedDeviceId) {
2852
- return null;
2853
- }
2854
-
2855
- const attachedDeviceName = resolveAttachedDeviceName(attachment, attachedDeviceId);
2856
-
2857
- return {
2858
- ...clientSummary,
2859
- attachment: {
2860
- deviceId: attachedDeviceId,
2861
- portIdx: attachment.portIdx,
2862
- deviceName: attachedDeviceName
2863
- }
2864
- };
2865
- })
2866
- .filter(Boolean);
2867
-
2868
- const siteIds = new Set();
2869
- clients.forEach((client) => {
2870
- const siteId = normalizeString(client && (client.siteId || client.site_id));
2871
- if (siteId) {
2872
- siteIds.add(siteId);
2873
- }
2874
- });
2875
- devices.forEach((device) => {
2876
- const siteId = normalizeString(device && (device.siteId || device.site_id));
2877
- if (siteId) {
2878
- siteIds.add(siteId);
2879
- }
2880
- });
2881
-
2882
- const legacyClients = [];
2883
- const legacyDevices = [];
2884
- await Promise.all(Array.from(siteIds).map(async (siteId) => {
2885
- try {
2886
- const [siteClients, siteDevices] = await Promise.all([
2887
- fetchLegacyKnownClients(siteId),
2888
- fetchLegacyDevices(siteId)
2889
- ]);
2890
- legacyClients.push(...siteClients);
2891
- legacyDevices.push(...siteDevices);
2892
- } catch (error) {
2826
+ const attachedClients = [];
2827
+ const seenKeys = new Set();
2828
+ function addEndpoint(endpoint, fallbackParentName) {
2829
+ const normalizedPort = parsePortIndex(endpoint && endpoint.portIdx);
2830
+ const parentMac = normalizeString(endpoint && endpoint.parentMac);
2831
+ if (!Number.isInteger(normalizedPort) || !parentMac) {
2832
+ return;
2893
2833
  }
2894
- }));
2895
-
2896
- const legacyClientsByKey = new Map();
2897
- legacyClients.forEach((legacyClient) => {
2898
- [
2899
- legacyClient && legacyClient.macAddress,
2900
- legacyClient && legacyClient.mac,
2901
- legacyClient && legacyClient.id,
2902
- legacyClient && legacyClient.clientId
2903
- ].forEach((value) => {
2904
- const compactKey = normalizeIdentifierKey(value);
2905
- if (compactKey && !legacyClientsByKey.has(compactKey)) {
2906
- legacyClientsByKey.set(compactKey, legacyClient);
2907
- }
2908
- });
2909
- });
2910
-
2911
- const seenClientIds = new Set();
2912
- attachedClients.forEach((client) => {
2913
- [
2914
- client && client.id,
2915
- client && client.resourceId,
2916
- client && client.macAddress,
2917
- client && client.raw && client.raw.macAddress,
2918
- client && client.raw && client.raw.mac
2919
- ].forEach((value) => {
2920
- const rawKey = normalizeString(value);
2921
- const compactKey = normalizeIdentifierKey(value);
2922
- if (rawKey) {
2923
- seenClientIds.add(rawKey);
2924
- }
2925
- if (compactKey) {
2926
- seenClientIds.add(compactKey);
2927
- }
2928
- });
2929
- });
2930
-
2931
- function addLegacyAttachedClient(legacyClient, attachment) {
2932
- const attachedDeviceId = resolveAttachedDeviceId(attachment);
2933
- if (!attachedDeviceId) {
2834
+ const parent = resolveParentDevice(parentMac, fallbackParentName);
2835
+ if (!parent.scopedId) {
2836
+ // The port lives on a switch we do not know how to address.
2934
2837
  return;
2935
2838
  }
2936
-
2937
- const siteId = normalizeString(legacyClient && legacyClient.siteId || attachment && attachment.siteId);
2938
- const normalizedClient = buildLegacyClientSummary(legacyClient, siteId);
2939
- const clientSummary = summarizeDevice("client", normalizedClient);
2940
- const clientKeys = [
2941
- clientSummary.id,
2942
- clientSummary.resourceId,
2943
- normalizedClient.macAddress,
2944
- normalizedClient.mac,
2945
- normalizedClient.id
2946
- ].flatMap((value) => {
2947
- const rawKey = normalizeString(value);
2948
- const compactKey = normalizeIdentifierKey(value);
2949
- return [rawKey, compactKey].filter(Boolean);
2950
- });
2951
- if (clientKeys.some((key) => seenClientIds.has(key))) {
2839
+ const endpointId = firstNonEmptyString([endpoint && endpoint.id, endpoint && endpoint.name]);
2840
+ const dedupeKey = normalizeIdentifierKey(endpointId);
2841
+ if (!endpointId || !dedupeKey || seenKeys.has(dedupeKey)) {
2952
2842
  return;
2953
2843
  }
2954
- clientKeys.forEach((key) => seenClientIds.add(key));
2844
+ seenKeys.add(dedupeKey);
2955
2845
 
2846
+ const online = endpoint && endpoint.online === true;
2956
2847
  attachedClients.push({
2957
- ...clientSummary,
2848
+ id: endpointId,
2849
+ name: stripOfflineTag(firstNonEmptyString([endpoint && endpoint.name, endpointId])) || endpointId,
2850
+ isOnline: online,
2851
+ online,
2852
+ offline: !online,
2853
+ state: online ? "online" : "offline",
2958
2854
  attachment: {
2959
- deviceId: attachedDeviceId,
2960
- portIdx: attachment.portIdx,
2961
- deviceName: resolveAttachedDeviceName(attachment, attachedDeviceId)
2855
+ deviceId: parent.scopedId,
2856
+ portIdx: normalizedPort,
2857
+ deviceName: parent.name
2962
2858
  }
2963
2859
  });
2964
2860
  }
2965
2861
 
2966
- legacyClients.forEach((legacyClient) => {
2967
- const siteId = normalizeString(legacyClient && legacyClient.siteId);
2968
- const attachment = resolveLegacyClientAttachment(legacyClient, siteId);
2969
- if (!attachment) {
2970
- return;
2862
+ await Promise.all(Array.from(siteIds).map(async (siteId) => {
2863
+ let legacyDevices = [];
2864
+ let legacyStations = [];
2865
+ try {
2866
+ legacyDevices = await fetchLegacyDevices(siteId);
2867
+ } catch (error) {
2868
+ legacyDevices = [];
2869
+ }
2870
+ try {
2871
+ legacyStations = await fetchLegacySiteCollection(siteId, "/stat/sta");
2872
+ } catch (error) {
2873
+ legacyStations = [];
2971
2874
  }
2972
2875
 
2973
- addLegacyAttachedClient(legacyClient, attachment);
2974
- });
2975
-
2976
- legacyDevices.forEach((legacyDevice) => {
2977
- const siteId = normalizeString(legacyDevice && legacyDevice.siteId);
2978
- const deviceResourceId = firstNonEmptyString([
2979
- legacyDevice && legacyDevice.mac,
2980
- legacyDevice && legacyDevice.device_id,
2981
- legacyDevice && legacyDevice.id,
2982
- legacyDevice && legacyDevice._id
2983
- ]);
2984
- const ports = Array.isArray(legacyDevice && legacyDevice.port_table)
2985
- ? legacyDevice.port_table
2986
- : [];
2987
-
2988
- ports.forEach((port) => {
2989
- const lastConnection = port && port.last_connection && typeof port.last_connection === "object"
2990
- ? port.last_connection
2876
+ // UniFi devices addressed by their own uplink to the parent port.
2877
+ legacyDevices.forEach((device) => {
2878
+ const uplink = device && device.uplink && typeof device.uplink === "object" && !Array.isArray(device.uplink)
2879
+ ? device.uplink
2991
2880
  : null;
2992
- const clientMac = firstNonEmptyString([
2993
- lastConnection && lastConnection.mac,
2994
- lastConnection && lastConnection.macAddress,
2995
- lastConnection && lastConnection.mac_address
2996
- ]);
2997
- const portIdx = parsePortIndex(port && port.port_idx);
2998
- if (!siteId || !deviceResourceId || !clientMac || !Number.isInteger(portIdx)) {
2881
+ if (!uplink) {
2999
2882
  return;
3000
2883
  }
2884
+ addEndpoint({
2885
+ id: firstNonEmptyString([device.mac, device._id, device.device_id, device.id]),
2886
+ name: firstNonEmptyString([device.name, device.hostname, device.mac]),
2887
+ online: device.state === 1 || device.state === "1",
2888
+ parentMac: uplink.uplink_mac,
2889
+ portIdx: uplink.uplink_remote_port
2890
+ }, uplink.uplink_device_name);
2891
+ });
3001
2892
 
3002
- const clientKey = normalizeIdentifierKey(clientMac);
3003
- const knownClient = legacyClientsByKey.get(clientKey) || {};
3004
- const legacyClient = {
3005
- ...knownClient,
3006
- mac: knownClient.mac || knownClient.macAddress || clientMac,
3007
- ip: knownClient.ip || knownClient.ipAddress || lastConnection.ip,
3008
- ipAddress: knownClient.ipAddress || knownClient.ip || lastConnection.ip,
3009
- __unifiUltimateOnline: lastConnection.connected === true,
3010
- siteId
3011
- };
3012
- const attachment = {
3013
- siteId,
3014
- resourceId: deviceResourceId,
3015
- portIdx
3016
- };
3017
- addLegacyAttachedClient(legacyClient, attachment);
2893
+ // Wired clients addressed by their switch uplink (sw_mac/sw_port).
2894
+ legacyStations.forEach((station) => {
2895
+ if (!station || station.is_wired === false) {
2896
+ return;
2897
+ }
2898
+ addEndpoint({
2899
+ id: firstNonEmptyString([station.mac, station._id, station.user_id]),
2900
+ name: firstNonEmptyString([station.name, station.hostname, station.mac]),
2901
+ online: true,
2902
+ parentMac: station.sw_mac,
2903
+ portIdx: station.sw_port
2904
+ });
3018
2905
  });
2906
+ }));
2907
+
2908
+ attachedClients.sort((a, b) => {
2909
+ return String(a.name || a.id).localeCompare(String(b.name || b.id));
3019
2910
  });
3020
2911
 
3021
2912
  return attachedClients;