node-red-contrib-unifi-ultimate 1.0.7 → 1.0.9

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
@@ -17,7 +17,7 @@ Control and monitor your **UniFi Network**, **UniFi Protect**, and **UniFi Acces
17
17
 
18
18
  [View Changelog](CHANGELOG.md)
19
19
 
20
- <a href="https://www.youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ">
20
+ <a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv">
21
21
  <img src="resources/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" align="absmiddle">
22
22
  Watch news and tutorials on YouTube
23
23
  <br/>
@@ -50,7 +50,7 @@ In Node-RED:
50
50
  7. Send any message into the node to run the action.
51
51
 
52
52
  > **Tip:** For most actions, the incoming message is just a trigger — the node uses the device and action you configured in the editor.
53
- > The only exception is **Control POE** with the *POE controlled by incoming message* action: send `true` to turn PoE on and `false` to turn it off.
53
+ > The only exception is **Clients Control** with a _controlled by msg.payload_ action: send `true` to enable (PoE or the port) and `false` to disable it, on every target in its list.
54
54
 
55
55
  > **Custom port:** Each config node has an optional **Port** field. Leave it empty to use the default (`443` for Network/Protect, `12445` for Access). Set it when your controller listens on a custom port — for example a **UniFi OS server** that uses `11443`. A port written directly in the controller address (`host:port`) takes precedence over this field.
56
56
 
@@ -81,8 +81,9 @@ Things you can do:
81
81
  - Read CPU usage, memory, and uptime from a switch or access point.
82
82
  - Read the temperature of a switch (where supported).
83
83
  - Restart a UniFi device.
84
- - Power-cycle or turn on/off a PoE port.
85
- - Let the PoE node automatically find which port a client is connected to.
84
+ - Control a **list of clients/ports** at once with the **Clients Control** node: pick targets by switch+port or by client (its uplink switch/port is resolved automatically) and apply one action to all of them, getting a single summary message back.
85
+ - Switch PoE on/off or power-cycle it, and **enable/disable the whole switch port** (the UniFi _Port State_ toggle), driven by the editor or by `msg.payload`.
86
+ - Let **Clients Control** automatically find which switch port a client is connected to.
86
87
  - Restart a whole list of switches/APs at once, or power-cycle all their active PoE ports, with the **Restart** node.
87
88
 
88
89
  <br/>
@@ -139,26 +140,26 @@ Things you can do:
139
140
 
140
141
  Every node has **two outputs**:
141
142
 
142
- | Output | When it fires |
143
- | ------ | ------------- |
143
+ | Output | When it fires |
144
+ | -------------- | ---------------------------------------------------------------------------- |
144
145
  | **1 — result** | The action completed successfully. The result is available in `msg.payload`. |
145
- | **2 — error** | Something went wrong (connection problem, timeout, unsupported action). |
146
+ | **2 — error** | Something went wrong (connection problem, timeout, unsupported action). |
146
147
 
147
148
  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.
148
149
 
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.
150
+ **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.
150
151
 
151
152
  ## Example Flows
152
153
 
153
154
  Import from `examples/`:
154
155
 
155
- | Flow file | What it demonstrates |
156
- | --------- | -------------------- |
157
- | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read the state of a Protect camera |
158
- | [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json) | Receive sensor events (motion, temperature, humidity, …) |
159
- | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Take snapshots, move PTZ, show doorbell messages |
160
- | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, remote unlock, temporary lock rule |
161
- | [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json) | Intercom — receive ring, trigger and cancel doorbell |
156
+ | Flow file | What it demonstrates |
157
+ | -------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
158
+ | [examples/unifi-protect-info.json](examples/unifi-protect-info.json) | Read the state of a Protect camera |
159
+ | [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json) | Receive sensor events (motion, temperature, humidity, …) |
160
+ | [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json) | Take snapshots, move PTZ, show doorbell messages |
161
+ | [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json) | Door state, remote unlock, temporary lock rule |
162
+ | [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json) | Intercom — receive ring, trigger and cancel doorbell |
162
163
 
163
164
  ## Notes
164
165
 
@@ -178,4 +179,4 @@ Import from `examples/`:
178
179
  [license-image]: https://img.shields.io/badge/license-MIT-green.svg
179
180
  [license-url]: https://opensource.org/licenses/MIT
180
181
  [youtube-image]: https://img.shields.io/badge/YouTube-Subscribe-red?logo=youtube&logoColor=white
181
- [youtube-url]: https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ
182
+ [youtube-url]: https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv
@@ -1,4 +1,5 @@
1
- <link rel="stylesheet" type="text/css" href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
1
+ <link rel="stylesheet" type="text/css"
2
+ href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
2
3
 
3
4
  <script type="text/javascript">
