-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
442 lines (359 loc) · 16.1 KB
/
index.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* jshint asi: true, node: true, laxbreak: true, laxcomma: true, undef: true, unused: true */
// there is no known webhook/websocket to use for events, this results in very frequent polling under the push-sensor model...
var homespun = require('homespun-discovery')
, querystring = require('querystring')
, pushsensor = homespun.utilities.pushsensor
, PushSensor = pushsensor.Sensor
, roundTrip = homespun.utilities.roundtrip
, sensorTypes = homespun.utilities.sensortypes
, underscore = require('underscore')
, url = require('url')
, util = require('util')
var Accessory
, Service
, Characteristic
, CommunityTypes
, UUIDGen
module.exports = function (homebridge) {
Accessory = homebridge.platformAccessory
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
CommunityTypes = require('hap-nodejs-community-types')(homebridge)
UUIDGen = homebridge.hap.uuid
pushsensor.init(homebridge)
homebridge.registerPlatform('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', Ring, true)
}
var Ring = function (log, config, api) {
if (!(this instanceof Ring)) return new Ring(log, config, api)
this.log = log
this.config = config || { platform: 'ring-video-doorbell' }
this.api = api
this.location = this.config.location || url.parse('https://api.ring.com')
this.options = underscore.defaults(this.config.options || {}, { ttl: 4, verboseP: false })
this.discoveries = {}
this.doorbots = {}
this.stickup_cams = {}
if (api) this.api.on('didFinishLaunching', this._didFinishLaunching.bind(this))
else this._didFinishLaunching()
}
Ring.prototype._didFinishLaunching = function () {
var self = this
self._login(function () {
self._refresh1(function (err) {
if (err) {
self.log.error('refresh1 error: ' + err.toString())
throw err
}
self._refresh2(function (err) {
if (err) {
self.log.error('refresh2 error: ' + err.toString())
throw err
}
underscore.keys(self.discoveries).forEach(function (uuid) {
var accessory = self.discoveries[uuid]
self.log.warn('accessory not discovered', { UUID: uuid })
accessory.updateReachability(false)
})
setInterval(function () { self._refresh1.bind(self)(function (err) {
if (err) self.log.error('refresh1 error: ' + err.toString())
}) }, 5 * 60 * 1000)
setInterval(function () { self._refresh2.bind(self)(function (err) {
if (err) self.log.error('refresh2 error: ' + err.toString())
}) }, self.options.ttl * 1000)
})
})
})
self.log('didFinishLaunching')
}
//if (this.device.kind = doorbot)
Ring.prototype._addAccessory = function (doorbot) {
var self = this
var accessory = new Accessory(doorbot.name, doorbot.uuid)
accessory.on('identify', function (paired, callback) {
self.log(accessory.displayName, ': identify request')
callback()
})
if (doorbot.attachAccessory.bind(doorbot)(accessory)) self.api.updatePlatformAccessories([ accessory ])
if (!self.discoveries[accessory.UUID]) {
self.api.registerPlatformAccessories('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', [ accessory ])
self.log('addAccessory', underscore.pick(doorbot, [ 'uuid', 'name', 'manufacturer', 'model', 'serialNumber' ]))
}
}
//else
Ring.prototype._addAccessory = function (stickup_cams) {
var self = this
var accessory = new Accessory(stickup_cams.name, stickup_cams.uuid)
accessory.on('identify', function (paired, callback) {
self.log(accessory.displayName, ': identify request')
callback()
})
if (stickup_cams.attachAccessory.bind(stickup_cams)(accessory)) self.api.updatePlatformAccessories([ accessory ])
if (!self.discoveries[accessory.UUID]) {
self.api.registerPlatformAccessories('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', [ accessory ])
self.log('addAccessory', underscore.pick(stickup_cams, [ 'uuid', 'name', 'manufacturer', 'model', 'serialNumber' ]))
}
}
Ring.prototype.configurationRequestHandler = function (context, request, callback) {/* jshint unused: false */
this.log('configuration request', { context: context, request: request })
}
Ring.prototype.configureAccessory = function (accessory) {
var self = this
accessory.on('identify', function (paired, callback) {
self.log(accessory.displayName, ': identify request')
callback()
})
self.discoveries[accessory.UUID] = accessory
self.log('configureAccessory', underscore.pick(accessory, [ 'UUID', 'displayName' ]))
}
/*
{ profile :
{ id : ...
, email : "[email protected]"
, first_name : null
, last_name : null
, phone_number : null
, authentication_token : "..."
, features :
{ remote_logging_format_storing : false
, remote_logging_level : 1
, subscriptions_enabled : true
, stickupcam_setup_enabled : true
, vod_enabled : false
, nw_enabled : true
, nw_user_activated : false
, ringplus_enabled : true
, lpd_enabled : true
, reactive_snoozing_enabled : false
, proactive_snoozing_enabled : false
, owner_proactive_snoozing_enabled : true
, live_view_settings_enabled : false
, delete_all_settings_enabled : false
, power_cable_enabled : false
, device_health_alerts_enabled : true
, chime_pro_enabled : true
, multiple_calls_enabled : true
, ujet_enabled : false
, multiple_delete_enabled : false
, delete_all_enabled : false
}
}
}
*/
Ring.prototype._login = function (callback) {
var self = this
var headers =
{ Authorization : 'Basic ' + new Buffer(self.config.username + ':' + self.config.password).toString('base64')
, Accept : '*/*'
, 'User-Agent' : 'Dalvik/1.6.0 (Linux; U; Android 4.4.4; Build/KTU84Q)'
, 'content-type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
, payload =
{ 'device[os]' : 'android'
, 'device[hardware_id]' : '180940d0-7285-3366-8c64-6ea91491982c'
, 'device[app_brand]' : 'ring'
, 'device[metadata][device_model]' : 'VirtualBox'
, 'device[metadata][resolution]' : '600x800'
, 'device[metadata][app_version]' : '1.7.29'
, 'device[metadata][app_instalation_date]' : ''
, 'device[metadata][os_version]' : '4.4.4'
, 'device[metadata][manufacturer]' : 'innotek GmbH'
, 'device[metadata][is_tablet]' : 'true'
, 'device[metadata][linphone_initialized]' : 'true'
, 'device[metadata][language]' : 'en'
, api_version : '9'
}
roundTrip(underscore.defaults({ location: self.location, logger: self.log }, self.options),
{ method: 'POST', path: '/clients_api/session', headers: headers, payload: querystring.stringify(payload) },
function (err, response, result) {
if (err) {
self.log.error('login', underscore.extend({ username: self.config.username }, err))
return setTimeout(function () { self._login.bind(self)(callback) }, 30 * 1000)
}
self.profile = result.profile
if ((!self.profile) || (!self.profile.authentication_token)) return callback(new Error('invalid session response'))
callback()
})
}
/*
{ "id" : ...
, "description" : "Front Gate"
, "device_id" : "..."
, "time_zone" : "America\/Chicago"
, "subscribed" : true
, "subscribed_motions" : true
, "battery_life" : 20
, "external_connection" : false
, "firmware_version" : "1.7.189"
, "kind" : "doorbell"
, "latitude" : 39.8333333
, "longitude" : -98.585522
, "address" : ".... .... .., Lebanon, KS 66952 USA"
, "owned" : true
, "alerts" :
{ "connection" : "offline"
, "battery" : "low"
}
, "owner" :
{ "id" : ...
, "first_name" : null
, "last_name" : null
, "email" : "[email protected]"
}
}
*/
Ring.prototype._refresh1 = function (callback) {
var self = this
var headers =
{ Accept : '*/*'
, 'User-Agent' : 'Dalvik/1.6.0 (Linux; U; Android 4.4.4; Build/KTU84Q)'
}
, query = '?' + querystring.stringify({ api_version: '9', auth_token: self.profile.authentication_token })
roundTrip(underscore.defaults({ location: self.location, logger: self.log }, self.options),
{ path: '/clients_api/ring_devices' + query, headers: headers },
function (err, response, result) {
console.log("Ring Accessories", result)
if (err) return callback(err)
if ((!result) || (!result.doorbots)) result = { doorbots: [] }
result.doorbots.forEach(function (service) {
var capabilities, properties
, doorbotId = service.id
, doorbot = self.doorbots[doorbotId]
if (!doorbot) {
capabilities = underscore.pick(sensorTypes,
[ 'battery_level', 'battery_low', 'motion_detected', 'reachability', 'ringing' ])
properties = { name : service.description
, manufacturer : 'Bot Home Automation, Inc.'
, model : service.kind
, serialNumber : service.id.toString()
, firmwareRevision : service.firmware_version
, hardwareRevision : ''
}
doorbot = new Doorbot(self, service.device_id, { capabilities: capabilities, properties: properties })
self.doorbots[doorbotId] = doorbot
}
doorbot.readings = { battery_level : service.battery_life
, battery_low : (service.alerts) && (service.alerts.battery == 'low')
? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
: Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
, reachability : (service.alerts) && (service.alerts.connection !== 'offline')
}
doorbot._update.bind(doorbot)(doorbot.readings)
})
if ((!result) || (!result.stickup_cams)) result = { stickup_cams: [] }
result.stickup_cams.forEach(function (service) {
var capabilities, properties
, stickup_camsId = service.id
, stickup_cams = self.stickup_cams[stickup_camsId]
if (!stickup_cams) {
capabilities = underscore.pick(sensorTypes,
[ 'battery_level', 'battery_low', 'motion_detected', 'reachability', 'ringing' ])
properties = { name : service.description
, manufacturer : 'Bot Home Automation, Inc.'
, model : service.kind
, serialNumber : service.id.toString()
, firmwareRevision : service.firmware_version
, hardwareRevision : ''
}
stickup_cams = new Stickup_cams(self, service.device_id, { capabilities: capabilities, properties: properties })
self.stickup_cams[stickup_camsId] = stickup_cams
}
stickup_cams.readings = { battery_level : service.battery_life
, battery_low : (service.alerts) && (service.alerts.battery == 'low')
? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
: Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
, reachability : (service.alerts) && (service.alerts.connection !== 'offline')
}
stickup_cams._update.bind(stickup_cams)(stickup_cams.readings)
})
callback()
})
}
/*
[
{
"id" : ...,
"state" : "ringing",
"protocol" : "sip",
"doorbot_id" : ...,
"doorbot_description" : "Front Gate",
"device_kind" : "doorbell",
"motion" : false,
"kind" : "ding",
"sip_server_ip" : "a.b.c.d"
"sip_server_port" : "15063",
"sip_server_tls" : "false",
"sip_session_id" : "...",
"sip_from" : "sip:[email protected]",
"sip_to" : "sip:[email protected]:15063;transport=tcp",
"audio_jitter_buffer_ms" : 0,
"video_jitter_buffer_ms" : 0,
"sip_endpoints" : null,
"expires_in" : 171,
"now" : 1483114179.70994,
"optimization_level" : 3,
"sip_token" : "..."
"sip_ding_id" : "..."
}
]
*/
Ring.prototype._refresh2 = function (callback) {
var self = this
var headers =
{ Accept : '*/*'
, 'User-Agent' : 'Dalvik/1.6.0 (Linux; U; Android 4.4.4; Build/KTU84Q)'
}
, query = '?' + querystring.stringify({ api_version: '9', auth_token: self.profile.authentication_token })
roundTrip(underscore.defaults({ location: self.location, logger: self.log }, self.options),
{ path: '/clients_api/dings/active' + query, headers: headers },
function (err, response, result) {
if (err) return callback(err)
if (!util.isArray(result)) return callback(new Error('not an Array: ' + typeof result))
underscore.keys(self.doorbots).forEach(function (doorbotId) {
underscore.extend(self.doorbots[doorbotId].readings, { motion_detected: false, ringing: false })
})
result.forEach(function (event) {
var doorbot
if (event.state !== 'ringing') return
doorbot = self.doorbots[event.doorbot_id]
if (!doorbot) return self.log.error('dings/active: no doorbot', event)
underscore.extend(doorbot.readings, { motion_detected : (event.kind === 'motion') || (event.motion)
, ringing : event.kind === 'ding' })
})
underscore.keys(self.doorbots).forEach(function (doorbotId) {
var doorbot = self.doorbots[doorbotId]
doorbot._update.bind(doorbot)(doorbot.readings)
})
callback()
},
function (err, response, result) {
if (err) return callback(err)
if (!util.isArray(result)) return callback(new Error('not an Array: ' + typeof result))
underscore.keys(self.Stickup_cams).forEach(function (stickup_camsId) {
underscore.extend(self.Stickup_cams[stickup_camsId].readings, { motion_detected: false, ringing: false })
})
result.forEach(function (event) {
var Stickup_cams
if (event.state !== 'ringing') return
stickup_cams = self.stickup_cams[event.stickup_cams_id]
if (!stickup_cams) return self.log.error('dings/active: no doorbot', event)
underscore.extend(stickup_cams.readings, { motion_detected : (event.kind === 'motion') || (event.motion)
, ringing : event.kind === 'ding' })
})
underscore.keys(self.stickup_cams).forEach(function (stickup_camsId) {
var stickup_cams = self.stickup_cams[stickup_camsId]
stickup_cams._update.bind(stickup_cams)(stickup_cams.readings)
})
callback()
}
)
}
var Doorbot = function (platform, doorbotId, service) {
if (!(this instanceof Doorbot)) return new Doorbot(platform, doorbotId, service)
PushSensor.call(this, platform, doorbotId, service)
}
util.inherits(Doorbot, PushSensor);
var Stickup_cams = function (platform, stickup_camsId, service) {
if (!(this instanceof Stickup_cams)) return new Stickup_cams(platform, stickup_camsId, service)
PushSensor.call(this, platform, stickup_camsId, service)
}
util.inherits(Stickup_cams, PushSensor);