unifi-protect 0.0.4 → 2.1.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/LICENSE.md ADDED
@@ -0,0 +1,16 @@
1
+ Internet Systems Consortium license
2
+ ===================================
3
+
4
+ Copyright (c) `2017-2022`, `HJD https://github.com/hjdhjd`
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any purpose
7
+ with or without fee is hereby granted, provided that the above copyright notice
8
+ and this permission notice appear in all copies.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
16
+ THIS SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,174 @@
1
- # unifi-protect
1
+ <SPAN ALIGN="CENTER" STYLE="text-align:center">
2
+ <DIV ALIGN="CENTER" STYLE="text-align:center">
2
3
 
3
- Unifi Protect API. Imported from https://github.com/hjdhjd/homebridge-unifi-protect.
4
+ [![unifi-protect: UniFi Protect API](https://raw.githubusercontent.com/hjdhjd/unifi-protect/main/unifi-protect-logo.svg)](https://github.com/hjdhjd/unifi-protect)
5
+
6
+ # UniFi Protect API
7
+
8
+ [![Downloads](https://img.shields.io/npm/dt/unifi-protect?color=%230559C9&logo=icloud&logoColor=%23FFFFFF&style=for-the-badge)](https://www.npmjs.com/package/unifi-protect)
9
+ [![Version](https://img.shields.io/npm/v/unifi-protect?color=%230559C9&label=UniFi%20Protect%20API&logo=ubiquiti&logoColor=%23FFFFFF&style=for-the-badge)](https://www.npmjs.com/package/unifi-protect)
10
+
11
+ ## A complete UniFi Protect API implementation.
12
+ </DIV>
13
+ </SPAN>
14
+
15
+ `unifi-protect` is a library that enabled you to connect to and communicate with the Ubiquiti UniFi Protect API and ecosystem. [UniFi Protect](https://ui.com/camera-security) is [Ubiquiti's](https://www.ui.com) next-generation video security platform, with rich camera, doorbell, and NVR controller hardware options for you to choose from, as well as an app which you can use to view, configure and manage your video camera and doorbells.
16
+
17
+ ## Why use this library for UniFi Protect support?
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
+
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.
21
+
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
+
24
+ ### <A NAME="protect-contribute"></A>How you can contribute and make this library even better
25
+ This implementation is largely feature complete. I strive to add support for meaningful features to a broad groups of people in order to avoid any unnecessary cruft and technical debt that may accrue over time.
26
+
27
+ The UniFi Protect API is undocumented and implementing a library like this one is the result of many hours of trial and error as well as community support.
28
+
29
+ ### Features
30
+ - Full access to the UniFi Protect NVR JSON.
31
+ - The ability to retrieve the JSON details, including status, of any supported UniFi Protect device.
32
+ - The ability to modify the Protect NVR JSON or Protect devices.
33
+
34
+ ## Changelog
35
+ * [Changelog](https://github.com/hjdhjd/unifi-protect/blob/main/docs/Changelog.md): changes and release history of this library.
36
+
37
+ ## Installation
38
+ To use this library in Node, install it from the command line:
39
+
40
+ ```sh
41
+ npm install unifi-protect
42
+ ```
43
+
44
+ ## Documentation
45
+
46
+ 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.
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
+
84
+
85
+ ## UniFi Protect Realtime Updates API
86
+ 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.
87
+
88
+ Reverse engineering the realtime updates API is a bit more difficult than the system events API because it's based on a binary protocol. The Protect system events API is a steady stream of JSONs published on all UniFi OS controllers over the `system` websocket. It's used by more than just UniFi Protect, which makes it interesting for future exploration.
89
+
90
+ 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
+
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-updates.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
+
94
+ 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
+
96
+ #### Non-Ubiquiti Apps Using the Protect API
97
+ This list represents all known apps that are using the realtime updates API for UniFi Protect. If you're using the information you discovered on this page for your own UniFi Protect-based solution, please open an issue and I'm happy to add a link to it below. I hope this can serve as a repository of sorts for UniFi Protect-based apps and solutions in the community.
98
+
99
+ * [homebridge-unifi-protect](https://github.com/hjdhjd/homebridge-unifi-protect): Seamless integration of UniFi Protect into HomeKit with support for cameras, doorbells, and more.
100
+
101
+ #### Connecting
102
+ * Login to the UniFi Protect controller and obtain the bootstrap JSON. The URL is: `https://protect-nvr-ip/proxy/protect/api/bootstrap`. You can look through [protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/master/src/protect-api.ts) for a better understanding of the Protect login process and how to obtain the bootstrap JSON.
103
+ * Open the websocket to the updates URL. The URL is: `wss://protect-nvr-ip/proxy/protect/ws/updates?lastUpdateId?lastUpdateId=X`. You can grab lastUpdateId from the bootstrap JSON in the prior step. You can [see an example in protect-api.ts](https://github.com/hjdhjd/unifi-protect/blob/src/protect-api.ts#L225).
104
+ * Then you're ready to listen to messages. You can see an [example of this in protect-nvr.ts](https://github.com/hjdhjd/unifi-protect/blob/src/protect-nvr.ts#L408).
105
+
106
+ Those are the basics and gets us up and running. Now, to explain how the updates API works...
107
+
108
+ #### Updates Websocket Binary Format
109
+ UniFi OS update data packets are used to provide a realtime stream of updates to Protect. It differs from the system events API in that the system events API appears to be shared across other applications (Network, Access, etc.) while the updates events API appears to only be utilized by Protect and not shared by other applications, although the protocol is shared.
110
+
111
+ The `updates` websocket is used by the UniFi Protect webUI and native applications to provide realtime updates back to the controller. UniFi cameras and doorbells also use a websocket to provide those same updates to the Protect controller. The `updates` websocket uses a binary protocol to encode data largely to minimize bandwidth requirements, and provide access to more than JSON data, if needed.
112
+
113
+ So how does it all work? Cameras continuously stream updates to the UniFi Protect controller containing things like camera health, statistics, and - crucially for us - events such as motion and doorbell ring. A complete update packet is composed of four frames:
114
+
115
+ ```sh
116
+ Header Frame (8 bytes)
117
+ ----------------------
118
+ Action Frame
119
+ ----------------------
120
+ Header Frame (8 bytes)
121
+ ----------------------
122
+ Data Frame
123
+ ```
124
+
125
+ Let's look at each of these.
126
+
127
+ ##### Header Frame
128
+ The header frame is required overhead since websockets provide only a transport medium. It's purpose is to tell us what's coming in the frame that follows.
129
+
130
+ A packet header is composed of 8 bytes in this order:
131
+
132
+ | Byte Offset | Description | Bits | Values
133
+ |----------------|-----------------|---------|-----------------------------------------
134
+ | 0 | Packet Type | 8 | 1 - action frame, 2 - payload frame.
135
+ | 1 | Payload Format | 8 | 1 - JSON object, 2 - UTF8-encoded string, 3 - Node Buffer.
136
+ | 2 | Deflated | 8 | 0 - uncompressed, 1 - deflated / compressed ([zlib](https://nodejs.org/api/zlib.html)-based).
137
+ | 3 | Unknown | 8 | Always 0. Possibly reserved for future use by Ubiquiti?
138
+ | 4-7 | Payload Size | 32 | Size of payload in network-byte order (big endian).
139
+
140
+ If the header has marked the payload as deflated (compressed), you'll need to inflate (uncompress) the payload before you can use it.
141
+
142
+ ##### Action Frame
143
+ The action frame identifies what the action and category that the update contains:
144
+
145
+ | Property | Description
146
+ |-------------|------------------------------------------------------------------------------------
147
+ | action | What action is being taken. Known actions are `add` and `update`.
148
+ | id | The identifier for the device we're updating.
149
+ | modelKey | The device model category that we're updating.
150
+ | newUpdateId | A new UUID generated on a per-update basis. This can be safely ignored it seems.
151
+
152
+ ##### Data Frame
153
+ The final part of the update packet is the data frame. The data frame can be three different types of data - although in practice, JSONs are all that come across, I've found. Those types are:
154
+
155
+ | Payload Type | Description
156
+ |--------------|------------------------------------------------------------------------------------
157
+ | 1 | JSON. If the action frame's `action` property is set to `update` and the `modelKey` property is not set to `event` (e.g. `camera`), this will **always** a subset of the [configuration bootstrap JSON](https://github.com/hjdhjd/unifi-protect/blob/src/protect-types.ts#L6).
158
+ | 2 | A UTF8-encoded string.
159
+ | 3 | Node Buffer.
160
+
161
+ #### Tips
162
+ * `update` actions are always tied to any valid modelKey that exists in the bootstrap JSON. The exception is `events` which is tied to the Protect events history list that it maintains. The supported modelKeys from the bootstrap JSON are: `bridge`, `camera`, `group`, `light`, `liveview`, `nvr`, `sensor`, `user`, and `viewer`.
163
+ * `add` actions are always tied to the `event` modelKey and indicate the beginning of an event item in the Protect events list. A subsequent `update` action is sent signaling the end of the event capture, and it's confidence score for motion detection.
164
+ * This is **not** the same thing as motion detection. If you want to detect motion, you should watch the `update` action for `camera` modelKeys, and look for a JSON that updates `lastMotion`. For doorbell rings, `lastRing`. The Protect events list is useful for the Protect app, but it's of limited utility to HomeKit, and it's slow relative to just looking for the `lastMotion` JSON that tends to be much more timely in its delivery. If you want true realtime updates, you want to look at the `update` action.
165
+ * JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
166
+ * With the exception of `update` actions with a `modelKey` of `event`, JSONs are always a subset of the bootstrap JSON, indexed off of `modelKey`. So for a `modelKey` of `camera`, the data payload is always a subset of ProtectCameraConfigInterface (see [protect-types.ts](https://github.com/hjdhjd/unifi-protect/blob/src/protect-types.ts#L108)).
167
+
168
+ ## Library Development Dashboard
169
+ This is mostly of interest to the true developer nerds amongst us.
170
+
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)
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)
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)
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,3 +1,4 @@
1
1
  export * from "./protect-api";
2
- export * from "./protect-types";
3
2
  export * from "./protect-api-updates";
3
+ export * from "./protect-types";
4
+ export { FetchError } from "node-fetch-cjs";
package/dist/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  "use strict";
2
+ /* Copyright(C) 2019-2022, HJD (https://github.com/hjdhjd). All rights reserved.
3
+ *
4
+ * index.ts: UniFi Protect API registration.
5
+ */
2
6
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
7
  if (k2 === undefined) k2 = k;
4
8
  Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
@@ -10,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.FetchError = void 0;
13
18
  __exportStar(require("./protect-api"), exports);
14
- __exportStar(require("./protect-types"), exports);
15
19
  __exportStar(require("./protect-api-updates"), exports);
20
+ __exportStar(require("./protect-types"), exports);
21
+ var node_fetch_cjs_1 = require("node-fetch-cjs");
22
+ Object.defineProperty(exports, "FetchError", { enumerable: true, get: function () { return node_fetch_cjs_1.FetchError; } });
16
23
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,gDAA8B;AAC9B,kDAAgC;AAChC,wDAAsC"}
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,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { Logging } from "./logging";
3
2
  import { ProtectCameraLcdMessagePayload } from "./protect-types";
3
+ import { protectLogging } from "./protect-logging";
4
4
  declare type ProtectNvrUpdatePacket = {
5
5
  action: ProtectNvrUpdateEventAction;
6
6
  payload: Record<string, unknown> | string | Buffer;
@@ -26,11 +26,10 @@ export declare type ProtectNvrUpdatePayloadCameraUpdate = {
26
26
  isMotionDetected: boolean;
27
27
  lastMotion: number;
28
28
  lastRing: number;
29
- lastSeen: number;
30
29
  lcdMessage: ProtectCameraLcdMessagePayload;
31
30
  };
32
31
  export declare class ProtectApiUpdates {
33
- static decodeUpdatePacket(log: Logging, packet: Buffer): ProtectNvrUpdatePacket | null;
32
+ static decodeUpdatePacket(log: protectLogging, packet: Buffer): ProtectNvrUpdatePacket | null;
34
33
  private static decodeUpdateFrame;
35
34
  }
36
35
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"protect-api-updates.js","sourceRoot":"","sources":["../src/protect-api-updates.ts"],"names":[],"mappings":";;;;;;AAMA,gDAAwB;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AAEH,oDAAoD;AACpD,MAAM,yBAAyB,GAAG,CAAC,CAAC;AAEpC,oCAAoC;AACpC,IAAK,gBAGJ;AAHD,WAAK,gBAAgB;IACnB,2DAAU,CAAA;IACV,6DAAW,CAAA;AACb,CAAC,EAHI,gBAAgB,KAAhB,gBAAgB,QAGpB;AAED,qCAAqC;AACrC,IAAK,iBAIJ;AAJD,WAAK,iBAAiB;IACpB,yDAAQ,CAAA;IACR,6DAAU,CAAA;IACV,6DAAU,CAAA;AACZ,CAAC,EAJI,iBAAiB,KAAjB,iBAAiB,QAIrB;AAED;;;;;;;;GAQG;AACH,IAAK,kBAMJ;AAND,WAAK,kBAAkB;IACrB,2DAAQ,CAAA;IACR,+EAAkB,CAAA;IAClB,mEAAY,CAAA;IACZ,iEAAW,CAAA;IACX,2EAAgB,CAAA;AAClB,CAAC,EANI,kBAAkB,KAAlB,kBAAkB,QAMtB;AAuCD,MAAa,iBAAiB;IAE5B,mEAAmE;IAC5D,MAAM,CAAC,kBAAkB,CAAC,GAAY,EAAE,MAAc;QAE3D,gGAAgG;QAEhG,IAAI,UAAU,CAAC;QAEf,IAAI;YAEF,8HAA8H;YAC9H,qBAAqB;YACrB,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,kB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O,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAA4B,CAAC;gBACjE,MAAM;YAER,KAAK,iBAAiB,CAAC,MAAM;gBAC3B,OAAO,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;gBAChC,MAAM;YAER,KAAK,iBAAiB,CAAC,MAAM;gBAC3B,OAAO,OAAO,CAAC;gBACf,MAAM;YAER;gBACE,GAAG,CAAC,KAAK,CAAC,6EAA6E,EAAE,aAAa,CAAC,CAAC;gBACxG,OAAO,IAAI,CAAC;gBACZ,MAAM;SACT;IACH,CAAC;CACF;AAlFD,8CAkFC"}
@@ -1,49 +1,59 @@
1
- import { ProtectCameraConfig, ProtectCameraConfigInterface, ProtectCameraConfigPayload, ProtectNvrBootstrap } from "./protect-types";
2
- import { RequestInfo, RequestInit, Response } from "node-fetch";
3
- import { Logging } from "./logging";
1
+ import { ProtectCameraConfig, ProtectCameraConfigInterface, ProtectCameraConfigPayload, ProtectLightConfig, ProtectLightConfigPayload, ProtectNvrBootstrap, ProtectSensorConfig, ProtectViewerConfig, ProtectViewerConfigPayload } from "./protect-types";
2
+ import { RequestInfo, RequestInit, Response } from "node-fetch-cjs";
4
3
  import WebSocket from "ws";
4
+ import { protectLogging } from "./protect-logging";
5
+ declare type ProtectKnownDeviceTypes = ProtectCameraConfig | ProtectLightConfig | ProtectSensorConfig | ProtectViewerConfig;
5
6
  export declare class ProtectApi {
6
- disableKeepAlive?: boolean | undefined;
7
7
  private apiErrorCount;
8
8
  private apiLastSuccess;
9
9
  bootstrap: ProtectNvrBootstrap | null;
10
- Cameras: ProtectCameraConfig[] | undefined;
11
- private debug;
10
+ cameras: ProtectCameraConfig[] | undefined;
12
11
  private eventHeartbeatTimer;
13
12
  eventListener: WebSocket | null;
14
13
  eventListenerConfigured: boolean;
15
14
  private headers;
15
+ private httpsAgent;
16
16
  isAdminUser: boolean;
17
+ private isLoginRefresh;
18
+ lights: ProtectLightConfig[] | undefined;
17
19
  private log;
18
20
  private loggedIn;
19
21
  private loginAge;
22
+ private loginAttempt;
20
23
  private nvrAddress;
21
24
  private password;
25
+ sensors: ProtectSensorConfig[] | undefined;
22
26
  private username;
23
- private pendingLogin?;
24
- constructor(debug: (message: string, ...parameters: unknown[]) => void, log: Logging, nvrAddress: string, username: string, password: string, disableKeepAlive?: boolean | undefined);
27
+ viewers: ProtectViewerConfig[] | undefined;
28
+ constructor(nvrAddress: string, username: string, password: string, log?: protectLogging);
25
29
  private acquireToken;
26
- private loginWrapped;
30
+ private loginNvr;
27
31
  private login;
28
32
  private bootstrapProtect;
29
33
  private launchUpdatesListener;
34
+ private refreshDeviceClass;
30
35
  refreshDevices(): Promise<boolean>;
31
36
  isAllRtspConfigured(): boolean;
32
37
  private checkAdminUserStatus;
33
- private checkCameraState;
34
- updateChannels(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
35
- enableRtsp(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
36
38
  updateCamera(device: ProtectCameraConfig, payload: ProtectCameraConfigPayload): Promise<ProtectCameraConfig | null>;
39
+ updateCameraChannels(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
40
+ updateLight(device: ProtectLightConfig, payload: ProtectLightConfigPayload): Promise<ProtectLightConfig | null>;
41
+ updateViewer(device: ProtectViewerConfig, payload: ProtectViewerConfigPayload): Promise<ProtectViewerConfig | null>;
42
+ enableRtsp(device: ProtectCameraConfigInterface): Promise<ProtectCameraConfig | null>;
37
43
  getNvrName(): string;
38
- getDeviceName(camera: ProtectCameraConfig, name?: string, cameraInfo?: boolean): string;
39
- getFullName(camera: ProtectCameraConfig): string;
44
+ getDeviceName(device: ProtectKnownDeviceTypes, name?: string, deviceInfo?: boolean): string;
45
+ getFullName(device: ProtectKnownDeviceTypes): string;
40
46
  camerasUrl(): string;
47
+ lightsUrl(): string;
48
+ viewersUrl(): string;
49
+ wsUrl(): string;
41
50
  private authUrl;
42
51
  private bootstrapUrl;
43
52
  private systemUrl;
44
53
  private updatesUrl;
45
54
  private heartbeatEventListener;
46
55
  clearLoginCredentials(): void;
47
- loginFetch(url: RequestInfo, options?: RequestInit, logErrors?: boolean, decodeResponse?: boolean): Promise<Response | null>;
48
- fetch(url: RequestInfo, options?: RequestInit, logErrors?: boolean, decodeResponse?: boolean): Promise<Response | null>;
56
+ private canModifyCamera;
57
+ fetch(url: RequestInfo, options?: RequestInit, logErrors?: boolean, decodeResponse?: boolean, isRetry?: boolean): Promise<Response | null>;
49
58
  }
59
+ export {};