unifi-protect 4.4.0 → 4.6.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 +5 -41
- package/dist/protect-api-events.d.ts +33 -18
- package/dist/protect-api-events.js +40 -2
- package/dist/protect-api-events.js.map +1 -1
- package/dist/protect-api-livestream.d.ts +1 -1
- package/dist/protect-api-livestream.js +7 -1
- package/dist/protect-api-livestream.js.map +1 -1
- package/dist/protect-api.d.ts +253 -9
- package/dist/protect-api.js +330 -39
- package/dist/protect-api.js.map +1 -1
- package/dist/protect-types.d.ts +23 -0
- package/package.json +10 -7
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<SPAN ALIGN="CENTER" STYLE="text-align:center">
|
|
2
2
|
<DIV ALIGN="CENTER" STYLE="text-align:center">
|
|
3
3
|
|
|
4
|
-
[](https://github.com/hjdhjd/unifi-protect)
|
|
4
|
+
[](https://github.com/hjdhjd/unifi-protect)
|
|
5
5
|
|
|
6
6
|
# UniFi Protect API
|
|
7
7
|
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
## Why use this library for UniFi Protect support?
|
|
18
18
|
In short - because I use it every day to support a very popular [Homebridge](https://homebridge.io) plugin named [homebridge-unifi-protect](https://www.npmjs.com/package/homebridge-unifi-protect) that I maintain. I have been occasionally asked if I would consider packaging the core API library separately from the plugin so that other open source projects can take advantage of the work that's been done here to understand and decode the UniFi Protect API.
|
|
19
19
|
|
|
20
|
-
In addition, this implementation is unique: it's the first complete open source implementation of the realtime UniFi Protect update API, enabling instantaneous updates to Protect-related events.
|
|
20
|
+
In addition, this implementation is unique: it's the first complete open source implementation of the realtime UniFi Protect update API, enabling instantaneous updates to Protect-related events. It's also the first (and to my knowledge only) complete implementation of the livestream API provided by UniFi Protect. Note: this is **not** the RTSP URLs that are provided by UniFi Protect controllers, but rather, true access to the H.264 datastream for any camera connected to the Protect controller.
|
|
21
21
|
|
|
22
22
|
Finally - the most significant reason that you should use this library: it's very well-tested, it is modern, and most importantly, *it just works*. It's quite easy to add support for UniFi Protect in your project using this library, and you can rely on the fact that the code is used by a significant population of users out there who ensure its continued robustness.
|
|
23
23
|
|
|
@@ -30,6 +30,7 @@ The UniFi Protect API is undocumented and implementing a library like this one i
|
|
|
30
30
|
- Full access to the UniFi Protect NVR JSON.
|
|
31
31
|
- The ability to retrieve the JSON details, including status, of any supported UniFi Protect device.
|
|
32
32
|
- The ability to modify the Protect NVR JSON or Protect devices.
|
|
33
|
+
- The ability to programmatically access the H.264 livestream for any camera. This is useful when you want lightweight access to the full camera feed without resorting to RTSP. For example, this is how [homebridge-unifi-protect](https://github.com/hjdhjd/homebridge-unifi-protect) implements HomeKit Secure Video capabilities, resulting in a lighter weight solution than trying to read the RTSP streams for each camera from the Protect controller.
|
|
33
34
|
|
|
34
35
|
## Changelog
|
|
35
36
|
* [Changelog](https://github.com/hjdhjd/unifi-protect/blob/main/docs/Changelog.md): changes and release history of this library.
|
|
@@ -43,44 +44,7 @@ npm install unifi-protect
|
|
|
43
44
|
|
|
44
45
|
## Documentation
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### ProtectApi(nvrAddress: string, username: string, password: string [, log: protectLogging])
|
|
49
|
-
Initialize the UniFi Protect API using the UniFi Protect account information contained in `username` and `password`. `log` is an optional parameter that enables you to customize the type of logging that can be generated, including debug logging. If `log` isn't specified, the Protect API will default to logging to the console.
|
|
50
|
-
|
|
51
|
-
### refreshDevices()
|
|
52
|
-
This is where the magic happens. This function:
|
|
53
|
-
|
|
54
|
-
* Logs into the Protect API, if we don't already have an access token.
|
|
55
|
-
* If we do have an access token and it's nearly time to refresh it, it will do so.
|
|
56
|
-
* It then requests a refresh of the UniFi Protect NVR state which includes device information for all the Protect devices associated with that NVR. There are failsafes in place to ensure the Protect NVR doesn't get slammed with API requests causing potential performance issues with the NVR.
|
|
57
|
-
|
|
58
|
-
**Note: `refreshDevices()` must be called at least once after instantiating the API in order to populate the list of UniFi Protect devices associated with an account.**
|
|
59
|
-
|
|
60
|
-
Returns: `true` if successful, `false` otherwise.
|
|
61
|
-
|
|
62
|
-
### protectApi.cameras[]
|
|
63
|
-
The `cameras` property maintains the list of all known Protect camera devices. It is an array of `ProtectCameraConfig` objects, and you can look through [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts) for a sense of what's contained in a `ProtectCameraConfig` object.
|
|
64
|
-
|
|
65
|
-
This property is refreshed each time `refreshDevices()` is called.
|
|
66
|
-
|
|
67
|
-
### protectApi.lights[]
|
|
68
|
-
The `lights` property maintains the list of all known Protect light devices. It is an array of `ProtectLightConfig` objects, and you can look through [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts) for a sense of what's contained in a `ProtectLightConfig` object.
|
|
69
|
-
|
|
70
|
-
This property is refreshed each time `refreshDevices()` is called.
|
|
71
|
-
|
|
72
|
-
### protectApi.sensors[]
|
|
73
|
-
The `sensors` property maintains the list of all known Protect sensor devices. It is an array of `ProtectSensorConfig` objects, and you can look through [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts) for a sense of what's contained in a `ProtectSensorConfig` object.
|
|
74
|
-
|
|
75
|
-
This property is refreshed each time `refreshDevices()` is called.
|
|
76
|
-
|
|
77
|
-
### protectApi.viewers[]
|
|
78
|
-
The `viewers` property maintains the list of all known Protect viewer devices. It is an array of `ProtectViewerConfig` objects, and you can look through [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/main/src/protect-types.ts) for a sense of what's contained in a `ProtectViewerConfig` object.
|
|
79
|
-
|
|
80
|
-
This property is refreshed each time `refreshDevices()` is called.
|
|
81
|
-
|
|
82
|
-
### More to come...
|
|
83
|
-
|
|
47
|
+
Documentation and examples for using this library to access UniFi Protect controllers is [available here](https://github.com/hjdhjd/unifi-protect/blob/main/docs/classes/ProtectApi.md). Additionally, if you'd like to see all this in action in a well-documented, real-world example, please take a good look at my [homebridge-unifi-protect](https://github.com/hjdhjd/homebridge-unifi-protect) project. It relies heavily on this library for the core functionality it provides.
|
|
84
48
|
|
|
85
49
|
## UniFi Protect Realtime Updates API
|
|
86
50
|
So...how does UniFi Protect provide realtime updates? On UniFi OS-based controllers, it uses a websocket called `updates`. This connection provides a realtime stream of health, status, and events that the cameras encounter - including motion events and doorbell ring events.
|
|
@@ -89,7 +53,7 @@ Reverse engineering the realtime updates API is a bit more difficult than the sy
|
|
|
89
53
|
|
|
90
54
|
The Protect realtime updates API, however, is a binary protocol published over the `updates` websocket, and until now has been undocumented. I spent time analyzing what's happening in the Protect browser webUI as well as observing the controller and various Protect versions themselves to reverse engineer what's going on. Pouring through obfuscated code is like solving a puzzle with all the pieces in front of you - you know it's all there, you're just not always sure how it fits together.
|
|
91
55
|
|
|
92
|
-
For the impatient, you can take a look at the code for how to decode and read the binary protocol here in [protect-updates-api.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-api-
|
|
56
|
+
For the impatient, you can take a look at the code for how to decode and read the binary protocol here in [protect-updates-api.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-api-events.ts) and the interface information located in [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-types.ts) as well.
|
|
93
57
|
|
|
94
58
|
I welcome any additions or corrections to the protocol for the benefit of the community. I hope this helps others launch their own exploration and create new and interesting Protect-enabled capabilities.
|
|
95
59
|
|
|
@@ -1,34 +1,49 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { ProtectCameraLcdMessagePayload } from "./protect-types.js";
|
|
3
2
|
import { ProtectLogging } from "./protect-logging.js";
|
|
3
|
+
/**
|
|
4
|
+
* UniFi Protect event packet.
|
|
5
|
+
*
|
|
6
|
+
* @param header - Protect event header.
|
|
7
|
+
* @param payload - Protect event payload.
|
|
8
|
+
*
|
|
9
|
+
* @remarks A UniFi Protect event packet represents a realtime event update from a UniFi Protect controller. There are two components to each packet, a `header` and
|
|
10
|
+
* a `payload`. The `header` contains information about which Protect device and what action category it belongs to. The `payload` contains the detailed information
|
|
11
|
+
* related to the device and action specified in the header.
|
|
12
|
+
*/
|
|
4
13
|
export type ProtectEventPacket = {
|
|
5
14
|
header: ProtectEventHeader;
|
|
6
15
|
payload: unknown;
|
|
7
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* UniFi Protect event header.
|
|
19
|
+
*
|
|
20
|
+
* @param header - Protect event header.
|
|
21
|
+
* @param payload - Protect event payload.
|
|
22
|
+
*
|
|
23
|
+
* @remarks A UniFi Protect event packet represents a realtime event update from a UniFi Protect controller. There are two components to each packet, a `header` and
|
|
24
|
+
* a `payload`. The `header` contains information about which Protect device and what action category it belongs to. The `payload` contains the detailed information
|
|
25
|
+
* related to the device and action specified in the header.
|
|
26
|
+
*/
|
|
8
27
|
export type ProtectEventHeader = {
|
|
9
28
|
action: string;
|
|
10
29
|
id: string;
|
|
11
30
|
modelKey: string;
|
|
12
31
|
newUpdateId: string;
|
|
13
32
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
modelKey: string;
|
|
18
|
-
partition: null;
|
|
19
|
-
score: number;
|
|
20
|
-
smartDetectEvents: string[];
|
|
21
|
-
smartDetectTypes: string[];
|
|
22
|
-
start: number;
|
|
23
|
-
type: string;
|
|
24
|
-
};
|
|
25
|
-
export type ProtectNvrUpdatePayloadCameraUpdate = {
|
|
26
|
-
isMotionDetected: boolean;
|
|
27
|
-
lastMotion: number;
|
|
28
|
-
lastRing: number;
|
|
29
|
-
lcdMessage: ProtectCameraLcdMessagePayload;
|
|
30
|
-
};
|
|
33
|
+
/**
|
|
34
|
+
* UniFi Protect event utility class that provides functions for decoding realtime event API packet frames.
|
|
35
|
+
*/
|
|
31
36
|
export declare class ProtectApiEvents {
|
|
37
|
+
/**
|
|
38
|
+
* Decode a UniFi Protect event packet.
|
|
39
|
+
*
|
|
40
|
+
* @param log - Logging functions to use.
|
|
41
|
+
* @param packet - Input packet to decode.
|
|
42
|
+
*
|
|
43
|
+
* @remarks A UniFi Protect event packet is an encoded representation of state updates that occur in a UniFi Protect controller. This utility function takes an
|
|
44
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. Events are generated automatically once a successful
|
|
45
|
+
* login has been made to a Protect controller and can be accessed by listening to `message` events emitted by an instance of {@link ProtectApi}.
|
|
46
|
+
*/
|
|
32
47
|
static decodePacket(log: ProtectLogging, packet: Buffer): ProtectEventPacket | null;
|
|
33
48
|
private static decodeFrame;
|
|
34
49
|
}
|
|
@@ -95,7 +95,45 @@ var ProtectEventPacketHeader;
|
|
|
95
95
|
ProtectEventPacketHeader[ProtectEventPacketHeader["UNKNOWN"] = 3] = "UNKNOWN";
|
|
96
96
|
ProtectEventPacketHeader[ProtectEventPacketHeader["PAYLOAD_SIZE"] = 4] = "PAYLOAD_SIZE";
|
|
97
97
|
})(ProtectEventPacketHeader || (ProtectEventPacketHeader = {}));
|
|
98
|
+
/*
|
|
99
|
+
// A complete description of the UniFi Protect realtime update events API payload packet JSONs.
|
|
100
|
+
// Payload JSON for modelKey: event action: add
|
|
101
|
+
type ProtectNvrUpdatePayloadEventAdd = {
|
|
102
|
+
|
|
103
|
+
camera: string,
|
|
104
|
+
id: string,
|
|
105
|
+
modelKey: string,
|
|
106
|
+
partition: null,
|
|
107
|
+
score: number,
|
|
108
|
+
smartDetectEvents: string[],
|
|
109
|
+
smartDetectTypes: string[],
|
|
110
|
+
start: number,
|
|
111
|
+
type: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Payload JSON for modelKey: camera action: update
|
|
115
|
+
type ProtectNvrUpdatePayloadCameraUpdate = {
|
|
116
|
+
isMotionDetected: boolean,
|
|
117
|
+
lastMotion: number,
|
|
118
|
+
lastRing: number,
|
|
119
|
+
lcdMessage: ProtectCameraLcdMessagePayload
|
|
120
|
+
}
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* UniFi Protect event utility class that provides functions for decoding realtime event API packet frames.
|
|
124
|
+
*/
|
|
125
|
+
// A utility class for decoding UniFi Protect events.
|
|
98
126
|
export class ProtectApiEvents {
|
|
127
|
+
/**
|
|
128
|
+
* Decode a UniFi Protect event packet.
|
|
129
|
+
*
|
|
130
|
+
* @param log - Logging functions to use.
|
|
131
|
+
* @param packet - Input packet to decode.
|
|
132
|
+
*
|
|
133
|
+
* @remarks A UniFi Protect event packet is an encoded representation of state updates that occur in a UniFi Protect controller. This utility function takes an
|
|
134
|
+
* encoded packet as an input, and decodes it into an event header and payload that can be acted upon. Events are generated automatically once a successful
|
|
135
|
+
* login has been made to a Protect controller and can be accessed by listening to `message` events emitted by an instance of {@link ProtectApi}.
|
|
136
|
+
*/
|
|
99
137
|
// Process an update data packet and return the action and payload.
|
|
100
138
|
static decodePacket(log, packet) {
|
|
101
139
|
// What we need to do here is to split this packet into the header and payload, and decode them.
|
|
@@ -110,7 +148,7 @@ export class ProtectApiEvents {
|
|
|
110
148
|
}
|
|
111
149
|
}
|
|
112
150
|
catch (error) {
|
|
113
|
-
log.error("Realtime
|
|
151
|
+
log.error("Realtime events API: error decoding update packet: %s.", error);
|
|
114
152
|
return null;
|
|
115
153
|
}
|
|
116
154
|
// Decode the action and payload frames now that we know where everything is.
|
|
@@ -150,7 +188,7 @@ export class ProtectApiEvents {
|
|
|
150
188
|
return payload;
|
|
151
189
|
break;
|
|
152
190
|
default:
|
|
153
|
-
log.error("Unknown payload packet type received in the realtime
|
|
191
|
+
log.error("Unknown payload packet type received in the realtime events API: %s.", payloadFormat);
|
|
154
192
|
return null;
|
|
155
193
|
break;
|
|
156
194
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"protect-api-events.js","sourceRoot":"","sources":["../src/protect-api-events.ts"],"names":[],"mappings":"AAKA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;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Aa,KAAK,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAuB,CAAC,CAAC,CAAC,IAAI,CAAC;SAChH;QAED,0CAA0C;QAC1C,QAAO,aAAa,EAAE;YAEpB,KAAK,gBAAgB,CAAC,IAAI;gBAExB,4CAA4C;gBAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAS,CAAC;gBAC9C,MAAM;YAER,KAAK,gBAAgB,CAAC,MAAM;gBAE1B,OAAO,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;gBAChC,MAAM;YAER,KAAK,gBAAgB,CAAC,MAAM;gBAE1B,OAAO,OAAO,CAAC;gBACf,MAAM;YAER;gBAEE,GAAG,CAAC,KAAK,CAAC,sEAAsE,EAAE,aAAa,CAAC,CAAC;gBACjG,OAAO,IAAI,CAAC;gBACZ,MAAM;SACT;IACH,CAAC;CACF"}
|
|
@@ -10,7 +10,7 @@ export declare class ProtectLivestream extends EventEmitter {
|
|
|
10
10
|
private log;
|
|
11
11
|
private segmentHandler;
|
|
12
12
|
private ws;
|
|
13
|
-
constructor(api: ProtectApi, log
|
|
13
|
+
constructor(api: ProtectApi, log: ProtectLogging);
|
|
14
14
|
start(cameraId: string, channel: number, lens?: number, segmentLength?: number, requestId?: string): Promise<boolean>;
|
|
15
15
|
stop(): void;
|
|
16
16
|
private launchLivestream;
|
|
@@ -53,8 +53,14 @@ var ProtectLiveFrame;
|
|
|
53
53
|
})(ProtectLiveFrame || (ProtectLiveFrame = {}));
|
|
54
54
|
// UniFi Protect livestream API implementation.
|
|
55
55
|
export class ProtectLivestream extends EventEmitter {
|
|
56
|
+
_initSegment;
|
|
57
|
+
api;
|
|
58
|
+
errorHandler;
|
|
59
|
+
log;
|
|
60
|
+
segmentHandler;
|
|
61
|
+
ws;
|
|
56
62
|
// Create a new instance.
|
|
57
|
-
constructor(api, log
|
|
63
|
+
constructor(api, log) {
|
|
58
64
|
// Initialize the event emitter.
|
|
59
65
|
super();
|
|
60
66
|
this._initSegment = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protect-api-livestream.js","sourceRoot":"","sources":["../src/protect-api-livestream.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,MAAM,EAAE,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGnD,OAAO,SAAS,MAAM,IAAI,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,4EAA4E;AAC5E,IAAK,gBAWJ;AAXD,WAAK,gBAAgB;IAEnB,iEAAc,CAAA;IACd,iFAAsB,CAAA;IACtB,yEAAkB,CAAA;IAClB,uEAAiB,CAAA;IACjB,yDAAU,CAAA;IACV,2DAAW,CAAA;IACX,2DAAW,CAAA;IACX,yDAAU,CAAA;IACV,qEAAgB,CAAA;AAClB,CAAC,EAXI,gBAAgB,KAAhB,gBAAgB,QAWpB;AAED,+CAA+C;AAC/C,MAAM,OAAO,iBAAkB,SAAQ,YAAY;
|
|
1
|
+
{"version":3,"file":"protect-api-livestream.js","sourceRoot":"","sources":["../src/protect-api-livestream.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,MAAM,EAAE,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGnD,OAAO,SAAS,MAAM,IAAI,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,4EAA4E;AAC5E,IAAK,gBAWJ;AAXD,WAAK,gBAAgB;IAEnB,iEAAc,CAAA;IACd,iFAAsB,CAAA;IACtB,yEAAkB,CAAA;IAClB,uEAAiB,CAAA;IACjB,yDAAU,CAAA;IACV,2DAAW,CAAA;IACX,2DAAW,CAAA;IACX,yDAAU,CAAA;IACV,qEAAgB,CAAA;AAClB,CAAC,EAXI,gBAAgB,KAAhB,gBAAgB,QAWpB;AAED,+CA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C;wBAC/B,MAAM;oBAER,uDAAuD;oBACvD,KAAK,gBAAgB,CAAC,IAAI;wBAExB,cAAc,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,cAAc,CAAC,IAAI,EAAE,IAAI,CAAE,CAAC,CAAC;wBACnE,MAAM;oBAER,uDAAuD;oBACvD,KAAK,gBAAgB,CAAC,IAAI;wBAExB,cAAc,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,cAAc,CAAC,IAAI,EAAE,IAAI,CAAE,CAAC,CAAC;wBACnE,MAAM;oBAER,uDAAuD;oBACvD,KAAK,gBAAgB,CAAC,KAAK;wBAEzB,cAAc,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAE,cAAc,CAAC,KAAK,EAAE,IAAI,CAAE,CAAC,CAAC;wBACrE,MAAM;oBAER,oEAAoE;oBACpE;wBAEE,MAAM;iBACT;gBAED,uCAAuC;gBACvC,MAAM,GAAG,gBAAgB,GAAG,aAAa,CAAC;aAC3C;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,sDAAsD;IAC/C,KAAK,CAAC,cAAc;QAEzB,yCAAyC;QACzC,IAAG,IAAI,CAAC,WAAW,EAAE;YAEnB,OAAO,IAAI,CAAC,WAAW,CAAC;SACzB;QAED,oEAAoE;QACpE,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,cAAc,EAAE,CAAC;IAC/B,CAAC;IAED,yDAAyD;IACzD,IAAW,WAAW;QAEpB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF"}
|
package/dist/protect-api.d.ts
CHANGED
|
@@ -1,48 +1,292 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { RequestOptions, Response } from "@adobe/fetch";
|
|
3
4
|
import { ProtectCameraConfig, ProtectCameraConfigInterface, ProtectCameraConfigPayload, ProtectChimeConfig, ProtectChimeConfigPayload, ProtectLightConfig, ProtectLightConfigPayload, ProtectNvrBootstrap, ProtectNvrConfig, ProtectNvrConfigPayload, ProtectSensorConfig, ProtectSensorConfigPayload, ProtectViewerConfig, ProtectViewerConfigPayload } from "./protect-types.js";
|
|
4
5
|
import { EventEmitter } from "node:events";
|
|
5
6
|
import { ProtectLivestream } from "./protect-api-livestream.js";
|
|
6
7
|
import { ProtectLogging } from "./protect-logging.js";
|
|
7
|
-
type ProtectKnownDeviceTypes = ProtectCameraConfig | ProtectChimeConfig | ProtectLightConfig | ProtectNvrConfig | ProtectSensorConfig | ProtectViewerConfig;
|
|
8
|
-
type ProtectKnownDevicePayloads = ProtectCameraConfigPayload | ProtectChimeConfigPayload | ProtectLightConfigPayload | ProtectNvrConfigPayload | ProtectSensorConfigPayload | ProtectViewerConfigPayload;
|
|
8
|
+
export type ProtectKnownDeviceTypes = ProtectCameraConfig | ProtectChimeConfig | ProtectLightConfig | ProtectNvrConfig | ProtectSensorConfig | ProtectViewerConfig;
|
|
9
|
+
export type ProtectKnownDevicePayloads = ProtectCameraConfigPayload | ProtectChimeConfigPayload | ProtectLightConfigPayload | ProtectNvrConfigPayload | ProtectSensorConfigPayload | ProtectViewerConfigPayload;
|
|
10
|
+
/**
|
|
11
|
+
* The UniFi Protect API is largely undocumented and has been reverse engineered mostly through
|
|
12
|
+
* the web interface, and trial and error.
|
|
13
|
+
*
|
|
14
|
+
* Here's how the UniFi Protect API works:
|
|
15
|
+
*
|
|
16
|
+
* 1. {@link login | Login} to the UniFi Protect controller and acquire security credentials for further calls to the API.
|
|
17
|
+
*
|
|
18
|
+
* 2. Enumerate the list of UniFi Protect devices by calling the {@link bootstrap} endpoint. This contains everything you would want to know about this particular
|
|
19
|
+
* UniFi Protect controller, including enumerating all the devices it knows about.
|
|
20
|
+
*
|
|
21
|
+
* 3. Listen for `message` events emitted by {@link ProtectApi} containing all Protect controller events, in realtime. They are delivered as {@link ProtectEventPacket}
|
|
22
|
+
* packets, containing the event-specific details.
|
|
23
|
+
*
|
|
24
|
+
* Those are the basics that gets us up and running.
|
|
25
|
+
*/
|
|
9
26
|
export declare class ProtectApi extends EventEmitter {
|
|
10
27
|
private _bootstrap;
|
|
11
28
|
private _eventsWs;
|
|
12
|
-
private _log;
|
|
13
29
|
private apiErrorCount;
|
|
14
30
|
private apiLastSuccess;
|
|
15
31
|
private fetch;
|
|
16
32
|
private headers;
|
|
17
33
|
private _isAdminUser;
|
|
34
|
+
private log;
|
|
18
35
|
private loginAge;
|
|
19
36
|
private nvrAddress;
|
|
20
37
|
private password;
|
|
21
38
|
private username;
|
|
39
|
+
/**
|
|
40
|
+
* Create an instance of the UniFi Protect API.
|
|
41
|
+
*
|
|
42
|
+
* @param log - Logging functions to use.
|
|
43
|
+
*
|
|
44
|
+
* @defaultValue `none` - Logging will be done to stdout and stderr.
|
|
45
|
+
*/
|
|
22
46
|
constructor(log?: ProtectLogging);
|
|
47
|
+
/**
|
|
48
|
+
* Execute a login attempt to the UniFi Protect API.
|
|
49
|
+
*
|
|
50
|
+
* @param nvrAddress - Address of the UniFi Protect controller, expressed as an FQDN or IP address.
|
|
51
|
+
* @param username - Username to use when logging into the controller.
|
|
52
|
+
* @param password - Password to use when logging into the controller.
|
|
53
|
+
*
|
|
54
|
+
* @returns Returns a promise that will resolve to `true` if successful and `false` otherwise.
|
|
55
|
+
*
|
|
56
|
+
* @remarks A `login` event will be emitted each time this method is called, with the result of the attempt as an argument.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* Login to the Protect controller. You can selectively choose to either `await` the promise that is returned by `login`, or subscribe to the `login` event.
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { ProtectApi } from "unifi-protect";
|
|
63
|
+
*
|
|
64
|
+
* // Create a new Protect API instance.
|
|
65
|
+
* const ufp = new ProtectApi();
|
|
66
|
+
*
|
|
67
|
+
* // Set a listener to wait for the login event to occur.
|
|
68
|
+
* ufp.once("login", (successfulLogin: boolean) => {
|
|
69
|
+
*
|
|
70
|
+
* // Indicate if we are successful.
|
|
71
|
+
* if(successfulLogin) {
|
|
72
|
+
*
|
|
73
|
+
* console.log("Logged in successfully.");
|
|
74
|
+
* process.exit(0);
|
|
75
|
+
* }
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* // Login to the Protect controller.
|
|
79
|
+
* if(!(await ufp.login("protect-controller.local", "username", "password"))) {
|
|
80
|
+
*
|
|
81
|
+
* console.log("Invalid login credentials.");
|
|
82
|
+
* process.exit(0);
|
|
83
|
+
* };
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
23
86
|
login(nvrAddress: string, username: string, password: string): Promise<boolean>;
|
|
24
87
|
private _acquireCsrfToken;
|
|
25
88
|
private loginController;
|
|
26
89
|
private bootstrapController;
|
|
27
90
|
private launchEventsWs;
|
|
91
|
+
/**
|
|
92
|
+
* Retrieve the bootstrap JSON from a UniFi Protect controller.
|
|
93
|
+
*
|
|
94
|
+
* @returns Returns a promise that will resolve to `true` if successful and `false` otherwise.
|
|
95
|
+
*
|
|
96
|
+
* @remarks A `bootstrap` event will be emitted each time this method is successfully called, with the {@link ProtectNvrBootstrap} JSON as an argument.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* Retrieve the bootstrap JSON. You can selectively choose to either `await` the promise that is returned by `getBootstrap`, or subscribe to the `bootstrap` event.
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { ProtectApi, ProtectNvrBootstrap } from "unifi-protect";
|
|
103
|
+
* import util from "node:util";
|
|
104
|
+
*
|
|
105
|
+
* // Create a new Protect API instance.
|
|
106
|
+
* const ufp = new ProtectApi();
|
|
107
|
+
*
|
|
108
|
+
* // Set a listener to wait for the bootstrap event to occur.
|
|
109
|
+
* ufp.once("bootstrap", (bootstrapJSON: ProtectNvrBootstrap) => {
|
|
110
|
+
*
|
|
111
|
+
* // Once we've bootstrapped the Protect controller, output the bootstrap JSON and we're done.
|
|
112
|
+
* process.stdout.write(util.inspect(bootstrapJSON, { colors: true, depth: null, sorted: true }) + "\n", () => process.exit(0));
|
|
113
|
+
* });
|
|
114
|
+
*
|
|
115
|
+
* // Login to the Protect controller.
|
|
116
|
+
* if(!(await ufp.login("protect-controller.local", "username", "password"))) {
|
|
117
|
+
*
|
|
118
|
+
* console.log("Invalid login credentials.");
|
|
119
|
+
* process.exit(0);
|
|
120
|
+
* };
|
|
121
|
+
*
|
|
122
|
+
* // Bootstrap the controller. It will emit a message once it's received the bootstrap JSON, or you can alternatively wait for the promise to resolve.
|
|
123
|
+
* if(!(await ufp.getBootstrap())) {
|
|
124
|
+
*
|
|
125
|
+
* console.log("Unable to bootstrap the Protect controller.");
|
|
126
|
+
* process.exit(0);
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* Alternatively, you can access the bootstrap JSON directly through the {@link bootstrap} accessor:
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { ProtectApi } from "unifi-protect";
|
|
134
|
+
* import util from "node:util";
|
|
135
|
+
*
|
|
136
|
+
* // Create a new Protect API instance.
|
|
137
|
+
* const ufp = new ProtectApi();
|
|
138
|
+
*
|
|
139
|
+
* // Login to the Protect controller.
|
|
140
|
+
* if(!(await ufp.login("protect-controller.local", "username", "password"))) {
|
|
141
|
+
*
|
|
142
|
+
* console.log("Invalid login credentials.");
|
|
143
|
+
* process.exit(0);
|
|
144
|
+
* };
|
|
145
|
+
*
|
|
146
|
+
* // Bootstrap the controller.
|
|
147
|
+
* if(!(await ufp.getBootstrap())) {
|
|
148
|
+
*
|
|
149
|
+
* console.log("Unable to bootstrap the Protect controller.");
|
|
150
|
+
* process.exit(0);
|
|
151
|
+
* }
|
|
152
|
+
*
|
|
153
|
+
* // Once we've bootstrapped the Protect controller, access the bootstrap JSON through the bootstrap accessor and we're done.
|
|
154
|
+
* process.stdout.write(util.inspect(ufp.bootstrap, { colors: true, depth: null, sorted: true }) + "\n", () => process.exit(0));
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
28
158
|
getBootstrap(): Promise<boolean>;
|
|
29
|
-
isAllRtspConfigured(): boolean;
|
|
30
159
|
private checkAdminUserStatus;
|
|
160
|
+
/**
|
|
161
|
+
* Retrieve a snapshot image from a Protect camera.
|
|
162
|
+
*
|
|
163
|
+
* @param device - Protect device.
|
|
164
|
+
* @param width - Optionally specify the image width to request. Defaults selected by the Protect controller, based on the camera resolution.
|
|
165
|
+
* @param height - Optionally specify the image height to request. Defaults selected by the Protect controller, based on the camera resolution.
|
|
166
|
+
* @param timestamp - Optionally specify the timestamp index to retrieve. Defaults to the current time.
|
|
167
|
+
* @param usePackageCamera - Optionally specify retrieving a snapshot fron the package camera, rather than the primary camera lens. Defaults to `false`.
|
|
168
|
+
*
|
|
169
|
+
* @returns Returns a promise that will resolve to a Buffer containing the JPEG image snapshot if successful, and `null` otherwise.
|
|
170
|
+
*/
|
|
171
|
+
getSnapshot(device: ProtectCameraConfig, width?: number, height?: number, timestamp?: number, usePackageCamera?: boolean): Promise<Buffer | null>;
|
|
172
|
+
/**
|
|
173
|
+
* Update a Protect device's configuration on the UniFi Protect controller.
|
|
174
|
+
*
|
|
175
|
+
* @typeParam DeviceType - Generic for any known Protect device type.
|
|
176
|
+
*
|
|
177
|
+
* @param device - Protect device.
|
|
178
|
+
* @param payload - Device configuration payload to upload, usually a subset of the device-specific configuration JSON.
|
|
179
|
+
*
|
|
180
|
+
* @returns Returns a promise that will resolve to the updated device-specific configuration JSON if successful, and `null` otherwise.
|
|
181
|
+
*
|
|
182
|
+
* @remarks Use this method to change the configuration of a given Protect device or controller. It requires the credentials used to login to the Protect API
|
|
183
|
+
* to have administrative privileges for most settings.
|
|
184
|
+
*/
|
|
31
185
|
updateDevice<DeviceType extends ProtectKnownDeviceTypes>(device: DeviceType, payload: ProtectKnownDevicePayloads): Promise<DeviceType | null>;
|
|
32
|
-
updateCameraChannels
|
|
186
|
+
private updateCameraChannels;
|
|
187
|
+
/**
|
|
188
|
+
* Utility method that enables all RTSP channels on a given Protect camera.
|
|
189
|
+
*
|
|
190
|
+
* @param device - Protect camera to modify.
|
|
191
|
+
*
|
|
192
|
+
* @returns Returns a promise that will resolve to the updated {@link ProtectCameraConfig} if successful, and `null` otherwise.
|
|
193
|
+
*/
|
|
33
194
|
enableRtsp(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
|
|
195
|
+
/**
|
|
196
|
+
* Utility method that generates a nicely formatted device information string.
|
|
197
|
+
*
|
|
198
|
+
* @param device - Protect device.
|
|
199
|
+
* @param name - Optional name for the device. Defaults to the device type (e.g. `G4 Pro`).
|
|
200
|
+
* @param deviceInfo - Optionally specify whether or not to include the IP address and MAC address in the returned string. Defaults to `false`.
|
|
201
|
+
*
|
|
202
|
+
* @returns Returns the Protect device name in the following format: <code>*Protect device name* [*Protect device type*] (address: *IP address* mac: *MAC address*)</code>.
|
|
203
|
+
*
|
|
204
|
+
* @remarks The example above assumed the `deviceInfo` parameter is set to `true`.
|
|
205
|
+
*/
|
|
34
206
|
getDeviceName(device: ProtectKnownDeviceTypes, name?: string, deviceInfo?: boolean): string;
|
|
207
|
+
/**
|
|
208
|
+
* Utility method that generates a combined, nicely formatted device and NVR string.
|
|
209
|
+
*
|
|
210
|
+
* @param device - Protect device.
|
|
211
|
+
*
|
|
212
|
+
* @returns Returns the Protect device name in the following format:
|
|
213
|
+
* <code>*Protect controller name* [*Protect controller type*] *Protect device name* [*Protect device type*]</code>.
|
|
214
|
+
*/
|
|
35
215
|
getFullName(device: ProtectKnownDeviceTypes): string;
|
|
216
|
+
/**
|
|
217
|
+
* Clear the login credentials and terminate any open connection to the UniFi Protect events API.
|
|
218
|
+
*/
|
|
36
219
|
clearLoginCredentials(): void;
|
|
37
220
|
private canModifyCamera;
|
|
221
|
+
/**
|
|
222
|
+
* Return a websocket API endpoint for the requested endpoint type.
|
|
223
|
+
*
|
|
224
|
+
* @param endpoint - Requested endpoint type. Valid types are `livestream` and `talkback`.
|
|
225
|
+
* @param params - Parameters to pass on for the endpoint request.
|
|
226
|
+
*
|
|
227
|
+
* @returns Returns a promise that will resolve to a URL to the requested endpoint if successful, and `null` otherwise.
|
|
228
|
+
*
|
|
229
|
+
* @remarks Valid API endpoints are `livestream` and `talkback`.
|
|
230
|
+
*
|
|
231
|
+
* * The `livestream` endpoint will return a URL to a websocket that provides an encoded livestream from a given camera. **Do not access this endpoint directly, use
|
|
232
|
+
* {@link createLivestream} instead.** Accessing the livestream endpoint directly is not directly useful without additional manipulation, which, unless you have
|
|
233
|
+
* a need for, you should avoid dealing with and use the {@link ProtectLivestream} API instead that provides you direct access to the livestream as an H.264 fMP4.
|
|
234
|
+
* * The `talkback` endpoint creates a talkback connection to a Protect camera that contains a speaker (e.g. Protect doorbells).
|
|
235
|
+
* The returned websocket accepts an AAC-encoded ADTS stream. The only valid parameter is `camera`, containing the ID of the Protect camera you want to connect to.
|
|
236
|
+
*/
|
|
38
237
|
getWsEndpoint(endpoint: "livestream" | "talkback", params?: URLSearchParams): Promise<string | null>;
|
|
39
238
|
private _getWsEndpoint;
|
|
40
|
-
|
|
239
|
+
/**
|
|
240
|
+
* Execute an HTTP fetch request to the Protect controller.
|
|
241
|
+
*
|
|
242
|
+
* @param url - Requested endpoint type. Valid types are `livestream` and `talkback`.
|
|
243
|
+
* @param options - Parameters to pass on for the endpoint request.
|
|
244
|
+
* @param logErrors - Log errors that aren't already accounted for and handled, rather than failing silently. Defaults to `true`.
|
|
245
|
+
*
|
|
246
|
+
* @returns Returns a promise that will resolve to a Response object successful, and `null` otherwise.
|
|
247
|
+
*
|
|
248
|
+
* @remarks This method should be used when direct access to the Protect controller is needed, or when this library doesn't have a needed method to access
|
|
249
|
+
* controller capabilities.
|
|
250
|
+
*/
|
|
251
|
+
retrieve(url: string, options?: RequestOptions, logErrors?: boolean): Promise<Response | null>;
|
|
252
|
+
private _retrieve;
|
|
253
|
+
private logRetry;
|
|
254
|
+
/**
|
|
255
|
+
* Return a new instance of the Protect livestream API.
|
|
256
|
+
*
|
|
257
|
+
* @returns Returns a new livestream API object.
|
|
258
|
+
*
|
|
259
|
+
* @remarks This method should be used to create a new livestream API object. It allows you to create access livestreams of individual cameras and interact
|
|
260
|
+
* directly with the H.264 fMP4 streams for a given camera.
|
|
261
|
+
*/
|
|
41
262
|
createLivestream(): ProtectLivestream;
|
|
42
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Return an API endpoint for the requested endpoint type.
|
|
265
|
+
*
|
|
266
|
+
* @param endpoint - Requested endpoint type.
|
|
267
|
+
*
|
|
268
|
+
* @returns Returns a URL to the requested endpoint if successful, and an empty string otherwise.
|
|
269
|
+
*
|
|
270
|
+
* @remarks Valid API endpoints are `bootstrap`, `camera`, `chime`, `light`, `login`, `nvr`, `self`, `sensor`, `websocket` and `viewer`.
|
|
271
|
+
*/
|
|
272
|
+
getApiEndpoint(endpoint: string): string;
|
|
273
|
+
/**
|
|
274
|
+
* Access the Protect controller bootstrap JSON.
|
|
275
|
+
*
|
|
276
|
+
* @returns Returns the bootstrap JSON if the Protect controller has been bootstrapped, `null` otherwise.
|
|
277
|
+
*/
|
|
43
278
|
get bootstrap(): ProtectNvrBootstrap | null;
|
|
279
|
+
/**
|
|
280
|
+
* Utility method that returns whether the credentials that were used to login to the Protect controller have administrative privileges or not.
|
|
281
|
+
*
|
|
282
|
+
* @returns Returns `true` if the logged in user has administrative privileges, `false` otherwise.
|
|
283
|
+
*/
|
|
44
284
|
get isAdminUser(): boolean;
|
|
45
|
-
|
|
285
|
+
/**
|
|
286
|
+
* Utility method that returns a nicely formatted version of the Protect controller name.
|
|
287
|
+
*
|
|
288
|
+
* @returns Returns the Protect controller name in the following format:
|
|
289
|
+
* <code>*Protect controller name* [*Protect controller type*]</code>.
|
|
290
|
+
*/
|
|
46
291
|
get name(): string;
|
|
47
292
|
}
|
|
48
|
-
export {};
|