Skip to content

Commit

Permalink
Merge pull request #53 from cazacutudor/descovery-devices-fix
Browse files Browse the repository at this point in the history
Improve support over different OS and NICs
  • Loading branch information
nishtahir authored Sep 27, 2018
2 parents 27c8871 + 7127d6e commit 8042ce8
Showing 1 changed file with 62 additions and 54 deletions.
116 changes: 62 additions & 54 deletions lib/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,68 @@ const request = require('request')
const properties = require('../utils/properties')
const xmlMatcher = /<(.*?)>(.*?)</g

module.exports = {
scan: (timeout, callback) => {
log.info('searching for rokus for %d seconds...', timeout)
var client = new ssdp({
explicitSocketBind: true,
unicastBindPort: 1900
})
var rokus = {}
client.on('response', function(headers, stats, rinfo) {
let usn = headers.USN.replace('uuid:roku:ecp:', '')
if (!rokus[usn]) {
rokus[usn] = rinfo.address
request.get(
'http://' + rinfo.address + ':8060/query/device-info',
(err, response, body) => {
if (response) {
let roku = {}
roku[usn] = {}
while ((match = xmlMatcher.exec(response.body))) {
roku[usn][match[1]] = match[2]
}
log.info(
`found %s : %s : ${rinfo.address}`,
roku[usn]['model-name'],
roku[usn]['serial-number']
)
function scan(timeout, callback) {
log.info('searching for rokus for %d seconds...', timeout)
var client = getSSDPClient()
var rokus = {}
client.on('response', function(headers, stats, rinfo) {
let usn = headers.USN.replace('uuid:roku:ecp:', '')
if (!rokus[usn]) {
rokus[usn] = rinfo.address
request.get(
'http://' + rinfo.address + ':8060/query/device-info',
(err, response, body) => {
if (response) {
let roku = {}
roku[usn] = {}
while ((match = xmlMatcher.exec(response.body))) {
roku[usn][match[1]] = match[2]
}
log.info(
`found %s : %s : ${rinfo.address}`,
roku[usn]['model-name'],
roku[usn]['serial-number']
)
}
)
}
})
setTimeout(() => {
log.info('finished')
}, timeout * 1000)
client.search('roku:ecp')
},
usn: (id, t, callback) => {
log.info('searching for %s for %d seconds...', id, t)
var client = new ssdp()
var roku = null
client.on('response', function(headers, stats, rinfo) {
log.pretty('debug', 'headers: ', headers)
if (!roku && headers.USN == 'uuid:roku:ecp:' + id) {
log.info('found roku: %s at %s', id, rinfo.address)
roku = rinfo.address
callback ? callback(roku) : null
}
})
setTimeout(() => {
if (!roku) {
log.error('find roku failed')
callback ? callback(null) : null
}
}, t * 1000)
client.search('roku:ecp')
}
}
)
}
})
setTimeout(() => {
log.info('finished')
}, timeout * 1000)
client.search('roku:ecp')
}

function usn(id, t, callback) {
log.info('searching for %s for %d seconds...', id, t)
var client = getSSDPClient()
var roku = null
client.on('response', function(headers, stats, rinfo) {
log.pretty('debug', 'headers: ', headers)
if (!roku && headers.USN == 'uuid:roku:ecp:' + id) {
log.info('found roku: %s at %s', id, rinfo.address)
roku = rinfo.address
callback ? callback(roku) : null
}
})
setTimeout(() => {
if (!roku) {
log.error('find roku failed')
callback ? callback(null) : null
}
}, t * 1000)
client.search('roku:ecp')
}

function getSSDPClient() {
return new ssdp({
explicitSocketBind: true,
unicastBindPort: 1900
})
}

module.exports = {
scan,
usn
}

0 comments on commit 8042ce8

Please sign in to comment.