-
Notifications
You must be signed in to change notification settings - Fork 15
/
get-live-stream.js
41 lines (29 loc) · 1.14 KB
/
get-live-stream.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
'use strict'
module.exports = bottle => bottle.service( 'getLiveStream', getLiveStream,
'restClient',
'apiUrls',
'getActiveDings',
'logger'
)
function getLiveStream( restClient, apiUrls, getActiveDings, logger ) {
return async device => {
const first = require( 'lodash.first' )
const maxTries = 10
const waitForDing = async() => {
// poll until the livestream is ready up to a maximum number of times
for ( let tries = 0; tries < maxTries; tries++ ) {
logger( `waiting for ding, attempt ${tries}` )
const dings = await getActiveDings({ burst: true })
const liveStreamDing = first( dings )
if ( liveStreamDing ) {
return liveStreamDing
}
}
throw new Error( `could not get a ding for this livestream after ${maxTries} attempts` )
}
// create a new live stream:
const liveStreamUrl = apiUrls.doorbots().device( device ).liveStream()
await restClient.authenticatedRequest( 'POST', liveStreamUrl )
return waitForDing()
}
}