node-red-contrib-unifi-ultimate 0.1.0-beta.5 → 0.1.0
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 +139 -52
- package/nodes/unifi-access-config.html +85 -13
- package/nodes/unifi-access-config.js +58 -10
- package/nodes/unifi-access-device.html +246 -26
- package/nodes/unifi-access-device.js +29 -3
- package/nodes/unifi-network-config.html +118 -0
- package/nodes/unifi-network-config.js +482 -0
- package/nodes/unifi-network-control-poe.html +399 -0
- package/nodes/unifi-network-control-poe.js +191 -0
- package/nodes/unifi-network-device.html +954 -0
- package/nodes/unifi-network-device.js +275 -0
- package/nodes/unifi-network-presence.html +342 -0
- package/nodes/unifi-network-presence.js +328 -0
- package/nodes/unifi-protect-config.html +89 -15
- package/nodes/unifi-protect-config.js +50 -10
- package/nodes/unifi-protect-device.html +246 -25
- package/nodes/unifi-protect-device.js +34 -5
- package/nodes/utils/unifi-access-device-registry.js +19 -0
- package/nodes/utils/unifi-access-utils.js +10 -0
- package/nodes/utils/unifi-network-device-registry.js +539 -0
- package/nodes/utils/unifi-network-utils.js +187 -0
- package/nodes/utils/unifi-protect-device-registry.js +34 -0
- package/nodes/utils/unifi-protect-field-metadata.js +7 -0
- package/nodes/utils/unifi-protect-utils.js +7 -0
- package/package.json +8 -3
- package/nodes/icons/unifi-ultimate-logo.svg.png +0 -0
package/README.md
CHANGED
|
@@ -2,36 +2,121 @@
|
|
|
2
2
|
<img src="nodes/icons/unifi-ultimate-logo.svg" alt="Unifi Ultimate" width="720">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# node-red-contrib-unifi-ultimate
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Use UniFi Protect, UniFi Access, and UniFi Network inside Node-RED without building custom API calls.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
- `Unifi Protect Config`: shared configuration node for controller IP, API key header, API key, and TLS behavior
|
|
11
|
-
- `Unifi Access Device`: first generic Access node focused on doors, Access devices, notification events, and safe doorbell control
|
|
12
|
-
- `Unifi Access Config`: shared configuration node for host, bearer token, and TLS behavior
|
|
9
|
+
This package lets you:
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
- monitor cameras, sensors, lights, doors, Access devices, sites, clients, and UniFi devices
|
|
12
|
+
- react to live events inside your flows
|
|
13
|
+
- trigger common Protect, Access, and Network actions from a single device node
|
|
14
|
+
- start from ready-to-import example flows
|
|
15
|
+
|
|
16
|
+
Current release status: `0.1.0`
|
|
17
|
+
|
|
18
|
+
[View Changelog](CHANGELOG.md)
|
|
19
|
+
|
|
20
|
+
## Before you start
|
|
21
|
+
|
|
22
|
+
- Node-RED `3.1.1` or newer
|
|
23
|
+
- Node.js `18` or newer
|
|
24
|
+
- For UniFi Protect: controller IP or host, API key header, and API key
|
|
25
|
+
- For UniFi Access: host and bearer token
|
|
26
|
+
- For UniFi Network: controller host and API key
|
|
27
|
+
- If your UniFi controller uses a self-signed certificate, each config node lets you adjust TLS behavior
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
In Node-RED:
|
|
32
|
+
|
|
33
|
+
1. Open `Manage palette`
|
|
34
|
+
2. Select `Install`
|
|
35
|
+
3. Search for `node-red-contrib-unifi-ultimate`
|
|
36
|
+
4. Install the package
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
1. Add the right config node for your UniFi product:
|
|
41
|
+
`Unifi Protect Config`, `Unifi Access Config`, or `Unifi Network Config`
|
|
42
|
+
2. Enter your connection details and credentials
|
|
43
|
+
3. Add a `Device` node
|
|
44
|
+
4. Select the device type, the specific UniFi device, and the capability you want
|
|
45
|
+
5. Deploy
|
|
46
|
+
6. Connect:
|
|
47
|
+
Output 1 for state or action results
|
|
48
|
+
Output 2 for live events when you use `Receive Events`
|
|
49
|
+
|
|
50
|
+
If you want the fastest path, import one of the included example flows and replace the credentials with your own.
|
|
51
|
+
|
|
52
|
+
## Available nodes
|
|
53
|
+
|
|
54
|
+
- `Unifi Protect Config`: stores the connection settings for UniFi Protect
|
|
55
|
+
- `Unifi Protect Device`: select a Protect device, read its state, listen for events, or trigger actions
|
|
56
|
+
- `Unifi Access Config`: stores the connection settings for UniFi Access
|
|
57
|
+
- `Unifi Access Device`: select a door or Access device, read its state, listen for events, or trigger actions
|
|
58
|
+
- `Unifi Network Config`: stores the connection settings for UniFi Network
|
|
59
|
+
- `Unifi Network Device`: select a site, client, or UniFi device and run supported Network actions
|
|
60
|
+
- `Unifi Network Presence`: emits `true/false` presence for a selected client with disconnect hysteresis
|
|
61
|
+
- `Unifi Network Control POE`: controls PoE state on a selected switch port
|
|
62
|
+
|
|
63
|
+
## UniFi Protect
|
|
64
|
+
|
|
65
|
+
Supported device types:
|
|
66
|
+
|
|
67
|
+
- `Camera`
|
|
68
|
+
- `Sensor`
|
|
69
|
+
- `Light`
|
|
70
|
+
- `Chime`
|
|
71
|
+
- `Viewer`
|
|
72
|
+
- `Live View`
|
|
73
|
+
- `NVR`
|
|
74
|
+
|
|
75
|
+
Common capabilities:
|
|
76
|
+
|
|
77
|
+
- `Receive Events`
|
|
78
|
+
- `Read Device State`
|
|
79
|
+
- `Send Raw Update`
|
|
80
|
+
|
|
81
|
+
Additional capabilities available when supported by the selected device:
|
|
82
|
+
|
|
83
|
+
- `Take Snapshot`
|
|
84
|
+
- `List RTSPS Streams`
|
|
85
|
+
- `Create RTSPS Stream`
|
|
86
|
+
- `Delete RTSPS Stream`
|
|
87
|
+
- `Start Talkback Session`
|
|
88
|
+
- `Disable Microphone Permanently`
|
|
89
|
+
- `Start PTZ Patrol`
|
|
90
|
+
- `Stop PTZ Patrol`
|
|
91
|
+
- `Recall PTZ Preset`
|
|
92
|
+
- `Show Doorbell Message`
|
|
93
|
+
- `Switch Live View`
|
|
94
|
+
- `Update One Property`
|
|
15
95
|
|
|
16
|
-
|
|
17
|
-
- [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json): Protect sensor boolean observe example
|
|
18
|
-
- [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json): Protect snapshot, PTZ preset, and doorbell message examples
|
|
19
|
-
- [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json): Access door observe, unlock, and temporary lock rule examples
|
|
20
|
-
- [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json): Access intercom observe, trigger doorbell, and safe cancel example
|
|
96
|
+
The editor adapts to the selected device and capability. For example, it can offer:
|
|
21
97
|
|
|
22
|
-
|
|
98
|
+
- sensor observables such as contact, motion, alarm, water leak, tamper, or battery low
|
|
99
|
+
- camera observables such as ring, motion, and smart detections
|
|
100
|
+
- light observables such as motion or light on/off
|
|
101
|
+
- PTZ preset choices for supported cameras
|
|
102
|
+
- live view choices for supported viewers
|
|
103
|
+
- patchable properties discovered from the selected device
|
|
104
|
+
|
|
105
|
+
For supported boolean event observables, the main payload is normalized to `true` or `false`, while the original event details remain available in the message.
|
|
106
|
+
|
|
107
|
+
## UniFi Access
|
|
108
|
+
|
|
109
|
+
Supported device types:
|
|
23
110
|
|
|
24
111
|
- `Door`
|
|
25
112
|
- `Access Device`
|
|
26
113
|
|
|
27
|
-
## Supported Access capabilities
|
|
28
|
-
|
|
29
114
|
Common capabilities:
|
|
30
115
|
|
|
31
116
|
- `Receive Events`
|
|
32
117
|
- `Read State`
|
|
33
118
|
|
|
34
|
-
Door
|
|
119
|
+
Door capabilities:
|
|
35
120
|
|
|
36
121
|
- `Unlock Door`
|
|
37
122
|
- `Read Lock Rule`
|
|
@@ -39,57 +124,59 @@ Door-specific capabilities:
|
|
|
39
124
|
- `Read Emergency Mode`
|
|
40
125
|
- `Set Emergency Mode`
|
|
41
126
|
|
|
42
|
-
Access device
|
|
127
|
+
Access device capabilities:
|
|
43
128
|
|
|
44
129
|
- `Read Access Methods`
|
|
45
130
|
- `Update Access Methods`
|
|
46
131
|
- `Trigger Doorbell`
|
|
47
132
|
- `Cancel Doorbell`
|
|
48
133
|
|
|
49
|
-
|
|
134
|
+
When you use `Receive Events`, the node first fetches the current state and then keeps listening for matching UniFi Access notifications.
|
|
50
135
|
|
|
51
|
-
|
|
136
|
+
## UniFi Network
|
|
52
137
|
|
|
53
|
-
|
|
54
|
-
- `msg.deviceId`
|
|
55
|
-
- `msg.capability`
|
|
56
|
-
- `msg.capabilityConfig`
|
|
57
|
-
- `msg.params`
|
|
58
|
-
- `msg.query`
|
|
59
|
-
- `msg.headers`
|
|
60
|
-
- `msg.payload`
|
|
138
|
+
Supported resource types:
|
|
61
139
|
|
|
62
|
-
|
|
140
|
+
- `Site`
|
|
141
|
+
- `UniFi Device`
|
|
142
|
+
- `Client`
|
|
63
143
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
- `
|
|
67
|
-
- `
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
- `Switch Live View` can offer available live views for the selected viewer
|
|
71
|
-
- `Update One Property` discovers patchable properties from the selected device payload and adapts the value field to booleans, numbers, strings, or related object selections
|
|
144
|
+
Common capabilities:
|
|
145
|
+
|
|
146
|
+
- `Read State`
|
|
147
|
+
- `Read Details`
|
|
148
|
+
|
|
149
|
+
Additional capabilities:
|
|
72
150
|
|
|
73
|
-
|
|
151
|
+
- Site: `List Site Devices`, `List Site Clients`
|
|
152
|
+
- UniFi Device: `Read Latest Statistics`, `Restart Device`, `Power Cycle Port`
|
|
153
|
+
- Client: `Authorize Guest Access`, `Revoke Guest Access`
|
|
74
154
|
|
|
75
|
-
For
|
|
155
|
+
For device and client resources, the editor automatically includes the site context behind the scenes and still keeps selection simple for the end user.
|
|
76
156
|
|
|
77
|
-
|
|
78
|
-
- the full original device/event context is exposed in `msg.RAW`
|
|
157
|
+
Dedicated utility nodes:
|
|
79
158
|
|
|
80
|
-
|
|
159
|
+
- `Unifi Network Presence`: checks one selected client and emits `msg.payload = true` when connected, `false` when disconnected, with disconnect hysteresis to avoid flapping
|
|
160
|
+
- `Unifi Network Control POE`: enables/disables PoE (and supports power cycle) on a selected switch port
|
|
161
|
+
|
|
162
|
+
## Outputs
|
|
163
|
+
|
|
164
|
+
- Output 1 returns the current state or the main action response
|
|
165
|
+
- Output 2 returns live events when the selected capability is `Receive Events` (Protect and Access nodes)
|
|
166
|
+
- The message also includes product-specific metadata and the last known device data when available
|
|
167
|
+
|
|
168
|
+
## Example flows
|
|
81
169
|
|
|
82
|
-
|
|
170
|
+
Import one of these flows from the `examples` folder:
|
|
83
171
|
|
|
84
|
-
-
|
|
85
|
-
-
|
|
86
|
-
-
|
|
87
|
-
-
|
|
172
|
+
- [examples/unifi-protect-info.json](examples/unifi-protect-info.json): read the state of a Protect camera
|
|
173
|
+
- [examples/unifi-protect-sensor-observe.json](examples/unifi-protect-sensor-observe.json): observe a Protect sensor as a boolean event stream
|
|
174
|
+
- [examples/unifi-protect-camera-actions.json](examples/unifi-protect-camera-actions.json): take snapshots, use PTZ presets, and show doorbell messages
|
|
175
|
+
- [examples/unifi-access-door-control.json](examples/unifi-access-door-control.json): observe a door, unlock it, and manage a temporary lock rule
|
|
176
|
+
- [examples/unifi-access-intercom-doorbell.json](examples/unifi-access-intercom-doorbell.json): observe an intercom device, trigger a doorbell, and cancel it safely
|
|
88
177
|
|
|
89
|
-
|
|
178
|
+
## Beta notes
|
|
90
179
|
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
94
|
-
- `msg.unifiAccess` contains standardized metadata for Access nodes
|
|
95
|
-
- `Cancel Doorbell` returns `msg.payload.skipped = true` when the shared Access config node is not currently tracking any active ring
|
|
180
|
+
- UniFi Protect is the main supported surface today
|
|
181
|
+
- UniFi Access is included in the first stable release and may still evolve quickly across upcoming releases
|
|
182
|
+
- UniFi Network is included in the first stable release and currently focuses on read-first flows plus selected action utilities
|
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
<style>
|
|
2
|
+
.unifi-ultimate-card {
|
|
3
|
+
margin-left: 150px;
|
|
4
|
+
padding: 12px 14px;
|
|
5
|
+
border-radius: 12px;
|
|
6
|
+
background: #f5f7fa;
|
|
7
|
+
color: #334155;
|
|
8
|
+
line-height: 1.45;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.unifi-ultimate-card strong {
|
|
12
|
+
display: block;
|
|
13
|
+
margin-bottom: 4px;
|
|
14
|
+
color: #0f172a;
|
|
15
|
+
font-weight: 600;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.unifi-ultimate-advanced {
|
|
19
|
+
margin-top: 4px;
|
|
20
|
+
padding-top: 4px;
|
|
21
|
+
border-top: 1px solid #e5e7eb;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
</style>
|
|
25
|
+
|
|
1
26
|
<script type="text/javascript">
|
|
2
27
|
RED.nodes.registerType("unifi-access-config", {
|
|
3
28
|
category: "config",
|
|
@@ -13,38 +38,85 @@
|
|
|
13
38
|
return this.name || this.host || "Unifi Access Config";
|
|
14
39
|
},
|
|
15
40
|
oneditprepare: function () {
|
|
41
|
+
const $host = $("#node-config-input-host");
|
|
42
|
+
const $connectionHint = $("#node-config-input-connectionHint");
|
|
43
|
+
|
|
44
|
+
function escapeHtml(value) {
|
|
45
|
+
// The connection preview is rendered as HTML, so sanitize any
|
|
46
|
+
// host text typed by the user first.
|
|
47
|
+
return String(value || "")
|
|
48
|
+
.replace(/&/g, "&")
|
|
49
|
+
.replace(/</g, "<")
|
|
50
|
+
.replace(/>/g, ">")
|
|
51
|
+
.replace(/"/g, """)
|
|
52
|
+
.replace(/'/g, "'");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function refreshConnectionHint() {
|
|
56
|
+
const host = String($host.val() || "").trim();
|
|
57
|
+
if (!host) {
|
|
58
|
+
$connectionHint.html("<strong>Enter your controller address</strong>The node uses the standard UniFi Access API connection automatically.");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// UniFi Access defaults to port 12445. Show the fully resolved
|
|
63
|
+
// host in the preview even when the user omitted the port.
|
|
64
|
+
const normalizedHost = host.replace(/^https?:\/\//i, "").replace(/\/+$/, "");
|
|
65
|
+
const hasPort = /:\d+$/.test(normalizedHost);
|
|
66
|
+
const previewHost = hasPort ? normalizedHost : normalizedHost + ":12445";
|
|
67
|
+
$connectionHint.html(
|
|
68
|
+
"<strong>Connection preview</strong>Will connect to <code>https://" +
|
|
69
|
+
escapeHtml(previewHost) +
|
|
70
|
+
"</code>"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
16
74
|
try {
|
|
75
|
+
// Open the help panel automatically so token/port notes are
|
|
76
|
+
// visible while the user configures the connection.
|
|
17
77
|
RED.sidebar.show("help");
|
|
18
78
|
} catch (error) {
|
|
19
79
|
}
|
|
80
|
+
|
|
81
|
+
refreshConnectionHint();
|
|
82
|
+
|
|
83
|
+
$host.on("change input", function () {
|
|
84
|
+
// Keep the preview synchronized with the current host value.
|
|
85
|
+
refreshConnectionHint();
|
|
86
|
+
});
|
|
20
87
|
}
|
|
21
88
|
});
|
|
22
89
|
</script>
|
|
23
90
|
|
|
24
91
|
<script type="text/html" data-template-name="unifi-access-config">
|
|
25
92
|
<div class="form-row">
|
|
26
|
-
<label for="node-config-input-
|
|
27
|
-
<input type="text" id="node-config-input-name" placeholder="Unifi Access Config">
|
|
28
|
-
</div>
|
|
29
|
-
<div class="form-row">
|
|
30
|
-
<label for="node-config-input-host"><i class="fa fa-globe"></i> Host</label>
|
|
93
|
+
<label for="node-config-input-host"><i class="fa fa-globe"></i> Controller</label>
|
|
31
94
|
<input type="text" id="node-config-input-host" placeholder="192.168.1.10 or access.local:12445">
|
|
32
95
|
</div>
|
|
33
96
|
<div class="form-row">
|
|
34
97
|
<label for="node-config-input-apiToken"><i class="fa fa-key"></i> API Token</label>
|
|
35
|
-
<input type="password" id="node-config-input-apiToken">
|
|
98
|
+
<input type="password" id="node-config-input-apiToken" placeholder="Paste your UniFi Access token">
|
|
36
99
|
</div>
|
|
37
100
|
<div class="form-row">
|
|
38
|
-
<label for="node-config-input-rejectUnauthorized"><i class="fa fa-lock"></i>
|
|
101
|
+
<label for="node-config-input-rejectUnauthorized"><i class="fa fa-lock"></i> Security</label>
|
|
39
102
|
<input type="checkbox" id="node-config-input-rejectUnauthorized" style="display:inline-block; width:auto; vertical-align:top;">
|
|
40
|
-
<span style="margin-left:8px;">
|
|
103
|
+
<span style="margin-left:8px;">Secure connection only</span>
|
|
104
|
+
</div>
|
|
105
|
+
<div class="form-row">
|
|
106
|
+
<div id="node-config-input-connectionHint" class="unifi-ultimate-card"></div>
|
|
107
|
+
</div>
|
|
108
|
+
<div id="node-config-row-advanced" class="unifi-ultimate-advanced">
|
|
109
|
+
<div class="form-row">
|
|
110
|
+
<label for="node-config-input-name"><i class="fa fa-tag"></i> Label</label>
|
|
111
|
+
<input type="text" id="node-config-input-name" placeholder="Optional custom label">
|
|
112
|
+
</div>
|
|
41
113
|
</div>
|
|
42
114
|
</script>
|
|
43
115
|
|
|
44
116
|
<script type="text/html" data-help-name="unifi-access-config">
|
|
45
|
-
<p>
|
|
46
|
-
<p>
|
|
47
|
-
<p>
|
|
48
|
-
<p>
|
|
49
|
-
<p><
|
|
117
|
+
<p>Connects the node to your UniFi Access controller.</p>
|
|
118
|
+
<p><b>Controller</b> accepts the UniFi Access host or <code>host:port</code>. If you omit the port, the node uses the default UniFi Access port automatically.</p>
|
|
119
|
+
<p><b>API Token</b> is the bearer token used for authentication.</p>
|
|
120
|
+
<p><b>Security</b> controls certificate verification. Disable it only when your controller uses a self-signed certificate.</p>
|
|
121
|
+
<p><b>Label</b> is optional and only changes how the config node is shown in the editor.</p>
|
|
50
122
|
</script>
|
|
@@ -18,6 +18,8 @@ const {
|
|
|
18
18
|
} = require("./utils/unifi-access-device-registry");
|
|
19
19
|
|
|
20
20
|
module.exports = function(RED) {
|
|
21
|
+
// Doorbell requests are "best effort" from the public API perspective, so
|
|
22
|
+
// keep a short-lived in-memory ledger to validate later cancel requests.
|
|
21
23
|
const ACTIVE_DOORBELL_TTL_MS = 180000;
|
|
22
24
|
|
|
23
25
|
function UnifiAccessConfigNode(config) {
|
|
@@ -53,6 +55,8 @@ module.exports = function(RED) {
|
|
|
53
55
|
throw new Error("The UniFi Access API token is missing.");
|
|
54
56
|
}
|
|
55
57
|
|
|
58
|
+
// Access uses bearer authentication against the controller directly,
|
|
59
|
+
// so all callers only need to provide the relative path and options.
|
|
56
60
|
const queryString = buildQueryString(query);
|
|
57
61
|
const normalizedPath = String(path || "").startsWith("/") ? String(path || "") : `/${String(path || "")}`;
|
|
58
62
|
const requestUrl = new URL(`${node.baseUrl}${normalizedPath}${queryString}`);
|
|
@@ -81,6 +85,8 @@ module.exports = function(RED) {
|
|
|
81
85
|
const response = await node.apiRequest({
|
|
82
86
|
path: definition.listPath,
|
|
83
87
|
method: "GET",
|
|
88
|
+
// Some Access device inventories only refresh reliably when this
|
|
89
|
+
// query flag is present.
|
|
84
90
|
query: deviceType === "device" ? { refresh: "true" } : undefined
|
|
85
91
|
});
|
|
86
92
|
|
|
@@ -98,6 +104,8 @@ module.exports = function(RED) {
|
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
if (definition.detailPath) {
|
|
107
|
+
// Doors expose a direct detail endpoint, while generic devices
|
|
108
|
+
// may need to be looked up from the collection response.
|
|
101
109
|
const response = await node.apiRequest({
|
|
102
110
|
path: definition.detailPath.replace(":id", encodeURIComponent(String(deviceId || "").trim())),
|
|
103
111
|
method: "GET"
|
|
@@ -136,12 +144,15 @@ module.exports = function(RED) {
|
|
|
136
144
|
};
|
|
137
145
|
|
|
138
146
|
node.buildWebSocketUrl = (path) => {
|
|
147
|
+
// Rebuild the websocket URL from the configured HTTPS controller URL.
|
|
139
148
|
const url = new URL(`${node.baseUrl}${path}`);
|
|
140
149
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
141
150
|
return url.toString();
|
|
142
151
|
};
|
|
143
152
|
|
|
144
153
|
node.broadcastNotification = (payload) => {
|
|
154
|
+
// The config node centralizes one websocket stream and fans it out
|
|
155
|
+
// to all Access runtime nodes interested in events.
|
|
145
156
|
node.nodeClients.forEach((client) => {
|
|
146
157
|
try {
|
|
147
158
|
if (client && typeof client.handleAccessEventUpdate === "function") {
|
|
@@ -153,6 +164,8 @@ module.exports = function(RED) {
|
|
|
153
164
|
};
|
|
154
165
|
|
|
155
166
|
node.updateDoorbellState = (payload) => {
|
|
167
|
+
// Track doorbell lifecycle so "Cancel Doorbell" can avoid sending a
|
|
168
|
+
// blind cancel when no active ring is currently known.
|
|
156
169
|
const eventName = String(payload && payload.event || "").trim();
|
|
157
170
|
const data = payload && payload.data && typeof payload.data === "object" ? payload.data : {};
|
|
158
171
|
const device = data.device && typeof data.device === "object" ? data.device : {};
|
|
@@ -177,6 +190,8 @@ module.exports = function(RED) {
|
|
|
177
190
|
}
|
|
178
191
|
|
|
179
192
|
if (eventName.startsWith("access.doorbell.completed")) {
|
|
193
|
+
// If a request id is present, use it to avoid deleting a newer
|
|
194
|
+
// active ring with an old completion event.
|
|
180
195
|
const current = node.activeDoorbells.get(deviceId);
|
|
181
196
|
if (!current) {
|
|
182
197
|
return;
|
|
@@ -189,6 +204,8 @@ module.exports = function(RED) {
|
|
|
189
204
|
};
|
|
190
205
|
|
|
191
206
|
node.purgeExpiredDoorbells = () => {
|
|
207
|
+
// Clean stale entries opportunistically instead of running a
|
|
208
|
+
// dedicated timer for such a small in-memory cache.
|
|
192
209
|
const now = Date.now();
|
|
193
210
|
Array.from(node.activeDoorbells.entries()).forEach(([deviceId, entry]) => {
|
|
194
211
|
const expiresAt = Number(entry && entry.expiresAt);
|
|
@@ -214,6 +231,8 @@ module.exports = function(RED) {
|
|
|
214
231
|
};
|
|
215
232
|
|
|
216
233
|
node.markDoorbellTriggered = (deviceId, metadata) => {
|
|
234
|
+
// Manual trigger requests can arrive before the websocket confirms
|
|
235
|
+
// the ring, so pre-mark them as active for safe cancel behavior.
|
|
217
236
|
const normalizedId = String(deviceId || "").trim();
|
|
218
237
|
if (!normalizedId) {
|
|
219
238
|
return;
|
|
@@ -237,9 +256,15 @@ module.exports = function(RED) {
|
|
|
237
256
|
return;
|
|
238
257
|
}
|
|
239
258
|
|
|
259
|
+
// Delay reconnect attempts slightly to avoid a busy loop while the
|
|
260
|
+
// controller is restarting or upgrading.
|
|
240
261
|
node.reconnectTimer = setTimeout(() => {
|
|
241
262
|
node.reconnectTimer = null;
|
|
242
|
-
|
|
263
|
+
try {
|
|
264
|
+
node.ensureWebSocket();
|
|
265
|
+
} catch (error) {
|
|
266
|
+
node.warn(`Access websocket reconnect failed: ${error && error.message ? error.message : error}`);
|
|
267
|
+
}
|
|
243
268
|
}, 5000);
|
|
244
269
|
};
|
|
245
270
|
|
|
@@ -254,6 +279,7 @@ module.exports = function(RED) {
|
|
|
254
279
|
return;
|
|
255
280
|
}
|
|
256
281
|
|
|
282
|
+
// Load ws only when live notifications are actually needed.
|
|
257
283
|
try {
|
|
258
284
|
({ WebSocket } = require("ws"));
|
|
259
285
|
} catch (error) {
|
|
@@ -261,16 +287,25 @@ module.exports = function(RED) {
|
|
|
261
287
|
return;
|
|
262
288
|
}
|
|
263
289
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
290
|
+
let ws;
|
|
291
|
+
try {
|
|
292
|
+
ws = new WebSocket(node.buildWebSocketUrl("/api/v1/developer/devices/notifications"), {
|
|
293
|
+
headers: {
|
|
294
|
+
Authorization: `Bearer ${apiToken}`,
|
|
295
|
+
Accept: "application/json"
|
|
296
|
+
},
|
|
297
|
+
rejectUnauthorized: node.rejectUnauthorized
|
|
298
|
+
});
|
|
299
|
+
} catch (error) {
|
|
300
|
+
node.warn(`Unable to open Access websocket: ${error && error.message ? error.message : error}`);
|
|
301
|
+
node.scheduleReconnect();
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
271
304
|
|
|
272
305
|
ws.on("message", (rawData) => {
|
|
273
306
|
try {
|
|
307
|
+
// Ignore malformed websocket frames rather than killing the
|
|
308
|
+
// whole notification stream.
|
|
274
309
|
const text = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : String(rawData);
|
|
275
310
|
const parsed = JSON.parse(text);
|
|
276
311
|
node.updateDoorbellState(parsed);
|
|
@@ -316,9 +351,15 @@ module.exports = function(RED) {
|
|
|
316
351
|
return;
|
|
317
352
|
}
|
|
318
353
|
|
|
354
|
+
// Maintain one entry per runtime node and spin up the websocket only
|
|
355
|
+
// when the first client subscribes.
|
|
319
356
|
node.nodeClients = node.nodeClients.filter((entry) => entry && entry.id !== client.id);
|
|
320
357
|
node.nodeClients.push(client);
|
|
321
|
-
|
|
358
|
+
try {
|
|
359
|
+
node.ensureWebSocket();
|
|
360
|
+
} catch (error) {
|
|
361
|
+
node.warn(`Unable to initialize Access websocket: ${error && error.message ? error.message : error}`);
|
|
362
|
+
}
|
|
322
363
|
};
|
|
323
364
|
|
|
324
365
|
node.removeClient = (client) => {
|
|
@@ -332,7 +373,9 @@ module.exports = function(RED) {
|
|
|
332
373
|
node.isClosing = true;
|
|
333
374
|
node.activeDoorbells.clear();
|
|
334
375
|
node.closeWebSocket();
|
|
335
|
-
done
|
|
376
|
+
if (typeof done === "function") {
|
|
377
|
+
done();
|
|
378
|
+
}
|
|
336
379
|
});
|
|
337
380
|
}
|
|
338
381
|
|
|
@@ -344,6 +387,8 @@ module.exports = function(RED) {
|
|
|
344
387
|
|
|
345
388
|
RED.httpAdmin.get("/unifiAccess/device-types", RED.auth.needsPermission("unifi-access-config.read"), async (req, res) => {
|
|
346
389
|
try {
|
|
390
|
+
// Editor bootstrap endpoint: return only the small list needed to
|
|
391
|
+
// populate the "Control" dropdown.
|
|
347
392
|
res.json(getDeviceTypes().map((definition) => ({
|
|
348
393
|
type: definition.type,
|
|
349
394
|
label: definition.label
|
|
@@ -364,6 +409,8 @@ module.exports = function(RED) {
|
|
|
364
409
|
}
|
|
365
410
|
|
|
366
411
|
if (!serverId || !deviceId) {
|
|
412
|
+
// Before a concrete device is selected, return the generic
|
|
413
|
+
// capability set for the chosen Access family.
|
|
367
414
|
res.json(getCapabilitiesForType(deviceType));
|
|
368
415
|
return;
|
|
369
416
|
}
|
|
@@ -448,4 +495,5 @@ module.exports = function(RED) {
|
|
|
448
495
|
res.status(500).json({ error: error.message });
|
|
449
496
|
}
|
|
450
497
|
});
|
|
498
|
+
|
|
451
499
|
};
|