This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathskybell_device.js
294 lines (259 loc) · 10.9 KB
/
skybell_device.js
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
// Homebridge plugin for SkyBell HD video doorbells
// Copyright © 2017 Alexander Thoukydides
'use strict';
let request = require('request');
let xml2js = require('xml2js');
// Default options
const DEFAULT_OPTIONS = {
log: console.log,
// Callback functions to be called for interesting events
callbackInfo: () => {},
callbackSettings: () => {},
callbackButton: () => {},
callbackMotion: () => {},
// Interval between polling for changes (in seconds)
intervalInfo: 5 * 60,
intervalSettings: 60,
intervalActivities: 5,
// Maximum retries when starting a call
callRetries: 5
};
const MS = 1000;
// A single SkyBell HD device
module.exports = class SkyBellDevice {
// Create a new SkyBell device object
constructor(api, device, options = {}) {
// Store useful information about the device
this.api = api;
this.deviceId = device.id;
this.name = device.name;
// Store the options, applying defaults for missing options
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
// Cache the initial device information
this.cache = { device: device };
// No alternative API identifiers initially
this.apiById = {};
// Start polling the device
this.pollInfo();
this.pollSettings();
this.pollActivities();
}
// Modify the options
setOptions(options) {
Object.assign(this.options, options);
}
// Periodically poll the device's information
pollInfo() {
clearTimeout(this.timerPollInfo);
this.api.getInfoByDevice(this.deviceId, (err, body) => {
// Process the information
if (err) {
this.options.log("Failed to read SkyBell '" + this.name
+ "' device information: " + err);
} else {
this.newInfo(body);
}
// Poll again later
this.timerPollInfo = setTimeout(() => this.pollInfo(),
this.options.intervalInfo * MS);
});
}
// Periodically poll the device's settings
pollSettings() {
clearTimeout(this.timerPollSettings);
this.api.getSettingsByDevice(this.deviceId, (err, body) => {
// Process the settings
if (err) {
this.options.log("Failed to read SkyBell '" + this.name
+ "' device settings: " + err);
} else {
this.newSettings(body);
}
// Poll again later
this.timerPollSettings =
setTimeout(() => this.pollSettings(),
this.options.intervalSettings * MS);
});
}
// Periodically poll the device's activity log
pollActivities() {
clearTimeout(this.timerPollActivities);
this.api.getActivitiesByDevice(this.deviceId, (err, body) => {
// Process the activities
if (err) {
this.options.log("Failed to read SkyBell '" + this.name
+ "' device activities: " + err);
} else {
if (this.lastActivityId) {
// Find the first new activity
for (var i = 0; i < body.length; ++i) {
if (body[i].id == this.lastActivityId) break;
}
// Process the new activities from oldest to newest
while (i--) {
this.newActivity(body[i]);
}
}
this.lastActivity = body[0];
this.lastActivityId = body.length ? body[0].id : 'none';
}
// Poll again later
this.timerPollActivities =
setTimeout(() => this.pollActivities(),
this.options.intervalActivities * MS);
});
}
// Information has been read
newInfo(info) {
this.options.log("SkyBell '" + this.name + "' Wi-Fi status: "
+ ' Quality=' + info.status.wifiLink
+ ", SSID='" + info.essid + "'"
+ ', RSSI=' + info.wifiSignalLevel + 'dBm'
+ ', Noise=' + info.wifiNoise + 'dBm'
+ ', SNR=' + info.wifiSnr + 'dB'
+ ', Quality=' + info.wifiLinkQuality + '%'
+ ', Rate=' + info.wifiBitrate + 'Mbps)');
const needKeys = ['serialNo', 'proxy_port', 'deviceId', 'localHostname', 'firmwareVersion', 'port', 'timestamp', 'address', 'proxy_address', 'wifiNoise', 'wifiBitrate', 'wifiLinkQuality', 'wifiSnr', 'mac', 'wifiTxPwrEeprom', 'hardwareRevision', 'wifiSignalLevel', 'essid', 'checkedInAt', 'status'];
if (needKeys.every(key => Object.keys(info).includes(key))) {
this.cache.info = info;
this.options.callbackInfo(info);
}
}
// Settings have been read
newSettings(settings) {
const needKeys = ['ring_tone', 'do_not_ring', 'do_not_disturb', 'digital_doorbell', 'video_profile', 'mic_volume', 'speaker_volume', 'chime_level', 'motion_threshold', 'low_lux_threshold', 'med_lux_threshold', 'high_lux_threshold', 'low_front_led_dac', 'med_front_led_dac', 'high_front_led_dac', 'green_r', 'green_g', 'green_b', 'led_intensity', 'motion_policy'];
if (needKeys.every(key => Object.keys(settings).includes(key))) {
this.cache.settings = settings;
this.options.callbackSettings(settings);
}
}
// Change some settings
updateSettings(settings, callback) {
this.api.setSettingsByDevice(this.deviceId, settings, (err, body) => {
if (err) {
this.options.log("Failed to reconfigure SkyBell '" + this.name
+ "' " + JSON.stringify(settings)
+ ': ' + err);
} else {
Object.assign(this.cache.settings, settings);
}
callback(err);
});
}
// A new activity has been detected
newActivity(activity) {
switch (activity.event) {
case 'device:sensor:button':
this.options.log("Button pressed on SkyBell '" + this.name + "'");
this.options.callbackButton(activity);
break;
case 'device:sensor:motion':
this.options.log("Motion detected by SkyBell '" + this.name + "'");
this.options.callbackMotion(activity);
break;
case 'application:on-demand':
this.options.log("On-demand activiation for SkyBell '"
+ this.name + "'");
break;
default:
this.options.log("Unknown activity event for SkyBell '"
+ this.name + "': " + activity.event);
}
}
// Obtain the URL for the video associated with an activity
getVideoUrl(activity, callback) {
this.api.getActivityVideoByDevice(this.deviceId, activity.id,
(err, body) => {
if (err) {
this.options.log("Failed to retrieve video " + activity.id
+ " from SkyBell '" + this.name + "': " + err);
}
callback(err, body.url);
});
}
// Obtain the current avatar
getAvatar(callback) {
this.api.getAvatarByDevice(this.deviceId, (err, avatar) => {
if (err) {
this.options.log("Failed to read SkyBell '" + this.name
+ "' avatar: " + err);
return callback(err);
}
// Check whether the most recent activity has a newer image
let url = avatar.url;
if (this.lastActivity
&& (avatar.createdAt < this.lastActivity.createdAt)) {
this.options.log("SkyBell '" + this.name
+ "' activity is more recent than avatar");
url = this.lastActivity.media;
}
// URL obtained, so download the actual avatar image
this.requestS3(url, (err, image) => {
if (err) {
this.options.log("Failed to retrieve SkyBell '" + this.name
+ "' avatar: " + err);
return callback(err);
}
let type = url.match(/\.(\w+)\?/);
callback(null, image, type ? type[1] : 'jpg');
});
});
}
// Issue a request for an Amazon Simple Storage Service (S3) object
requestS3(url, callback) {
let options = {
url: url,
encoding: null
};
let logPrefix = 'AWS S3 request: ';
this.options.log(logPrefix + url);
let startTime = Date.now();
request(options, (err, response, body) => {
// Check for errors
this.options.log(logPrefix + (err || response.statusMessage)
+ ' +' + (Date.now() - startTime) + 'ms ');
if (response && response.statusCode < 400) {
return callback(null, body);
}
// Attempt to extract a useful error message
let msg = err || response.statusMessage;
xml2js.parseString(body || '', (err, xml) => {
if (xml && xml.Error) {
let e = xml.Error;
msg = e.Message[0] || e.Code[0] || JSON.stringify(e);
}
callback(new Error('SkyBell S3 error: ' + msg));
});
});
}
// Start a call
startCall(id, callback, attempt = 1) {
this.getApiById(id).startCallByDevice(this.deviceId, (err, body) => {
if (err && (attempt < this.options.callRetries)) {
this.options.log("Retrying call to SkyBell '" + this.name
+ "' " + id + ': ' + err);
return this.startCall(id, callback, attempt + 1);
}
if (err) {
this.options.log("Failed to start call to SkyBell '" + this.name
+ "' " + id + ': ' + err);
}
callback(err, body);
});
}
// End a call
stopCall(id, callback) {
this.getApiById(id).stopCallByDevice(this.deviceId, (err, body) => {
if (err) {
this.options.log("Failed to stop call to SkyBell '" + this.name
+ "' " + id + ': ' + err);
}
callback(err);
});
}
// Obtain an API using specific identifiers
getApiById(id) {
if (!this.apiById[id]) this.apiById[id] = this.api.clone();
return this.apiById[id];
}
};