unifi-protect 0.0.2 → 2.0.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.
@@ -152,11 +152,14 @@ export interface ProtectCameraConfigInterface {
152
152
  host: string;
153
153
  id: string;
154
154
  isAdopted: boolean;
155
+ isAdoptedByOther: boolean;
155
156
  isAdopting: boolean;
156
157
  isAttemptingToConnect: boolean;
157
158
  isConnected: boolean;
158
159
  isDark: boolean;
160
+ isDeleting: boolean;
159
161
  isHidden: boolean;
162
+ isLiveHeatmapEnabled: boolean;
160
163
  isManaged: boolean;
161
164
  isMicEnabled: boolean;
162
165
  isMotionDetected: boolean;
@@ -199,6 +202,7 @@ export interface ProtectCameraConfigInterface {
199
202
  lastMotion: number;
200
203
  lastRing: number | null;
201
204
  lastSeen: number;
205
+ latestFirmwareVersion: string;
202
206
  lcdMessage: ProtectCameraLcdMessageConfigInterface;
203
207
  ledSettings: {
204
208
  blinkRate: number;
@@ -436,28 +440,6 @@ export interface ProtectNvrSystemEventControllerInterface {
436
440
  updateAvailable: string;
437
441
  version: string;
438
442
  }
439
- export interface ProtectOptions {
440
- controllers: ProtectNvrOptions[];
441
- debugAll: boolean;
442
- ffmpegOptions: string[];
443
- motionDuration: number;
444
- options: string[];
445
- verboseFfmpeg: boolean;
446
- videoProcessor: string;
447
- }
448
- export interface ProtectNvrOptions {
449
- address: string;
450
- doorbellMessages: {
451
- duration: number;
452
- message: string;
453
- }[];
454
- mqttTopic: string;
455
- mqttUrl: string;
456
- name: string;
457
- refreshInterval: number;
458
- username: string;
459
- password: string;
460
- }
461
443
  export declare type DeepPartial<T> = {
462
444
  [P in keyof T]?: T[P] extends Array<infer I> ? Array<DeepPartial<I>> : DeepPartial<T[P]>;
463
445
  };
package/dist/settings.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
- /* Copyright(C) 2017-2021, HJD (https://github.com/hjdhjd). All rights reserved.
2
+ /* Copyright(C) 2017-2022, HJD (https://github.com/hjdhjd). All rights reserved.
3
3
  *
4
- * settings.ts: Settings and constants for homebridge-unifi-protect.
4
+ * settings.ts: Settings and constants for UniFi Protect.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.PROTECT_LOGIN_REFRESH_INTERVAL = exports.PROTECT_EVENTS_HEARTBEAT_INTERVAL = exports.PROTECT_API_TIMEOUT = exports.PROTECT_API_RETRY_INTERVAL = exports.PROTECT_API_ERROR_LIMIT = void 0;
package/package.json CHANGED
@@ -1,7 +1,56 @@
1
1
  {
2
2
  "name": "unifi-protect",
3
- "version": "0.0.2",
4
- "description": "Unifi Protect API",
3
+ "type": "commonjs",
4
+ "version": "2.0.0",
5
+ "displayName": "UniFi Protect API",
6
+ "description": "A nearly complete implementation of the UniFi Protect API.",
7
+ "author": {
8
+ "name": "HJD",
9
+ "url": "https://github.com/hjdhjd"
10
+ },
11
+ "homepage": "https://github.com/hjdhjd/unifi-protect#readme",
12
+ "license": "ISC",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git://github.com/hjdhjd/unifi-protect.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/hjdhjd/unifi-protect/issues"
19
+ },
20
+ "keywords": [
21
+ "camera",
22
+ "doorbell",
23
+ "ubiquiti",
24
+ "unifi",
25
+ "unifi protect",
26
+ "motion",
27
+ "motion sensor",
28
+ "protect",
29
+ "security",
30
+ "uck",
31
+ "uck g2",
32
+ "uck g2+",
33
+ "uck gen2",
34
+ "uck gen2+",
35
+ "udm",
36
+ "udm pro",
37
+ "udm-pro",
38
+ "unifi camera",
39
+ "unifi cloud key",
40
+ "unifi dream machine pro",
41
+ "unifi ffmpeg",
42
+ "unifi nvr",
43
+ "unifi-nvr",
44
+ "unifios",
45
+ "uvc g3 flex",
46
+ "uvc g4 doorbell",
47
+ "uvc g4 pro",
48
+ "rtsp"
49
+ ],
50
+ "engines": {
51
+ "node": ">=12"
52
+ },
53
+ "main": "dist/index.js",
5
54
  "scripts": {
6
55
  "build": "rimraf ./dist && tsc",
7
56
  "clean": "rimraf ./dist",
@@ -11,17 +60,20 @@
11
60
  "test": "eslint src/**.ts",
12
61
  "watch": "npm run build && npm link && nodemon"
13
62
  },
14
- "main": "dist/index.js",
63
+ "devDependencies": {
64
+ "@types/node": "^17.0.6",
65
+ "@types/ws": "^8.2.2",
66
+ "@typescript-eslint/eslint-plugin": "^5.8.1",
67
+ "@typescript-eslint/parser": "^5.8.1",
68
+ "eslint": "^8.6.0",
69
+ "rimraf": "^3.0.2",
70
+ "typescript": "^4.5.4"
71
+ },
15
72
  "dependencies": {
16
73
  "abort-controller": "^3.0.0",
17
- "node-fetch": "^3.0.0-beta.10",
18
- "ws": "^8.2.0"
19
- },
20
- "devDependencies": {
21
- "@types/node": "^16.7.1",
22
- "@types/ws": "^7.4.7",
23
- "@typescript-eslint/eslint-plugin": "^4.29.3",
24
- "eslint": "^7.32.0",
25
- "typescript": "^4.3.5"
74
+ "domexception": "^4.0.0",
75
+ "node-fetch-cjs": "^3.1.0-fix.1",
76
+ "util": "^0.12.4",
77
+ "ws": "^8.4.0"
26
78
  }
27
79
  }
package/.eslintrc.json DELETED
@@ -1,40 +0,0 @@
1
- {
2
- "parser": "@typescript-eslint/parser",
3
- "extends": [
4
- "eslint:recommended",
5
- "plugin:@typescript-eslint/eslint-recommended",
6
- "plugin:@typescript-eslint/recommended",
7
- "plugin:@typescript-eslint/recommended-requiring-type-checking"
8
- ],
9
- "parserOptions": {
10
- "ecmaVersion": 2020,
11
- "project": "tsconfig.json",
12
- "sourceType": "module"
13
- },
14
- "ignorePatterns": [ "dist" ],
15
- "rules": {
16
- "brace-style": [ "warn" ],
17
- "camelcase": [ "warn" ],
18
- "comma-dangle": [ "error" ],
19
- "curly": [ "warn", "all" ],
20
- "dot-notation": "warn",
21
- "eqeqeq": "warn",
22
- "indent": [ "warn", 2, { "SwitchCase": 1 } ],
23
- "linebreak-style": [ "warn", "unix" ],
24
- "lines-between-class-members": [ "warn", "always", { "exceptAfterSingleLine": true } ],
25
- "max-len": [ "warn", 170 ],
26
- "no-await-in-loop": [ "warn" ],
27
- "no-console": [ "warn" ],
28
- "prefer-arrow-callback": [ "warn" ],
29
- "quotes": [ "warn", "double", { "avoidEscape": true } ],
30
- "semi": [ "warn", "always" ],
31
- "sort-imports": [ "warn" ],
32
- "sort-keys": [ "warn" ],
33
- "sort-vars": [ "warn" ],
34
- "@typescript-eslint/explicit-function-return-type": [ "warn" ],
35
- "@typescript-eslint/explicit-module-boundary-types": [ "warn" ],
36
- "@typescript-eslint/no-explicit-any": [ "warn" ],
37
- "@typescript-eslint/no-non-null-assertion": [ "warn" ],
38
- "@typescript-eslint/no-this-alias": [ "warn" ]
39
- }
40
- }
package/dist/logging.d.ts DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- * Represents a logging device which can be used directly as a function (for INFO logging)
3
- * but also has dedicated logging functions for respective logging levels.
4
- */
5
- export interface Logging {
6
- info(message: string, ...parameters: any[]): void;
7
- warn(message: string, ...parameters: any[]): void;
8
- error(message: string, ...parameters: any[]): void;
9
- debug(message: string, ...parameters: any[]): void;
10
- }
package/dist/logging.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=logging.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":""}
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./protect-api";
2
- export * from "./protect-types";
3
- export * from "./protect-api-updates";
package/src/logging.ts DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * Represents a logging device which can be used directly as a function (for INFO logging)
3
- * but also has dedicated logging functions for respective logging levels.
4
- */
5
- export interface Logging {
6
-
7
- /* eslint-disable @typescript-eslint/no-explicit-any */
8
- info(message: string, ...parameters: any[]): void;
9
- /* eslint-disable @typescript-eslint/no-explicit-any */
10
- warn(message: string, ...parameters: any[]): void;
11
- /* eslint-disable @typescript-eslint/no-explicit-any */
12
- error(message: string, ...parameters: any[]): void;
13
- /* eslint-disable @typescript-eslint/no-explicit-any */
14
- debug(message: string, ...parameters: any[]): void;
15
-
16
- }
@@ -1,226 +0,0 @@
1
- /* Copyright(C) 2021, HJD (https://github.com/hjdhjd). All rights reserved.
2
- *
3
- * protect-api-updates.ts: Our UniFi Protect realtime updates event API implementation.
4
- */
5
- import { Logging } from "./logging";
6
- import { ProtectCameraLcdMessagePayload } from "./protect-types";
7
- import zlib from "zlib";
8
-
9
- /*
10
- * The UniFi Protect realtime updates API is largely undocumented and has been reverse engineered mostly through
11
- * trial and error, as well as observing the Protect controller in action.
12
- *
13
- * Here's how to get started with the UniFi Protect Updates API:
14
- *
15
- * 1. Login to the UniFi Protect controller, obtain the bootstrap JSON.
16
- * 2. Open the websocket to the updates URL (see protect-api.ts).
17
- *
18
- * Then you're ready to listen to messages. You can see an example of this in protect-nvr.ts.
19
- *
20
- * Those are the basics and gets us up and running. Now, to explain how the updates API works...
21
- *
22
- * UniFi OS update data packets are used to provide a realtime stream of updates to Protect. It differs from
23
- * the system events API in that the system events API appears to be shared across other applications (Network, Access, etc.)
24
- * while the updates events API appears to only be utilized by Protect and not shared by other applications, although the protocol
25
- * is shared.
26
- *
27
- * So how does it all work? Cameras continuously stream updates to the UniFi Protect controller containing things like camera
28
- * health, statistics, and - crucially for us - events such as motion and doorbell ring. A complete update packet is composed of four
29
- * frames:
30
- *
31
- * Header Frame (8 bytes)
32
- * Action Frame
33
- * Header Frame (8 bytes)
34
- * Data Frame
35
- *
36
- * The header frame is required overhead since websockets provide only a transport medium. It's purpose is to tell us what's
37
- * coming in the frame that follows.
38
- *
39
- * The action frame identifies what the action and category that the update contains:
40
- *
41
- * Property Description
42
- * -------- -----------
43
- * action What action is being taken. Known actions are "add" and "update".
44
- * id The identifier for the device we're updating.
45
- * modelKey The device model category that we're updating.
46
- * newUpdateId A new UUID generated on a per-update basis. This can be safely ignored it seems.
47
- *
48
- * The final part of the update packet is the data frame. The data frame can be three different types of data - although in
49
- * practice, I've only seen JSONs come across. Those types are:
50
- *
51
- * Payload Type Description
52
- * 1 JSON. For update actions that are not events, this is always a subset of the configuration bootstrap JSON.
53
- * 2 A UTF8-encoded string
54
- * 3 Node Buffer
55
- *
56
- * Some tips:
57
- *
58
- * - "update" actions are always tied to the following modelKeys: camera, event, nvr, and user.
59
- *
60
- * - "add" actions are always tied to the "event" modelKey and indicate the beginning of an event item in the Protect events list.
61
- * A subsequent "update" action is sent signaling the end of the event capture, and it's confidence score for motion detection.
62
- *
63
- * - The above is NOT the same thing as motion detection. If you want to detect motion, you should watch the "update" action for "camera"
64
- * modelKeys, and look for a JSON that updates lastMotion. For doorbell rings, lastRing. The Protect events list is useful for the
65
- * Protect app, but it's of limited utility to HomeKit, and it's slow - relative to looking for lastMotion that is. If you want true
66
- * realtime updates, you want to look at the "update" action.
67
- *
68
- * - JSONs are only payload type that seems to be sent, although the protocol is designed to accept all three.
69
- *
70
- * - With the exception of update actions with a modelKey of event, JSONs are always a subset of the bootstrap JSON, indexed off
71
- * of modelKey. So for a modelKey of camera, the data payload is always a subset of ProtectCameraConfigInterface (see protect-types.ts).
72
- */
73
-
74
- // Update realtime API packet header size, in bytes.
75
- const UPDATE_PACKET_HEADER_SIZE = 8;
76
-
77
- // Update realtime API packet types.
78
- enum UpdatePacketType {
79
- ACTION = 1,
80
- PAYLOAD = 2
81
- }
82
-
83
- // Update realtime API payload types.
84
- enum UpdatePayloadType {
85
- JSON = 1,
86
- STRING = 2,
87
- BUFFER = 3
88
- }
89
-
90
- /* A packet header is composed of 8 bytes in this order:
91
- *
92
- * Byte Offset Description Bits Values
93
- * 0 Packet Type 8 1 - action frame, 2 - payload frame.
94
- * 1 Payload Format 8 1 - JSON object, 2 - UTF8-encoded string, 3 - Node Buffer.
95
- * 2 Deflated 8 0 - uncompressed, 1 - compressed / deflated (zlib-based compression).
96
- * 3 Unknown 8 Always 0. Possibly reserved for future use by Ubiquiti?
97
- * 4-7 Payload Size: 32 Size of payload in network-byte order (big endian).
98
- */
99
- enum UpdatePacketHeader {
100
- TYPE = 0,
101
- PAYLOAD_FORMAT = 1,
102
- DEFLATED = 2,
103
- UNKNOWN = 3,
104
- PAYLOAD_SIZE = 4
105
- }
106
-
107
- // A complete description of the UniFi Protect realtime update events API packet format.
108
- type ProtectNvrUpdatePacket = {
109
- action: ProtectNvrUpdateEventAction,
110
- payload: Record<string, unknown> | string | Buffer
111
- }
112
-
113
- // A complete description of the UniFi Protect realtime update events API action packet JSON.
114
- type ProtectNvrUpdateEventAction = {
115
- action: string,
116
- id: string,
117
- modelKey: string,
118
- newUpdateId: string
119
- }
120
-
121
- // A complete description of the UniFi Protect realtime update events API payload packet JSONs.
122
- // Payload JSON for modelKey: event action: add
123
- export type ProtectNvrUpdatePayloadEventAdd = {
124
- camera: string,
125
- id: string,
126
- modelKey: string,
127
- partition: null,
128
- score: number,
129
- smartDetectEvents: string[],
130
- smartDetectTypes: string[],
131
- start: number,
132
- type: string
133
- }
134
-
135
- // Payload JSON for modelKey: camera action: update
136
- export type ProtectNvrUpdatePayloadCameraUpdate = {
137
- isMotionDetected: boolean,
138
- lastMotion: number,
139
- lastRing: number,
140
- lastSeen: number,
141
- lcdMessage: ProtectCameraLcdMessagePayload
142
- }
143
-
144
- export class ProtectApiUpdates {
145
-
146
- // Process an update data packet and return the action and payload.
147
- public static decodeUpdatePacket(log: Logging, packet: Buffer): ProtectNvrUpdatePacket | null {
148
-
149
- // What we need to do here is to split this packet into the header and payload, and decode them.
150
-
151
- let dataOffset;
152
-
153
- try {
154
-
155
- // The fourth byte holds our payload size. When you add the payload size to our header frame size, you get the location of the
156
- // data header frame.
157
- dataOffset = packet.readUInt32BE(UpdatePacketHeader.PAYLOAD_SIZE) + UPDATE_PACKET_HEADER_SIZE;
158
-
159
- // Validate our packet size, just in case we have more or less data than we expect. If we do, we're done for now.
160
- if(packet.length !== (dataOffset + UPDATE_PACKET_HEADER_SIZE + packet.readUInt32BE(dataOffset + UpdatePacketHeader.PAYLOAD_SIZE))) {
161
- throw new Error("Packet length doesn't match header information.");
162
- }
163
-
164
- } catch(error) {
165
-
166
- log.error("Realtime update API: error decoding update packet: %s.", error);
167
- return null;
168
-
169
- }
170
-
171
- // Decode the action and payload frames now that we know where everything is.
172
- const actionFrame = this.decodeUpdateFrame(log, packet.slice(0, dataOffset), UpdatePacketType.ACTION) as ProtectNvrUpdateEventAction;
173
- const payloadFrame = this.decodeUpdateFrame(log, packet.slice(dataOffset), UpdatePacketType.PAYLOAD);
174
-
175
- if(!actionFrame || !payloadFrame) {
176
- return null;
177
- }
178
-
179
- return({ action: actionFrame, payload: payloadFrame });
180
- }
181
-
182
- // Decode a frame, composed of a header and payload, received through the update events API.
183
- private static decodeUpdateFrame(log: Logging, packet: Buffer, packetType: number): ProtectNvrUpdateEventAction | Record<string, unknown> | string | Buffer | null {
184
-
185
- // Read the packet frame type.
186
- const frameType = packet.readUInt8(UpdatePacketHeader.TYPE);
187
-
188
- // This isn't the frame type we were expecting - we're done.
189
- if(packetType !== frameType) {
190
- return null;
191
- }
192
-
193
- // Read the payload format.
194
- const payloadFormat = packet.readUInt8(UpdatePacketHeader.PAYLOAD_FORMAT);
195
-
196
- // Check to see if we're compressed or not, and inflate if needed after skipping past the 8-byte header.
197
- const payload = packet.readUInt8(UpdatePacketHeader.DEFLATED) ? zlib.inflateSync(packet.slice(UPDATE_PACKET_HEADER_SIZE)) : packet.slice(UPDATE_PACKET_HEADER_SIZE);
198
-
199
- // If it's an action, it can only have one format.
200
- if(frameType === UpdatePacketType.ACTION) {
201
- return (payloadFormat === UpdatePayloadType.JSON) ? JSON.parse(payload.toString()) as ProtectNvrUpdateEventAction : null;
202
- }
203
-
204
- // Process the payload format accordingly.
205
- switch(payloadFormat) {
206
-
207
- case UpdatePayloadType.JSON:
208
- // If it's data payload, it can be anything.
209
- return JSON.parse(payload.toString()) as Record<string, unknown>;
210
- break;
211
-
212
- case UpdatePayloadType.STRING:
213
- return payload.toString("utf8");
214
- break;
215
-
216
- case UpdatePayloadType.BUFFER:
217
- return payload;
218
- break;
219
-
220
- default:
221
- log.error("Unknown payload packet type received in the realtime update events API: %s.", payloadFormat);
222
- return null;
223
- break;
224
- }
225
- }
226
- }