forked from thoukydides/homebridge-skybell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskybell_account.js
84 lines (69 loc) · 2.47 KB
/
skybell_account.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
// Homebridge plugin for SkyBell HD video doorbells
// Copyright © 2017 Alexander Thoukydides
'use strict';
let SkyBellAPI = require('./skybell_api');
let SkyBellDevice = require('./skybell_device');
// Default options
const DEFAULT_OPTIONS = {
log: console.log,
// Callback functions to be called for interesting events
callbackAdd: () => {},
callbackDiscovered: () => {},
// Interval between polling for new devices (in seconds)
intervalDevices: 5 * 60,
};
const MS = 1000;
// A SkyBell account
module.exports = class SkyBellAccount {
// Create a new SkyBell account object
constructor(user, pass, options = {}) {
// Store the options, applying defaults for missing options
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
// Create a SkyBell API object
this.api = new SkyBellAPI(user, pass, this.options.log);
// Start polling the list of devices
this.skybellDevices = {};
this.pollDevices();
}
// Modify the options
setOptions(options) {
Object.assign(this.options, options);
}
// Periodically poll the list of SkyBell devices
pollDevices() {
this.api.getDevices((err, body) => {
// Process the list of devices
if (err) {
this.options.log('Unable to enumerate SkyBell devices: ' + err);
} else {
this.gotDevices(body);
}
// Poll again later
setTimeout(() => this.pollDevices(),
this.options.intervalDevices * MS);
});
}
// An updated list of SkyBell devices has been obtained
gotDevices(devices) {
// Add any new devices
devices.forEach(device => {
let deviceId = device.id;
if (!this.skybellDevices[deviceId]) {
this.skybellDevices[deviceId] = this.addDevice(device);
}
});
// End of discovery, first time only
if (!this.discoveryDone) {
this.options.callbackDiscovered();
this.discoveryDone = true;
}
}
// A new SkyBell device has been found
addDevice(device) {
this.options.log("Discovered Skybell '" + device.name
+ "': device_id=" + device.id);
let skybellDevice = new SkyBellDevice(this.api, device, this.options);
this.options.callbackAdd(skybellDevice);
return skybellDevice;
}
};