unifi-protect 2.0.0 → 3.0.1
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 +18 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/protect-api-livestream.d.ts +21 -0
- package/dist/protect-api-livestream.js +294 -0
- package/dist/protect-api-livestream.js.map +1 -0
- package/dist/protect-api-updates.d.ts +2 -2
- package/dist/protect-api.d.ts +36 -15
- package/dist/protect-api.js +261 -106
- package/dist/protect-api.js.map +1 -1
- package/dist/protect-logging.d.ts +1 -1
- package/dist/protect-types.d.ts +256 -18
- package/package.json +9 -9
package/dist/protect-api.js
CHANGED
|
@@ -62,9 +62,21 @@ class ProtectApi {
|
|
|
62
62
|
/* eslint-enable no-console */
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
+
this._bootstrap = null;
|
|
66
|
+
this._cameras = null;
|
|
67
|
+
this._lights = null;
|
|
68
|
+
this._sensors = null;
|
|
69
|
+
this._viewers = null;
|
|
65
70
|
this.apiErrorCount = 0;
|
|
66
71
|
this.apiLastSuccess = 0;
|
|
67
|
-
this.
|
|
72
|
+
this._eventsWs = null;
|
|
73
|
+
this.httpsAgent = new https_1.default.Agent({ rejectUnauthorized: false });
|
|
74
|
+
this.isAdminUser = false;
|
|
75
|
+
this.isLoggedIn = false;
|
|
76
|
+
this.isLoginRefresh = false;
|
|
77
|
+
this._log = log;
|
|
78
|
+
this.loginAge = 0;
|
|
79
|
+
this.loginAttempt = null;
|
|
68
80
|
this.nvrAddress = nvrAddress;
|
|
69
81
|
this.username = username;
|
|
70
82
|
this.password = password;
|
|
@@ -74,7 +86,7 @@ class ProtectApi {
|
|
|
74
86
|
async acquireToken() {
|
|
75
87
|
// We only need to acquire a token if we aren't already logged in, or we don't already have a token,
|
|
76
88
|
// or don't know which device type we're on.
|
|
77
|
-
if (this.
|
|
89
|
+
if (this.isLoggedIn || this.headers.has("X-CSRF-Token") || this.headers.has("Authorization")) {
|
|
78
90
|
return true;
|
|
79
91
|
}
|
|
80
92
|
// UniFi OS has cross-site request forgery protection built into it's web management UI.
|
|
@@ -86,29 +98,30 @@ class ProtectApi {
|
|
|
86
98
|
// We found a token.
|
|
87
99
|
if (csrfToken) {
|
|
88
100
|
this.headers.set("X-CSRF-Token", csrfToken);
|
|
89
|
-
// UniFi OS has support for keepalive. Let's take advantage of that and reduce the workload on controllers.
|
|
90
|
-
this.httpsAgent = new https_1.default.Agent({ keepAlive: true, maxFreeSockets: 5, maxSockets: 10, rejectUnauthorized: false, timeout: 60 * 1000 });
|
|
91
101
|
return true;
|
|
92
102
|
}
|
|
93
103
|
}
|
|
94
104
|
// Couldn't deduce what type of NVR device we were connecting to.
|
|
95
105
|
return false;
|
|
96
106
|
}
|
|
97
|
-
//
|
|
98
|
-
async
|
|
107
|
+
// Login to the UniFi Protect API.
|
|
108
|
+
async loginNvr() {
|
|
99
109
|
const now = Date.now();
|
|
100
110
|
// Is it time to renew our credentials?
|
|
101
111
|
if (now > (this.loginAge + (settings_1.PROTECT_LOGIN_REFRESH_INTERVAL * 1000))) {
|
|
102
|
-
this.
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
if (this.loginAge) {
|
|
113
|
+
this.isLoginRefresh = true;
|
|
114
|
+
}
|
|
115
|
+
this.clearLoginCredentials();
|
|
105
116
|
}
|
|
106
117
|
// If we're already logged in, and it's not time to renew our credentials, we're done.
|
|
107
|
-
if (this.
|
|
118
|
+
if (this.isLoggedIn) {
|
|
119
|
+
this.isLoginRefresh = false;
|
|
108
120
|
return true;
|
|
109
121
|
}
|
|
110
122
|
// Make sure we have a token, or get one if needed.
|
|
111
123
|
if (!(await this.acquireToken())) {
|
|
124
|
+
this.clearLoginCredentials();
|
|
112
125
|
return false;
|
|
113
126
|
}
|
|
114
127
|
// Log us in.
|
|
@@ -117,11 +130,12 @@ class ProtectApi {
|
|
|
117
130
|
method: "POST"
|
|
118
131
|
});
|
|
119
132
|
if (!(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
133
|
+
this.clearLoginCredentials();
|
|
120
134
|
return false;
|
|
121
135
|
}
|
|
122
136
|
// We're logged in.
|
|
123
|
-
this.loggedIn = true;
|
|
124
137
|
this.loginAge = now;
|
|
138
|
+
this.isLoggedIn = true;
|
|
125
139
|
// Configure headers.
|
|
126
140
|
const csrfToken = response.headers.get("X-CSRF-Token");
|
|
127
141
|
const cookie = response.headers.get("Set-Cookie");
|
|
@@ -134,6 +148,16 @@ class ProtectApi {
|
|
|
134
148
|
this.clearLoginCredentials();
|
|
135
149
|
return false;
|
|
136
150
|
}
|
|
151
|
+
// Create a new UniFi Protect login attempt if one isn't already inflight.
|
|
152
|
+
login() {
|
|
153
|
+
// We want to ensure we aren't trying simultaneous login attempts and creating a potential DoS, so we wait
|
|
154
|
+
// for one attempt to complete before beginning another.
|
|
155
|
+
if (!this.loginAttempt) {
|
|
156
|
+
this.loginAttempt = this.loginNvr();
|
|
157
|
+
this.loginAttempt.finally(() => this.loginAttempt = null);
|
|
158
|
+
}
|
|
159
|
+
return this.loginAttempt;
|
|
160
|
+
}
|
|
137
161
|
// Get our UniFi Protect NVR configuration.
|
|
138
162
|
async bootstrapProtect() {
|
|
139
163
|
// Log us in if needed.
|
|
@@ -165,9 +189,9 @@ class ProtectApi {
|
|
|
165
189
|
}
|
|
166
190
|
// On launch, let the user know we made it.
|
|
167
191
|
const firstRun = this.bootstrap ? false : true;
|
|
168
|
-
this.
|
|
169
|
-
if (firstRun) {
|
|
170
|
-
this.log.info("%s: Connected to the Protect controller API (address: %s mac: %s).", this.getNvrName(), data.nvr.host, data.nvr.mac);
|
|
192
|
+
this._bootstrap = data;
|
|
193
|
+
if (firstRun && !this.isLoginRefresh) {
|
|
194
|
+
this.log.info("%s: Connected to the UniFi Protect controller API (address: %s mac: %s).", this.getNvrName(), data.nvr.host, data.nvr.mac);
|
|
171
195
|
}
|
|
172
196
|
// Capture the bootstrap if we're debugging.
|
|
173
197
|
this.log.debug(util_1.default.inspect(this.bootstrap, { colors: true, depth: null, sorted: true }));
|
|
@@ -184,7 +208,7 @@ class ProtectApi {
|
|
|
184
208
|
return false;
|
|
185
209
|
}
|
|
186
210
|
// If we already have a listener, we're already all set.
|
|
187
|
-
if (this.
|
|
211
|
+
if (this.eventsWs) {
|
|
188
212
|
return true;
|
|
189
213
|
}
|
|
190
214
|
const params = new URLSearchParams({ lastUpdateId: (_b = (_a = this.bootstrap) === null || _a === void 0 ? void 0 : _a.lastUpdateId) !== null && _b !== void 0 ? _b : "" });
|
|
@@ -198,74 +222,77 @@ class ProtectApi {
|
|
|
198
222
|
});
|
|
199
223
|
if (!ws) {
|
|
200
224
|
this.log.error("Unable to connect to the realtime update events API. Will retry again later.");
|
|
201
|
-
this.
|
|
202
|
-
this.eventListenerConfigured = false;
|
|
225
|
+
this._eventsWs = null;
|
|
203
226
|
return false;
|
|
204
227
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
this.eventListener.on("message", this.heartbeatEventListener.bind(this));
|
|
208
|
-
this.eventListener.on("open", this.heartbeatEventListener.bind(this));
|
|
209
|
-
this.eventListener.on("ping", this.heartbeatEventListener.bind(this));
|
|
210
|
-
this.eventListener.on("close", () => {
|
|
211
|
-
clearTimeout(this.eventHeartbeatTimer);
|
|
212
|
-
});
|
|
213
|
-
this.eventListener.on("error", (error) => {
|
|
214
|
-
var _a;
|
|
228
|
+
// Handle any websocket errors.
|
|
229
|
+
ws.once("error", (error) => {
|
|
215
230
|
// If we're closing before fully established it's because we're shutting down the API - ignore it.
|
|
216
231
|
if (error.message !== "WebSocket was closed before the connection was established") {
|
|
217
232
|
this.log.error("%s: %s", this.getNvrName(), error);
|
|
218
233
|
}
|
|
219
|
-
|
|
220
|
-
this.
|
|
221
|
-
this.eventListenerConfigured = false;
|
|
234
|
+
ws.terminate();
|
|
235
|
+
this._eventsWs = null;
|
|
222
236
|
});
|
|
223
|
-
|
|
237
|
+
// Make the websocket available, and then we're done.
|
|
238
|
+
this._eventsWs = ws;
|
|
239
|
+
if (!this.isLoginRefresh) {
|
|
240
|
+
this.log.info("%s: Connected to the UniFi Protect realtime update events API.", this.getNvrName());
|
|
241
|
+
}
|
|
224
242
|
}
|
|
225
243
|
catch (error) {
|
|
226
244
|
this.log.error("%s: Error connecting to the realtime update events API: %s", this.getNvrName(), error);
|
|
227
245
|
}
|
|
228
246
|
return true;
|
|
229
247
|
}
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
var _a, _b;
|
|
233
|
-
// Refresh the configuration from the NVR.
|
|
234
|
-
if (!(await this.bootstrapProtect())) {
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
this.log.debug(util_1.default.inspect(this.bootstrap, { colors: true, depth: null, sorted: true }));
|
|
238
|
-
const newDeviceList = (_a = this.bootstrap) === null || _a === void 0 ? void 0 : _a.cameras;
|
|
248
|
+
// Generic to refresh the device list of a specific Protect device class.
|
|
249
|
+
refreshDeviceClass(currentDeviceList, newDeviceList) {
|
|
239
250
|
// Notify the user about any new devices that we've discovered.
|
|
240
251
|
if (newDeviceList) {
|
|
241
252
|
for (const newDevice of newDeviceList) {
|
|
242
253
|
// We already know about this device.
|
|
243
|
-
if (
|
|
254
|
+
if (currentDeviceList === null || currentDeviceList === void 0 ? void 0 : currentDeviceList.some((x) => x.mac === newDevice.mac)) {
|
|
244
255
|
continue;
|
|
245
256
|
}
|
|
246
|
-
// We only want to discover
|
|
247
|
-
if (!newDevice.
|
|
257
|
+
// We only want to discover adopted devices.
|
|
258
|
+
if (!newDevice.isAdopted) {
|
|
248
259
|
continue;
|
|
249
260
|
}
|
|
250
261
|
// We've discovered a new device.
|
|
251
262
|
this.log.info("%s: Discovered %s: %s.", this.getNvrName(), newDevice.modelKey, this.getDeviceName(newDevice, newDevice.name, true));
|
|
252
|
-
this.log.debug(util_1.default.inspect(newDevice, { colors: true, depth: null, sorted: true }));
|
|
253
263
|
}
|
|
254
264
|
}
|
|
255
265
|
// Notify the user about any devices that have disappeared.
|
|
256
|
-
if (
|
|
257
|
-
for (const existingDevice of
|
|
266
|
+
if (currentDeviceList) {
|
|
267
|
+
for (const existingDevice of currentDeviceList) {
|
|
258
268
|
// This device still is visible.
|
|
259
269
|
if (newDeviceList === null || newDeviceList === void 0 ? void 0 : newDeviceList.some((x) => x.mac === existingDevice.mac)) {
|
|
260
270
|
continue;
|
|
261
271
|
}
|
|
262
272
|
// We've had a device disappear.
|
|
263
273
|
this.log.debug("%s: Detected %s removal.", this.getFullName(existingDevice), existingDevice.modelKey);
|
|
264
|
-
this.log.debug(util_1.default.inspect(existingDevice, { colors: true, depth: null, sorted: true }));
|
|
265
274
|
}
|
|
266
275
|
}
|
|
267
|
-
//
|
|
268
|
-
|
|
276
|
+
// Return the updated list of devices.
|
|
277
|
+
return newDeviceList !== null && newDeviceList !== void 0 ? newDeviceList : null;
|
|
278
|
+
}
|
|
279
|
+
// Get the list of UniFi Protect devices associated with a NVR.
|
|
280
|
+
async refreshDevices() {
|
|
281
|
+
var _a, _b, _c, _d;
|
|
282
|
+
// Refresh the configuration from the NVR.
|
|
283
|
+
if (!(await this.bootstrapProtect())) {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
this.log.debug(util_1.default.inspect(this.bootstrap, { colors: true, depth: null, sorted: true }));
|
|
287
|
+
// We currently know about the following device classes:
|
|
288
|
+
// - cameras (including doorbells)
|
|
289
|
+
// - lights
|
|
290
|
+
// - sensors
|
|
291
|
+
// - viewers
|
|
292
|
+
this._cameras = this.refreshDeviceClass(this.cameras, (_a = this.bootstrap) === null || _a === void 0 ? void 0 : _a.cameras);
|
|
293
|
+
this._lights = this.refreshDeviceClass(this.lights, (_b = this.bootstrap) === null || _b === void 0 ? void 0 : _b.lights);
|
|
294
|
+
this._sensors = this.refreshDeviceClass(this.sensors, (_c = this.bootstrap) === null || _c === void 0 ? void 0 : _c.sensors);
|
|
295
|
+
this._viewers = this.refreshDeviceClass(this.viewers, (_d = this.bootstrap) === null || _d === void 0 ? void 0 : _d.viewers);
|
|
269
296
|
return true;
|
|
270
297
|
}
|
|
271
298
|
// Validate if all RTSP channels enabled on all cameras.
|
|
@@ -314,37 +341,45 @@ class ProtectApi {
|
|
|
314
341
|
}
|
|
315
342
|
return true;
|
|
316
343
|
}
|
|
317
|
-
//
|
|
318
|
-
async
|
|
319
|
-
|
|
344
|
+
// Update a camera object.
|
|
345
|
+
async updateCamera(device, payload) {
|
|
346
|
+
// No device object, we're done.
|
|
347
|
+
if (!device) {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
320
350
|
// Log us in if needed.
|
|
321
351
|
if (!(await this.login())) {
|
|
322
352
|
return null;
|
|
323
353
|
}
|
|
324
|
-
// Only admin users can
|
|
354
|
+
// Only admin users can update JSON objects.
|
|
325
355
|
if (!this.isAdminUser) {
|
|
326
356
|
return null;
|
|
327
357
|
}
|
|
328
|
-
|
|
329
|
-
|
|
358
|
+
this.log.debug("%s: %s", this.getFullName(device), util_1.default.inspect(payload, { colors: true, depth: null, sorted: true }));
|
|
359
|
+
// Update Protect with the new configuration.
|
|
360
|
+
const response = await this.fetch(this.camerasUrl() + "/" + device.id, {
|
|
361
|
+
body: JSON.stringify(payload),
|
|
362
|
+
method: "PATCH"
|
|
363
|
+
});
|
|
364
|
+
if (!(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
365
|
+
this.log.error("%s: Unable to configure the camera: %s.", this.getFullName(device), response === null || response === void 0 ? void 0 : response.status);
|
|
330
366
|
return null;
|
|
331
367
|
}
|
|
332
|
-
//
|
|
333
|
-
|
|
334
|
-
|
|
368
|
+
// We successfully set the message, return the updated device object.
|
|
369
|
+
return await response.json();
|
|
370
|
+
}
|
|
371
|
+
// Update camera channels on a supported Protect device.
|
|
372
|
+
async updateCameraChannels(device) {
|
|
373
|
+
// Make sure we have the permissions to modify the camera JSON.
|
|
374
|
+
if (!(await this.canModifyCamera(device))) {
|
|
375
|
+
return null;
|
|
335
376
|
}
|
|
336
|
-
// Enable RTSP on all available channels.
|
|
337
|
-
device.channels = device.channels.map((channel) => {
|
|
338
|
-
channel.isRtspEnabled = true;
|
|
339
|
-
return channel;
|
|
340
|
-
});
|
|
341
377
|
// Update Protect with the new configuration.
|
|
342
378
|
const response = await this.fetch(this.camerasUrl() + "/" + device.id, {
|
|
343
379
|
body: JSON.stringify({ channels: device.channels }),
|
|
344
380
|
method: "PATCH"
|
|
345
381
|
}, true, false);
|
|
346
|
-
// Since we took responsibility for interpreting the outcome of the fetch, we need to check for
|
|
347
|
-
// errors for ourself.
|
|
382
|
+
// Since we took responsibility for interpreting the outcome of the fetch, we need to check for any errors.
|
|
348
383
|
if (!response || !(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
349
384
|
this.apiErrorCount++;
|
|
350
385
|
if ((response === null || response === void 0 ? void 0 : response.status) === 403) {
|
|
@@ -362,8 +397,8 @@ class ProtectApi {
|
|
|
362
397
|
// Everything worked, save the new channel array.
|
|
363
398
|
return await response.json();
|
|
364
399
|
}
|
|
365
|
-
// Update a
|
|
366
|
-
async
|
|
400
|
+
// Update a light object.
|
|
401
|
+
async updateLight(device, payload) {
|
|
367
402
|
// No device object, we're done.
|
|
368
403
|
if (!device) {
|
|
369
404
|
return null;
|
|
@@ -372,23 +407,97 @@ class ProtectApi {
|
|
|
372
407
|
if (!(await this.login())) {
|
|
373
408
|
return null;
|
|
374
409
|
}
|
|
375
|
-
// Only admin users can
|
|
410
|
+
// Only admin users can update JSON objects.
|
|
411
|
+
if (!this.isAdminUser) {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
this.log.error("%s: %s", this.getFullName(device), util_1.default.inspect(payload, { colors: true, depth: null, sorted: true }));
|
|
415
|
+
// Update Protect with the new configuration.
|
|
416
|
+
const response = await this.fetch(this.lightsUrl() + "/" + device.id, {
|
|
417
|
+
body: JSON.stringify(payload),
|
|
418
|
+
method: "PATCH"
|
|
419
|
+
});
|
|
420
|
+
if (!(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
421
|
+
this.log.debug("%s: Unable to configure the light: %s.", this.getFullName(device), response === null || response === void 0 ? void 0 : response.status);
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
// We successfully set the message, return the updated device object.
|
|
425
|
+
return await response.json();
|
|
426
|
+
}
|
|
427
|
+
// Update a viewer object.
|
|
428
|
+
async updateViewer(device, payload) {
|
|
429
|
+
// No device object, we're done.
|
|
430
|
+
if (!device) {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
// Log us in if needed.
|
|
434
|
+
if (!(await this.login())) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
// Only admin users can update JSON objects.
|
|
376
438
|
if (!this.isAdminUser) {
|
|
377
439
|
return null;
|
|
378
440
|
}
|
|
379
441
|
this.log.debug("%s: %s", this.getFullName(device), util_1.default.inspect(payload, { colors: true, depth: null, sorted: true }));
|
|
380
442
|
// Update Protect with the new configuration.
|
|
381
|
-
const response = await this.fetch(this.
|
|
443
|
+
const response = await this.fetch(this.viewersUrl() + "/" + device.id, {
|
|
382
444
|
body: JSON.stringify(payload),
|
|
383
445
|
method: "PATCH"
|
|
384
446
|
});
|
|
385
447
|
if (!(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
386
|
-
this.log.error("%s: Unable to configure the
|
|
448
|
+
this.log.error("%s: Unable to configure the viewer: %s.", this.getFullName(device), response === null || response === void 0 ? void 0 : response.status);
|
|
387
449
|
return null;
|
|
388
450
|
}
|
|
389
451
|
// We successfully set the message, return the updated device object.
|
|
390
452
|
return await response.json();
|
|
391
453
|
}
|
|
454
|
+
// Enable RTSP stream support on an attached Protect device.
|
|
455
|
+
async enableRtsp(device) {
|
|
456
|
+
var _a;
|
|
457
|
+
// Make sure we have the permissions to modify the camera JSON.
|
|
458
|
+
if (!(await this.canModifyCamera(device))) {
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
// Do we have any non-RTSP enabled channels? If not, we're done.
|
|
462
|
+
if (!((_a = device.channels) === null || _a === void 0 ? void 0 : _a.some(channel => !channel.isRtspEnabled))) {
|
|
463
|
+
return device;
|
|
464
|
+
}
|
|
465
|
+
// Enable RTSP on all available channels.
|
|
466
|
+
device.channels = device.channels.map((channel) => {
|
|
467
|
+
channel.isRtspEnabled = true;
|
|
468
|
+
return channel;
|
|
469
|
+
});
|
|
470
|
+
// Update the camera channel JSON with our edits.
|
|
471
|
+
return this.updateCameraChannels(device);
|
|
472
|
+
}
|
|
473
|
+
// Get the bootstrap JSON.
|
|
474
|
+
get bootstrap() {
|
|
475
|
+
return this._bootstrap;
|
|
476
|
+
}
|
|
477
|
+
// Return all the cameras on this controller.
|
|
478
|
+
get cameras() {
|
|
479
|
+
return this._cameras;
|
|
480
|
+
}
|
|
481
|
+
// Get the events websocket.
|
|
482
|
+
get eventsWs() {
|
|
483
|
+
return this._eventsWs;
|
|
484
|
+
}
|
|
485
|
+
// Return all the lights on this controller.
|
|
486
|
+
get lights() {
|
|
487
|
+
return this._lights;
|
|
488
|
+
}
|
|
489
|
+
// Utility to access the logging functions, used by other classes in this library.
|
|
490
|
+
get log() {
|
|
491
|
+
return this._log;
|
|
492
|
+
}
|
|
493
|
+
// Return all the sensors on this controller.
|
|
494
|
+
get sensors() {
|
|
495
|
+
return this._sensors;
|
|
496
|
+
}
|
|
497
|
+
// Return all the viewers on this controller.
|
|
498
|
+
get viewers() {
|
|
499
|
+
return this._viewers;
|
|
500
|
+
}
|
|
392
501
|
// Utility to generate a nicely formatted NVR string.
|
|
393
502
|
getNvrName() {
|
|
394
503
|
var _a;
|
|
@@ -403,33 +512,50 @@ class ProtectApi {
|
|
|
403
512
|
}
|
|
404
513
|
}
|
|
405
514
|
// Utility to generate a nicely formatted device string.
|
|
406
|
-
getDeviceName(
|
|
515
|
+
getDeviceName(device, name = device === null || device === void 0 ? void 0 : device.name, deviceInfo = false) {
|
|
407
516
|
// Validate our inputs.
|
|
408
|
-
if (!
|
|
517
|
+
if (!device) {
|
|
409
518
|
return "";
|
|
410
519
|
}
|
|
520
|
+
// Include the host address information, if we have it.
|
|
521
|
+
const host = (("host" in device) && device.host) ? "address: " + device.host + " " : "";
|
|
411
522
|
// A completely enumerated device will appear as:
|
|
412
|
-
//
|
|
413
|
-
return name + " [" +
|
|
414
|
-
(
|
|
523
|
+
// Device Name [Device Type] (address: IP address, mac: MAC address).
|
|
524
|
+
return name + " [" + device.type + "]" +
|
|
525
|
+
(deviceInfo ? " (" + host + "mac: " + device.mac + ")" : "");
|
|
415
526
|
}
|
|
416
527
|
// Utility to generate a nicely formatted NVR and device string.
|
|
417
|
-
getFullName(
|
|
418
|
-
const
|
|
419
|
-
// Returns: NVR [NVR Type]
|
|
420
|
-
return this.getNvrName() + (
|
|
528
|
+
getFullName(device) {
|
|
529
|
+
const deviceName = this.getDeviceName(device);
|
|
530
|
+
// Returns: NVR [NVR Type] Device Name [Device Type]
|
|
531
|
+
return this.getNvrName() + (deviceName.length > 0 ? " " + deviceName : "");
|
|
421
532
|
}
|
|
422
|
-
// Return the URL to directly access cameras
|
|
533
|
+
// Return the URL to directly access cameras.
|
|
423
534
|
camerasUrl() {
|
|
424
535
|
// Boostrapping a UniFi OS device is done through: https://protect-nvr-ip/proxy/protect/api/cameras/CAMERAID.
|
|
425
536
|
return "https://" + this.nvrAddress + "/proxy/protect/api/cameras";
|
|
426
537
|
}
|
|
427
|
-
// Return the
|
|
538
|
+
// Return the URL to directly access lights.
|
|
539
|
+
lightsUrl() {
|
|
540
|
+
// Boostrapping a UniFi OS device is done through: https://protect-nvr-ip/proxy/protect/api/lights/LIGHTID.
|
|
541
|
+
return "https://" + this.nvrAddress + "/proxy/protect/api/lights";
|
|
542
|
+
}
|
|
543
|
+
// Return the URL to directly access viewers.
|
|
544
|
+
viewersUrl() {
|
|
545
|
+
// Boostrapping a UniFi OS device is done through: https://protect-nvr-ip/proxy/protect/api/viewers/VIEWERID.
|
|
546
|
+
return "https://" + this.nvrAddress + "/proxy/protect/api/viewers";
|
|
547
|
+
}
|
|
548
|
+
// Return the URL to the websocket API endpoint.
|
|
549
|
+
wsUrl() {
|
|
550
|
+
// Boostrapping a UniFi OS device is done through: https://protect-nvr-ip/proxy/protect/api/ws/WEBSOCKET.
|
|
551
|
+
return "https://" + this.nvrAddress + "/proxy/protect/api/ws";
|
|
552
|
+
}
|
|
553
|
+
// Return the right Protect NVR authentication URL.
|
|
428
554
|
authUrl() {
|
|
429
555
|
// Authenticating a UniFi OS device is done through: https://protect-nvr-ip/api/auth/login.
|
|
430
556
|
return "https://" + this.nvrAddress + "/api/auth/login";
|
|
431
557
|
}
|
|
432
|
-
// Return the right
|
|
558
|
+
// Return the right Protect NVR bootstrap URL.
|
|
433
559
|
bootstrapUrl() {
|
|
434
560
|
// Boostrapping a UniFi OS device is done through: https://protect-nvr-ip/proxy/protect/api/bootstrap.
|
|
435
561
|
return "https://" + this.nvrAddress + "/proxy/protect/api/bootstrap";
|
|
@@ -442,37 +568,66 @@ class ProtectApi {
|
|
|
442
568
|
updatesUrl() {
|
|
443
569
|
return "wss://" + this.nvrAddress + "/proxy/protect/ws/updates";
|
|
444
570
|
}
|
|
445
|
-
// Utility to check the heartbeat of our listener.
|
|
446
|
-
heartbeatEventListener() {
|
|
447
|
-
// Clear out our last timer and set a new one.
|
|
448
|
-
clearTimeout(this.eventHeartbeatTimer);
|
|
449
|
-
// We use terminate() to immediately destroy the connection, instead of close(), which waits for the close timer.
|
|
450
|
-
this.eventHeartbeatTimer = setTimeout(() => {
|
|
451
|
-
var _a;
|
|
452
|
-
(_a = this.eventListener) === null || _a === void 0 ? void 0 : _a.terminate();
|
|
453
|
-
this.eventListener = null;
|
|
454
|
-
this.eventListenerConfigured = false;
|
|
455
|
-
}, settings_1.PROTECT_EVENTS_HEARTBEAT_INTERVAL * 1000);
|
|
456
|
-
}
|
|
457
571
|
// Utility to clear out old login credentials or attempts.
|
|
458
572
|
clearLoginCredentials() {
|
|
459
|
-
var _a, _b;
|
|
460
573
|
this.isAdminUser = false;
|
|
461
|
-
this.
|
|
574
|
+
this.isLoggedIn = false;
|
|
462
575
|
this.loginAge = 0;
|
|
463
|
-
this.
|
|
464
|
-
|
|
465
|
-
(_a = this.eventListener) === null || _a === void 0 ? void 0 : _a.terminate();
|
|
466
|
-
this.eventListener = null;
|
|
467
|
-
this.eventListenerConfigured = false;
|
|
576
|
+
this.loginAttempt = null;
|
|
577
|
+
this._bootstrap = null;
|
|
468
578
|
// Initialize the headers we need.
|
|
469
579
|
this.headers = new node_fetch_cjs_1.Headers();
|
|
470
580
|
this.headers.set("Content-Type", "application/json");
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
581
|
+
}
|
|
582
|
+
// Utility to validate that we have the privileges we need to modify the camera JSON.
|
|
583
|
+
async canModifyCamera(device) {
|
|
584
|
+
// Log us in if needed.
|
|
585
|
+
if (!(await this.login())) {
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
// Only admin users can activate RTSP streams.
|
|
589
|
+
if (!this.isAdminUser) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
// At the moment, we only know about camera devices.
|
|
593
|
+
if (device.modelKey !== "camera") {
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
return true;
|
|
597
|
+
}
|
|
598
|
+
// Return a WebSocket URL endpoint from the Protect controller for Protect API services (e.g. livestream, talkback).
|
|
599
|
+
async getWsEndpoint(url) {
|
|
600
|
+
if (!url) {
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
// Ask Protect to give us a URL for this websocket.
|
|
604
|
+
const response = await this.fetch(url);
|
|
605
|
+
// Something went wrong, we're done here.
|
|
606
|
+
if (!(response === null || response === void 0 ? void 0 : response.ok)) {
|
|
607
|
+
this.log.error("%s: API endpoint access error%s", this.getNvrName(), response ? ": " + response.status.toString() + " - " + response.statusText + "." : "");
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
try {
|
|
611
|
+
const responseJson = await response.json();
|
|
612
|
+
// Adjust the URL for our address.
|
|
613
|
+
const responseUrl = new URL(responseJson.url);
|
|
614
|
+
responseUrl.hostname = this.nvrAddress;
|
|
615
|
+
// Return the URL to the websocket.
|
|
616
|
+
return responseUrl.toString();
|
|
617
|
+
}
|
|
618
|
+
catch (error) {
|
|
619
|
+
if (error instanceof node_fetch_cjs_1.FetchError) {
|
|
620
|
+
switch (error.code) {
|
|
621
|
+
default:
|
|
622
|
+
this.log.error("%s: Unknown error while communicating with the controller: %s", this.getNvrName(), error.message);
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
this.log.error("%s: An error occurred while communicating with the controller: %s.", this.getNvrName(), error);
|
|
628
|
+
}
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
476
631
|
}
|
|
477
632
|
// Utility to let us streamline error handling and return checking from the Protect API.
|
|
478
633
|
async fetch(url, options = { method: "GET" }, logErrors = true, decodeResponse = true, isRetry = false) {
|