4
5
  RED.nodes.registerType("unifi-access-config", {
@@ -74,7 +75,7 @@
74
75
  <img class="unifi-ultimate-product-logo" src="resources/node-red-contrib-unifi-ultimate/logos/unifi-access-black.svg" alt="UniFi Access logo" onerror="this.closest('.unifi-ultimate-product-logo-row').style.display='none';">
75
76
  </div>
76
77
  <div class="form-row unifi-ultimate-hero">
77
- <a class="unifi-ultimate-youtube-link" href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer">
78
+ <a class="unifi-ultimate-youtube-link" href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer">
78
79
  <img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18">
79
80
  <span>Youtube Tutorials</span>
80
81
  </a>
@@ -105,7 +106,7 @@
105
106
  </script>
106
107
 
107
108
  <script type="text/html" data-help-name="unifi-access-config">
108
- <p><a href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
109
+ <p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
109
110
 
110
111
  <p>Shared connection for UniFi Access nodes.</p>
111
112
 
@@ -119,4 +120,4 @@
119
120
  <h3>Usage</h3>
120
121
  <p>This config node is used by UniFi Access Device nodes for API requests and live notification events.</p>
121
122
  <p>All Access API calls and notification channels are managed by this config node. Runtime nodes subscribe through <code>addClient/removeClient</code> and receive fan-out updates.</p>
122
- </script>
123
+ </script>
@@ -1,4 +1,5 @@
1
- <link rel="stylesheet" type="text/css" href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
1
+ <link rel="stylesheet" type="text/css"
2
+ href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
2
3
 
3
4
 
4
5
  <script type="text/javascript">
@@ -1079,7 +1080,7 @@
1079
1080
  <img class="unifi-ultimate-product-logo" src="resources/node-red-contrib-unifi-ultimate/logos/unifi-access-black.svg" alt="UniFi Access logo" onerror="this.closest('.unifi-ultimate-product-logo-row').style.display='none';">
1080
1081
  </div>
1081
1082
  <div class="form-row unifi-ultimate-hero">
1082
- <a class="unifi-ultimate-youtube-link" href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer">
1083
+ <a class="unifi-ultimate-youtube-link" href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer">
1083
1084
  <img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18">
1084
1085
  <span>Youtube Tutorials</span>
1085
1086
  </a>
@@ -1128,7 +1129,7 @@
1128
1129
  </script>
1129
1130
 
1130
1131
  <script type="text/html" data-help-name="unifi-access-device">
1131
- <p><a href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
1132
+ <p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
1132
1133
 
1133
1134
  <p>Use this node to execute a configured UniFi Access action or listen for live Access events.</p>
1134
1135
 
@@ -1201,4 +1202,4 @@
1201
1202
  "payload": null,
1202
1203
  "error": { "message": "Request timed out" }
1203
1204
  }</code></pre>
1204
- </script>
1205
+ </script>
@@ -1,4 +1,5 @@
1
- <link rel="stylesheet" type="text/css" href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
1
+ <link rel="stylesheet" type="text/css"
2
+ href="resources/node-red-contrib-unifi-ultimate/editor/unifi-ultimate-editor-common.css">
2
3
 
3
4
 
4
5
  <script type="text/javascript">
@@ -87,7 +88,7 @@
87
88
  <img class="unifi-ultimate-product-logo" src="resources/node-red-contrib-unifi-ultimate/logos/unifi-network-black.svg" alt="UniFi Network logo" onerror="this.closest('.unifi-ultimate-product-logo-row').style.display='none';">
88
89
  </div>
89
90
  <div class="form-row unifi-ultimate-hero">
90
- <a class="unifi-ultimate-youtube-link" href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer">
91
+ <a class="unifi-ultimate-youtube-link" href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer">
91
92
  <img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18">
92
93
  <span>Youtube Tutorials</span>
93
94
  </a>
@@ -122,7 +123,7 @@
122
123
  </script>
123
124
 
124
125
  <script type="text/html" data-help-name="unifi-network-config">
125
- <p><a href="https://www.youtube.com/watch?v=ZOq7M5mgUBk&list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
126
+ <p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
126
127
 
127
128
  <p>Shared connection for UniFi Network nodes.</p>
128
129
 
@@ -139,4 +140,4 @@
139
140
  <p>All UniFi Network requests are executed by this config node. Runtime nodes subscribe through <code>addClient/removeClient</code> and receive updates/events from here.</p>
140
141
  <p>Event streams and observation timers are centralized here (shared scheduler per config node), so runtime nodes do not open their own websocket channels or polling timers.</p>
141
142
  <p>The node uses the official Network integration API where available and falls back automatically to local legacy endpoints when the official <code>/v1</code> routes are not exposed (for example on older local releases). For switch port/client mapping and PoE enable/disable, legacy endpoints are also used where needed.</p>
142
- </script>
143
+ </script>
@@ -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;