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 CHANGED
@@ -59,11 +59,25 @@ This is where the magic happens. This function:
59
59
 
60
60
  Returns: `true` if successful, `false` otherwise.
61
61
 
62
- ### protectApi.Cameras[]
63
- The `Cameras` property maintains the list of all known Protect 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.
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
64
 
65
65
  This property is refreshed each time `refreshDevices()` is called.
66
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.
67
81
 
68
82
  ### More to come...
69
83
 
@@ -154,7 +168,7 @@ The final part of the update packet is the data frame. The data frame can be thr
154
168
  ## Library Development Dashboard
155
169
  This is mostly of interest to the true developer nerds amongst us.
156
170
 
157
- [![License](https://img.shields.io/npm/l/@hjdhjd/unifi-protect?color=%230559C9&logo=open%20source%20initiative&logoColor=%23FFFFFF&style=for-the-badge)](https://github.com/hjdhjd/unifi-protect/blob/main/LICENSE.md)
171
+ [![License](https://img.shields.io/npm/l/unifi-protect?color=%230559C9&logo=open%20source%20initiative&logoColor=%23FFFFFF&style=for-the-badge)](https://github.com/hjdhjd/unifi-protect/blob/main/LICENSE.md)
158
172
  [![Build Status](https://img.shields.io/github/workflow/status/hjdhjd/unifi-protect/Continuous%20Integration?color=%230559C9&logo=github-actions&logoColor=%23FFFFFF&style=for-the-badge)](https://github.com/hjdhjd/unifi-protect/actions?query=workflow%3A%22Continuous+Integration%22)
159
- [![Dependencies](https://img.shields.io/librariesio/release/npm/@hjdhjd/unifi-protect?color=%230559C9&logo=dependabot&style=for-the-badge)](https://libraries.io/npm/@hjdhjd/uniti-protect)
173
+ [![Dependencies](https://img.shields.io/librariesio/release/npm/unifi-protect?color=%230559C9&logo=dependabot&style=for-the-badge)](https://libraries.io/npm/uniti-protect)
160
174
  [![GitHub commits since latest release (by SemVer)](https://img.shields.io/github/commits-since/hjdhjd/unifi-protect/latest?color=%230559C9&logo=github&sort=semver&style=for-the-badge)](https://github.com/hjdhjd/unifi-protect/commits/main)
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./protect-api";
2
+ export * from "./protect-api-livestream";
2
3
  export * from "./protect-api-updates";
4
+ export * from "./protect-logging";
3
5
  export * from "./protect-types";
4
6
  export { FetchError } from "node-fetch-cjs";
package/dist/index.js CHANGED
@@ -16,7 +16,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.FetchError = void 0;
18
18
  __exportStar(require("./protect-api"), exports);
19
+ __exportStar(require("./protect-api-livestream"), exports);
19
20
  __exportStar(require("./protect-api-updates"), exports);
21
+ __exportStar(require("./protect-logging"), exports);
20
22
  __exportStar(require("./protect-types"), exports);
21
23
  var node_fetch_cjs_1 = require("node-fetch-cjs");
22
24
  Object.defineProperty(exports, "FetchError", { enumerable: true, get: function () { return node_fetch_cjs_1.FetchError; } });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;AAEH,gDAA8B;AAC9B,wDAAsC;AACtC,kDAAgC;AAChC,iDAA4C;AAAnC,4GAAA,UAAU,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;AAEH,gDAA8B;AAC9B,2DAAyC;AACzC,wDAAsC;AACtC,oDAAkC;AAClC,kDAAgC;AAChC,iDAA4C;AAAnC,4GAAA,UAAU,OAAA"}
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from "events";
3
+ import { ProtectApi } from "./protect-api";
4
+ import { ProtectLogging } from "./protect-logging";
5
+ export declare class ProtectLivestream extends EventEmitter {
6
+ private _ftyp;
7
+ private _moov;
8
+ private api;
9
+ private errorHandler;
10
+ private log;
11
+ private name;
12
+ private segmentHandler;
13
+ private ws;
14
+ constructor(api: ProtectApi, log?: ProtectLogging);
15
+ start(cameraId: string, channel: number, segmentLength?: number, requestId?: string): Promise<boolean>;
16
+ stop(): void;
17
+ private launchLivestream;
18
+ private processLivestream;
19
+ get ftyp(): Buffer | null;
20
+ get moov(): Buffer | null;
21
+ }
@@ -0,0 +1,294 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ProtectLivestream = void 0;
7
+ /* Copyright(C) 2022, HJD (https://github.com/hjdhjd). All rights reserved.
8
+ *
9
+ * protect-api-livestream.ts: Our UniFi Protect livestream API implementation.
10
+ */
11
+ const events_1 = require("events");
12
+ const ws_1 = __importDefault(require("ws"));
13
+ /*
14
+ * The UniFi Protect livestream API is largely undocumented and has been reverse engineered mostly through
15
+ * trial and error, as well as observing the Protect controller in action. It builds on the works of others in the
16
+ * community - particularly https://github.com/XciD - who have experimented and successfully gotten parts of this API decoded.
17
+ * As always, this work stands on the contributions of others and the work that's come before it, and I want to acknowledge those
18
+ * that paved the way.
19
+ *
20
+ * Let's start by defining some terms. In the MP4 world, an MP4 file (or stream) is composed of multiple atoms or segments.
21
+ * Think of these as packet types that contain specific pieces of information that are needed to put together a valid MP4 file.
22
+ * For our purposes, we're primarily interested in four types of MP4 boxes:
23
+ *
24
+ * FTYP - File type box. This contains codec and file information for the stream that follows it.
25
+ * It must be at the beginning of any stream, and preceded by the FTYP box.
26
+ * MDAT - Media data box. This contains a segment of the actual audio and video data in the MP4 stream.
27
+ * It is always paired with an MOOF box, which contains the metadata describing this payload in an MDAT box.
28
+ * MOOF - Movie fragment box. This defines the metadata for a specific segment of audio and video.
29
+ * It is always paired with an MDAT box, which contains the actual data.
30
+ * MOOV - Movie metadata box. This contains all the metadata information about the stream that follows.
31
+ * It must be at the beginning of any stream, and preceded by the FTYP box.
32
+ *
33
+ * Every fMP4 stream begins with the FTYP and MOOV boxes for the entire stream. It defines the file type,
34
+ * what the movie metadata is, and other characteristics. Think of it as the header for the entire stream.
35
+ *
36
+ * After the header, every fMP4 stream has a series of segments (sometimes called fragments, hence the term fMP4),
37
+ * that consist of a pair of moof / mdat boxes that includes all the audio and video for that segment. You end up with something
38
+ * that looks like:
39
+ *
40
+ * |ftyp|moov|moof|mdat|moof|mdat...
41
+ *
42
+ * The UniFi Protect livestream API provides a straightforward interface to generate bespoke fMP4 streams that
43
+ * can be tailored depending on your needs. This API allows you to create those streams, and retrieve all the relevant boxes/atoms
44
+ * you need to manipulate them for your application.
45
+ */
46
+ // A complete description of the UniFi Protect livestream API websocket API.
47
+ var ProtectLiveFrame;
48
+ (function (ProtectLiveFrame) {
49
+ ProtectLiveFrame[ProtectLiveFrame["KEYFRAME"] = 247] = "KEYFRAME";
50
+ ProtectLiveFrame[ProtectLiveFrame["FTYP"] = 248] = "FTYP";
51
+ ProtectLiveFrame[ProtectLiveFrame["BEGINSEGMENT"] = 249] = "BEGINSEGMENT";
52
+ ProtectLiveFrame[ProtectLiveFrame["MOOV"] = 250] = "MOOV";
53
+ ProtectLiveFrame[ProtectLiveFrame["MOOF"] = 251] = "MOOF";
54
+ ProtectLiveFrame[ProtectLiveFrame["VIDEO"] = 252] = "VIDEO";
55
+ ProtectLiveFrame[ProtectLiveFrame["AUDIO"] = 253] = "AUDIO";
56
+ ProtectLiveFrame[ProtectLiveFrame["MDAT"] = 254] = "MDAT";
57
+ ProtectLiveFrame[ProtectLiveFrame["ENDSEGMENT"] = 255] = "ENDSEGMENT";
58
+ })(ProtectLiveFrame || (ProtectLiveFrame = {}));
59
+ // UniFi Protect livestream API implementation.
60
+ class ProtectLivestream extends events_1.EventEmitter {
61
+ // Create a new instance.
62
+ constructor(api, log = api.log) {
63
+ // Initialize the event emitter.
64
+ super();
65
+ this._ftyp = null;
66
+ this._moov = null;
67
+ this.api = api;
68
+ this.errorHandler = null;
69
+ this.log = log;
70
+ this.name = api.getNvrName.bind(api);
71
+ this.segmentHandler = null;
72
+ this.ws = null;
73
+ }
74
+ // Start the UniFi Protect livestream.
75
+ async start(cameraId, channel, segmentLength = 100, requestId = cameraId + "-" + channel.toString()) {
76
+ // Stop any existing stream.
77
+ this.stop();
78
+ // Clear out the movie metadata atom.
79
+ this._ftyp = null;
80
+ this._moov = null;
81
+ // Launch the livestream.
82
+ return await this.launchLivestream(cameraId, channel, segmentLength, requestId);
83
+ }
84
+ // Stop the UniFi Protect livestream.
85
+ stop() {
86
+ var _a, _b, _c, _d, _e;
87
+ // Close the websocket.
88
+ if ((((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CLOSING) || (((_b = this.ws) === null || _b === void 0 ? void 0 : _b.readyState) === ws_1.default.OPEN)) {
89
+ (_c = this.ws) === null || _c === void 0 ? void 0 : _c.terminate();
90
+ }
91
+ // Clean up our segment processing handler.
92
+ if (this.errorHandler) {
93
+ (_d = this.ws) === null || _d === void 0 ? void 0 : _d.removeListener("error", this.errorHandler);
94
+ this.errorHandler = null;
95
+ }
96
+ if (this.segmentHandler) {
97
+ (_e = this.ws) === null || _e === void 0 ? void 0 : _e.removeListener("message", this.segmentHandler);
98
+ this.segmentHandler = null;
99
+ }
100
+ // Flag that we are no longer running.
101
+ this.ws = null;
102
+ }
103
+ // Configure the websocket to populate the prebuffer.
104
+ async launchLivestream(cameraId, channel, segmentLength, requestId) {
105
+ var _a;
106
+ // To ensure there are minimal performance implications to the Protect NVR, enforce a 100ms floor for
107
+ // segment length. Protect happens to default to a 100ms segment length as well, so we do too.
108
+ if (segmentLength < 100) {
109
+ segmentLength = 100;
110
+ }
111
+ // Parameters that can be set for the livestream. We allow the modification of a useful subset of these,
112
+ // though not all of them, in order to simplify the API experience and ensure things always work.
113
+ //
114
+ // allowPartialGOP: Allow partial groups of pictures. This is necessary for a valid fMP4 stream that can be used in realtime.
115
+ // camera: The camera ID of the camera you are trying to livestream.
116
+ // channel: The camera channel to use for this livestream.
117
+ // extendedVideoMetadata: Provide extended metadata in the MOOV box when possible.
118
+ // fragmentDurationMillis: Length of each fMP4 segment or fragment, in milliseconds.
119
+ // progressive: Enable progressive livestreaming.
120
+ // rebaseTimestampsToZero: Rebase the timestamps of each segment to zero. Otherwise, timestamps will reflect the controller's default.
121
+ // requestId: Name for this particular request. It's optional in practice, and can be any string.
122
+ // type: Container format type. The valid values are fmp4 and UBV (UniFi Video proprietary format).
123
+ const params = new URLSearchParams({
124
+ allowPartialGOP: "",
125
+ camera: cameraId,
126
+ channel: channel.toString(),
127
+ extendedVideoMetadata: "",
128
+ fragmentDurationMillis: segmentLength.toString(),
129
+ progressive: "",
130
+ rebaseTimestampsToZero: "false",
131
+ requestId: requestId,
132
+ type: "fmp4"
133
+ });
134
+ // Generate our websocket endpoint URL.
135
+ const url = this.api.wsUrl() + "/livestream?" + params.toString();
136
+ this.log.debug("%s: Opening livestream websocket URL: %s", this.name(), url);
137
+ // Get the websocket.
138
+ const wsUrl = await this.api.getWsEndpoint(url);
139
+ // We ran into a problem getting the websocket URL. We're done.
140
+ if (!wsUrl) {
141
+ this.log.error("%s: Unable to retrieve the livestream websocket API endpoint from the UniFi Protect controller.", this.name());
142
+ return false;
143
+ }
144
+ try {
145
+ // Open the livestream websocket.
146
+ this.ws = new ws_1.default(wsUrl, { rejectUnauthorized: false });
147
+ if (!this.ws) {
148
+ this.ws = null;
149
+ this.log.error("%s: Unable to connect to the livestream websocket API endpoint.", this.name());
150
+ return false;
151
+ }
152
+ // Catch any errors and inform the user, if needed.
153
+ (_a = this.ws) === null || _a === void 0 ? void 0 : _a.once("error", this.errorHandler = (error) => {
154
+ // Ignore timeout errors, but notify the user about anything else.
155
+ if (error.code !== "ETIMEDOUT") {
156
+ this.log.error("%s: Error while communicating with the livestream websocket API: %s", this.name(), error);
157
+ }
158
+ this.stop();
159
+ });
160
+ // Process packets coming to our websocket.
161
+ this.processLivestream();
162
+ }
163
+ catch (error) {
164
+ this.log.error("%s: Error while connecting to the livestream websocket API: %s", this.name(), error);
165
+ this.stop();
166
+ }
167
+ return true;
168
+ }
169
+ // Process fMP4 packets as they arrive over the websocket.
170
+ processLivestream() {
171
+ // Check to ensure our websocket is live.
172
+ if (!this.ws) {
173
+ return;
174
+ }
175
+ let currentSegment = {
176
+ audio: Buffer.alloc(0),
177
+ keyframe: Buffer.alloc(0),
178
+ mdat: Buffer.alloc(0),
179
+ moof: Buffer.alloc(0),
180
+ video: Buffer.alloc(0)
181
+ };
182
+ let packetRemaining = Buffer.alloc(0);
183
+ // Process data coming in from the websocket.
184
+ this.ws.on("message", this.segmentHandler = (packet) => {
185
+ // If we have anything left from the last packet we processed, prepend it to this packet.
186
+ if (packetRemaining.length > 0) {
187
+ packet = Buffer.concat([packetRemaining, packet]);
188
+ packetRemaining = Buffer.alloc(0);
189
+ }
190
+ let offset = 0;
191
+ for (;;) {
192
+ // If we have less than 4 bytes remaining, it's an incomplete packet and we don't have enough
193
+ // information to decode it since we need the packet header to decode. We save it to prepend
194
+ // to the next packet that comes across.
195
+ if ((packet.length - offset) < 4) {
196
+ packetRemaining = packet.slice(offset);
197
+ break;
198
+ }
199
+ // Grab the encoded packet header.
200
+ const offsetWithHeader = offset + 4;
201
+ const header = packet.slice(offset, offsetWithHeader);
202
+ // Ensure we have a valid header before we do anything.
203
+ if (!(header[0] in ProtectLiveFrame)) {
204
+ this.log.error("%s: Invalid header found while decoding the livestream: %s", this.name(), header[0]);
205
+ break;
206
+ }
207
+ // Get the length of the actual fMP4 segment we are decoding. Since all this is done over a websocket,
208
+ // portions of an fMP4 segment can span packets and need to be encoded. Protect encodes the length
209
+ // of the entire fMP4 segment in the first three bytes of the header like so...
210
+ const segmentLength = (((header[1] << 8) | header[2]) << 8) | header[3];
211
+ // Once we know our length, if we don't have the complete packet, we save it and punt until we see
212
+ // more data come across.
213
+ if (packet.length < (offset + segmentLength + 4)) {
214
+ packetRemaining = packet.slice(offset);
215
+ break;
216
+ }
217
+ // Grab the data portion of the packet.
218
+ const data = packet.slice(offsetWithHeader, offsetWithHeader + segmentLength);
219
+ let completeSegment = null;
220
+ // Figure out which data type we've got based on our header, and process it.
221
+ switch (header[0]) {
222
+ // We've got audio data. Add it to our current segment.
223
+ case ProtectLiveFrame.AUDIO:
224
+ currentSegment.audio = Buffer.concat([currentSegment.audio, data]);
225
+ break;
226
+ // End of segment. Build the entire segment, and emit our events.
227
+ case ProtectLiveFrame.ENDSEGMENT:
228
+ completeSegment = Buffer.concat([currentSegment.moof, currentSegment.mdat, currentSegment.video, currentSegment.audio]);
229
+ this.emit("segment", completeSegment);
230
+ this.emit("message", completeSegment);
231
+ break;
232
+ // FTYP data. Build the FTYP box and emit our events.
233
+ case ProtectLiveFrame.FTYP:
234
+ // Write the ftyp header. The first four bytes represent the length of this atom, the next four designate the atom type.
235
+ this._ftyp = Buffer.alloc(8);
236
+ this._ftyp.writeUInt32BE(data.length + 8);
237
+ this._ftyp.write("ftyp", 4);
238
+ // Create the complete atom.
239
+ this._ftyp = Buffer.concat([this._ftyp, data]);
240
+ this.emit("ftyp", this.ftyp);
241
+ this.emit("message", this.ftyp);
242
+ break;
243
+ // Beginning of segment. Create an empty segment to get started.
244
+ case ProtectLiveFrame.BEGINSEGMENT:
245
+ currentSegment = {
246
+ audio: Buffer.alloc(0),
247
+ keyframe: Buffer.alloc(0),
248
+ mdat: Buffer.alloc(0),
249
+ moof: Buffer.alloc(0),
250
+ video: Buffer.alloc(0)
251
+ };
252
+ break;
253
+ // We've got a keyframe. Add it to our current segment.
254
+ case ProtectLiveFrame.KEYFRAME:
255
+ currentSegment.keyframe = data;
256
+ break;
257
+ // MDAT box. Add it to the MDAT portion of our segment.
258
+ case ProtectLiveFrame.MDAT:
259
+ currentSegment.mdat = Buffer.concat([currentSegment.mdat, data]);
260
+ break;
261
+ // MOOF box. Add it to the MOOF portion of our segment.
262
+ case ProtectLiveFrame.MOOF:
263
+ currentSegment.moof = Buffer.concat([currentSegment.moof, data]);
264
+ break;
265
+ // MOOV box. Add it to the MOOV portion of our segment and emit our events.
266
+ case ProtectLiveFrame.MOOV:
267
+ this._moov = data;
268
+ this.emit("moov", this.moov);
269
+ this.emit("message", this.moov);
270
+ break;
271
+ // We've got video data. Add it to our current segment.
272
+ case ProtectLiveFrame.VIDEO:
273
+ currentSegment.video = Buffer.concat([currentSegment.video, data]);
274
+ break;
275
+ // We'll never get here since we check our header information above.
276
+ default:
277
+ break;
278
+ }
279
+ // Move the offset to the next segment.
280
+ offset = offsetWithHeader + segmentLength;
281
+ }
282
+ });
283
+ }
284
+ // Retrieve the FTYP box, if we've seen it.
285
+ get ftyp() {
286
+ return this._ftyp;
287
+ }
288
+ // Retrieve the MOOV box, if we've seen it.
289
+ get moov() {
290
+ return this._moov;
291
+ }
292
+ }
293
+ exports.ProtectLivestream = ProtectLivestream;
294
+ //# sourceMappingURL=protect-api-livestream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protect-api-livestream.js","sourceRoot":"","sources":["../src/protect-api-livestream.ts"],"names":[],"mappings":";;;;;;AAAA;;;GAGG;AACH,mCAAsC;AAGtC,4CAA2B;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,4EAA4E;AAC5E,IAAK,gBAWJ;AAXD,WAAK,gBAAgB;IAEnB,iEAAc,CAAA;IACd,yDAAU,CAAA;IACV,yEAAkB,CAAA;IAClB,yDAAU,CAAA;IACV,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,MAAa,iBAAkB,SAAQ,qBAAY;IAWjD,yBAAyB;IACzB,YAAY,GAAe,EAAE,GAAG,GAAG,GAAG,CAAC,GAAG;QAExC,gCAAgC;QAChC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;QAC3B,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC;IACjB,CAAC;IAED,sCAAsC;IAC/B,KAAK,CAAC,KAAK,CAAC,Q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,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;wBAC7B,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;wBAChC,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,2CAA2C;IAC3C,IAAW,IAAI;QAEb,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,2CAA2C;IAC3C,IAAW,IAAI;QAEb,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF;AAnUD,8CAmUC"}
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { ProtectCameraLcdMessagePayload } from "./protect-types";
3
- import { protectLogging } from "./protect-logging";
3
+ import { ProtectLogging } from "./protect-logging";
4
4
  declare type ProtectNvrUpdatePacket = {
5
5
  action: ProtectNvrUpdateEventAction;
6
6
  payload: Record<string, unknown> | string | Buffer;
@@ -29,7 +29,7 @@ export declare type ProtectNvrUpdatePayloadCameraUpdate = {
29
29
  lcdMessage: ProtectCameraLcdMessagePayload;
30
30
  };
31
31
  export declare class ProtectApiUpdates {
32
- static decodeUpdatePacket(log: protectLogging, packet: Buffer): ProtectNvrUpdatePacket | null;
32
+ static decodeUpdatePacket(log: ProtectLogging, packet: Buffer): ProtectNvrUpdatePacket | null;
33
33
  private static decodeUpdateFrame;
34
34
  }
35
35
  export {};
@@ -1,43 +1,64 @@
1
- import { ProtectCameraConfig, ProtectCameraConfigInterface, ProtectCameraConfigPayload, ProtectNvrBootstrap } from "./protect-types";
1
+ import { ProtectCameraConfig, ProtectCameraConfigInterface, ProtectCameraConfigPayload, ProtectLightConfig, ProtectLightConfigPayload, ProtectNvrBootstrap, ProtectSensorConfig, ProtectViewerConfig, ProtectViewerConfigPayload } from "./protect-types";
2
2
  import { RequestInfo, RequestInit, Response } from "node-fetch-cjs";
3
+ import { ProtectLogging } from "./protect-logging";
3
4
  import WebSocket from "ws";
4
- import { protectLogging } from "./protect-logging";
5
+ declare type ProtectKnownDeviceTypes = ProtectCameraConfig | ProtectLightConfig | ProtectSensorConfig | ProtectViewerConfig;
5
6
  export declare class ProtectApi {
7
+ private _bootstrap;
8
+ private _cameras;
9
+ private _eventsWs;
10
+ private _lights;
11
+ private _log;
12
+ private _sensors;
13
+ private _viewers;
6
14
  private apiErrorCount;
7
15
  private apiLastSuccess;
8
- bootstrap: ProtectNvrBootstrap | null;
9
- Cameras: ProtectCameraConfig[] | undefined;
10
- private eventHeartbeatTimer;
11
- eventListener: WebSocket | null;
12
- eventListenerConfigured: boolean;
13
16
  private headers;
14
17
  private httpsAgent;
15
- isAdminUser: boolean;
16
- private log;
17
- private loggedIn;
18
+ private isAdminUser;
19
+ private isLoggedIn;
20
+ private isLoginRefresh;
18
21
  private loginAge;
22
+ private loginAttempt;
19
23
  private nvrAddress;
20
24
  private password;
21
25
  private username;
22
- constructor(nvrAddress: string, username: string, password: string, log?: protectLogging);
26
+ constructor(nvrAddress: string, username: string, password: string, log?: ProtectLogging);
23
27
  private acquireToken;
28
+ private loginNvr;
24
29
  private login;
25
30
  private bootstrapProtect;
26
31
  private launchUpdatesListener;
32
+ private refreshDeviceClass;
27
33
  refreshDevices(): Promise<boolean>;
28
34
  isAllRtspConfigured(): boolean;
29
35
  private checkAdminUserStatus;
30
- enableRtsp(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
31
36
  updateCamera(device: ProtectCameraConfig, payload: ProtectCameraConfigPayload): Promise<ProtectCameraConfig | null>;
37
+ updateCameraChannels(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
38
+ updateLight(device: ProtectLightConfig, payload: ProtectLightConfigPayload): Promise<ProtectLightConfig | null>;
39
+ updateViewer(device: ProtectViewerConfig, payload: ProtectViewerConfigPayload): Promise<ProtectViewerConfig | null>;
40
+ enableRtsp(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
41
+ get bootstrap(): ProtectNvrBootstrap | null;
42
+ get cameras(): ProtectCameraConfig[] | null;
43
+ get eventsWs(): WebSocket | null;
44
+ get lights(): ProtectLightConfig[] | null;
45
+ get log(): ProtectLogging;
46
+ get sensors(): ProtectSensorConfig[] | null;
47
+ get viewers(): ProtectViewerConfig[] | null;
32
48
  getNvrName(): string;
33
- getDeviceName(camera: ProtectCameraConfig, name?: string, cameraInfo?: boolean): string;
34
- getFullName(camera: ProtectCameraConfig): string;
49
+ getDeviceName(device: ProtectKnownDeviceTypes, name?: string, deviceInfo?: boolean): string;
50
+ getFullName(device: ProtectKnownDeviceTypes): string;
35
51
  camerasUrl(): string;
52
+ lightsUrl(): string;
53
+ viewersUrl(): string;
54
+ wsUrl(): string;
36
55
  private authUrl;
37
56
  private bootstrapUrl;
38
57
  private systemUrl;
39
58
  private updatesUrl;
40
- private heartbeatEventListener;
41
59
  clearLoginCredentials(): void;
60
+ private canModifyCamera;
61
+ getWsEndpoint(url: string): Promise<string | null>;
42
62
  fetch(url: RequestInfo, options?: RequestInit, logErrors?: boolean, decodeResponse?: boolean, isRetry?: boolean): Promise<Response | null>;
43
63
  }
64
+ export {};