-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
307 lines (268 loc) · 7.61 KB
/
index.js
File metadata and controls
307 lines (268 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
process.title = process.env.TITLE || "ring-microservice";
process.env.DEBUG = "HostBase,Ring";
const debug = require("debug")("Ring"),
promisify = require("util").promisify,
fs = require("fs"),
{ writeFile, readFile } = fs,
HostBase = require("microservice-core/HostBase");
const mqtt_host = process.env.MQTT_HOST || "mqtt";
const topic = process.env.MQTT_TOPIC || "ring";
const RingAPI = require("ring-client-api"),
ring = new RingAPI.RingApi({
cameraStatusPollingSeconds: 20,
cameraDingsPollingSeconds: 2,
avoidSnapshotBatteryDrain: true,
refreshToken: process.env.RING_TOKEN,
});
ring.onRefreshTokenUpdated.subscribe(
async ({ newRefreshToken, oldRefreshToken }) => {
console.log("Refresh Token Updated: ", newRefreshToken);
// If you are implementing a project that use `ring-client-api`, you should subscribe to onRefreshTokenUpdated and update your config each time it fires an event
// Here is an example using a .env file for configuration
if (!oldRefreshToken) {
return;
}
const currentConfig = await promisify(readFile)(".env"),
updatedConfig = currentConfig
.toString()
.replace(oldRefreshToken, newRefreshToken);
await promisify(writeFile)(".env", updatedConfig);
},
);
function deepEqual(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1 = object1[key];
const val2 = object2[key];
const areObjects = isObject(val1) && isObject(val2);
if (
(areObjects && !deepEqual(val1, val2)) ||
(!areObjects && val1 !== val2)
) {
return false;
}
}
return true;
}
function isObject(object) {
return object != null && typeof object === "object";
}
class DelayedTask {
constructor(fn, time) {
this.fn = fn;
this.timer = setTimeout(fn, time);
}
defer(time) {
this.cancel();
this.timer = setTimeout(this.fn, time);
}
cancel() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
}
class CameraHost extends HostBase {
constructor(camera, location) {
super(
mqtt_host,
topic +
"/" +
location.locationDetails.name +
"/" +
camera.initialData.description,
true,
);
debug("construct camera", this.topic);
this.alert("alert", process.title, " running");
this.camera = camera;
this.device = camera.initialData.description;
const d = camera.initialData;
this.info = {
id: camera.id,
type: camera.deviceType,
model: camera.model,
description: d.description,
deviceId: d.device_id,
isDoorBot: camera.isDoorbot,
hasBattery: camera.hasBattery,
hasSiren: camera.hasSiren,
hasLight: camera.hasLight,
batteryLife: d.battery_life,
kind: d.kind,
locationId: d.location_id,
created: new Date(d.created_at),
data: camera.data,
};
this.events = {};
this.data = {}; // camera.initialData;
this.run();
}
async processEvents() {
while (this.processing) {
await this.wait(100);
}
this.processing = true;
try {
// debug("processEvents");
const eventResponse = await this.camera.getEvents({ limit: 10000 }),
events = eventResponse.events;
let update = false;
this.events = {};
for (const event of events) {
const created = new Date(event.created_at),
key = created.getTime();
event.created = created;
if (!this.events[key]) {
// console.log("update", key);
const url = await this.camera.getRecordingUrl(event.ding_id_str, {
transcoded: false,
});
event.url = url;
this.events[key] = {
event: event,
url: url,
};
update = true;
}
}
if (update) {
debug(
">>>>>>>>>>>>>>>>>>>>>>>>>> processEvents UPDATE >>>>>>>>>>>>>>>>>>>>",
);
this.retain = true;
this.state = { events: this.events };
}
} catch (e) {}
this.processing = false;
}
async run() {
this.client.on("connect", async () => {
this.camera.onData.subscribe(async (data) => {
debug("<<<<<< onData", JSON.stringify(data).substr(0, 40));
if (!deepEqual(data, this.data)) {
this.retain = true;
this.state = { battery: Number(data.battery_life) };
const newInfo = Object.assign({}, this.info);
newInfo.data = data;
this.retain = true;
this.state = { info: newInfo };
this.data = data;
}
await this.processEvents();
});
this.camera.onNewDing.subscribe(async (data) => {
debug("<<<<<< newDing data", data);
this.retain = false;
this.state = { ding: data };
await this.processEvents();
});
this.camera.onDoorbellPressed.subscribe(async (state) => {
debug("<<<<<< doorbell pressed", state);
this.retain = false;
this.state = { doorbell: "active" };
this.alert(
"ALERT",
`There is someone ringing the doorbell at ${this.device}.`,
);
if (this.delayedTask) {
this.delayedTask.defer(2000);
} else {
this.delayedTask = new DelayedTask(async () => {
this.retain = true;
this.state = { doorbell: "inactive" };
this.delayedTask = null;
}, 2000);
}
await this.processEvents();
});
this.camera.onMotionDetected.subscribe(async (state) => {
debug("<<<<<< detected motion state", state);
this.retain = true;
this.state = { motion: state ? "active" : "inactive" };
if (state) {
this.alert("ALERT", `There is motion at ${this.device}.`);
}
await this.processEvents();
});
this.retain = true;
this.state = { doorbell: "inactive" };
});
}
}
class ChimeHost extends HostBase {
constructor(chime, location) {
super(
mqtt_host,
topic +
"/" +
location.locationDetails.name +
"/" +
chime.initialData.description,
true,
);
debug("construct chime", this.topic);
this.retain = false;
this.chime = chime;
const d = chime.initialData;
this.info = {
id: chime.id,
type: chime.deviceType,
model: chime.model,
description: d.description,
deviceId: d.device_id,
kind: d.kind,
created: new Date(d.created_at),
data: chime.data,
};
this.run();
}
async run() {
for (;;) {
await this.wait(15000);
}
}
async command(topic, command) {
debug("command", topic, command);
await chime.playSound(command);
}
}
const cameraHosts = [],
chimeHosts = [];
const processCameras = async (location) => {
debug("cameras", location.cameras.length);
for (const camera of location.cameras) {
cameraHosts.push(new CameraHost(camera, location));
}
debug("");
};
const processChimes = async (location) => {
debug("chimes", location.chimes.length);
for (const chime of location.chimes) {
chimeHosts.push(new ChimeHost(chime, location));
}
debug("");
};
const processLocation = async (location) => {
await processCameras(location);
await processChimes(location);
};
const main = async () => {
try {
const locations = await ring.getLocations();
debug("locations", locations.length);
for (const location of locations) {
await processLocation(location);
}
} catch (e) {
console.log("e", e);
}
};
//
console.clear();
main();