Skip to content

Commit

Permalink
Merge pull request #54 from cazacutudor/refactor-find-command
Browse files Browse the repository at this point in the history
Refactor find command
  • Loading branch information
nishtahir authored Sep 28, 2018
2 parents 8042ce8 + 43c26f7 commit bc8ee73
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 56 deletions.
17 changes: 11 additions & 6 deletions bin/ukor-find.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const log = require('../lib/utils/log')
const utils = require('../lib/utils/utils')
const find = require('../lib/commands/find')
const program = require('../lib/utils/log-commander')
const DEFAULT_TIMEOUT = 5

program
.arguments('[id]')
.option('-t, --timeout <seconds>', 'Time to scan in seconds')
.parse(process.argv)

let t = 5
if (program['timeout']) {
t = program.timeout
}

if (program.args.length == 0) {
find.scan(t)
let timeout = program['timeout'] ? program.timeout : DEFAULT_TIMEOUT

find.scan(timeout, ipAddress => {
utils.getDeviceInfo(ipAddress, device => {
log.info(`found ${device['model-name']} : ${device['serial-number']} : ${ipAddress}`)
})
})
} else {
find.usn(program.args[0], t)
find.findDeviceBySerialNo(program.args[0], t)
}
62 changes: 15 additions & 47 deletions lib/commands/find.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,39 @@
const ssdp = require('node-ssdp').Client
const ssdp = require('node-ssdp')
const log = require('../utils/log')
const request = require('request')
const properties = require('../utils/properties')
const xmlMatcher = /<(.*?)>(.*?)</g

function scan(timeout, callback) {
log.info('searching for rokus for %d seconds...', timeout)
log.info('searching devices 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']
)
}
}
)
}
log.pretty('debug', 'headers: ', headers)
callback ? callback(rinfo.address, headers) : null
})

setTimeout(() => {
log.info('finished')
log.debug('scan: TIMEOUT')
}, 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
function findDeviceBySerialNo(deviceSerialNo, timeout, callback) {
scan(timeout, (ipAddress, headers) => {
if (headers.USN == `uuid:roku:ecp:${deviceSerialNo}`) {
log.info('found roku: %s at %s', deviceSerialNo, ipAddress)

callback ? callback(ipAddress) : null
}
})
setTimeout(() => {
if (!roku) {
log.error('find roku failed')
callback ? callback(null) : null
}
}, t * 1000)
client.search('roku:ecp')
}

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

module.exports = {
scan,
usn
findDeviceBySerialNo
}
2 changes: 1 addition & 1 deletion lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function doInstall(test, options, callback) {
} else {
usn = options.roku
}
find.usn(usn, 5, ip => {
find.findDeviceBySerialNo(usn, 5, ip => {
ip ? upload(options, ip, callback) : null
})
}
Expand Down
17 changes: 15 additions & 2 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs')
const path = require('path')
const properties = require('./properties')
const log = require('./log')
const xml2js = require('xml2js')
const request = require('request')
const properties = require('./properties')
const matchers = {
ip: /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,
usn: /^[A-Z0-9]{12}$/
Expand Down Expand Up @@ -54,9 +56,20 @@ function continueIfExists(object, message) {
}
}

function getDeviceInfo(ip, callback) {
request.get(`http://${ip}:8060/query/device-info`, (err, response, body) => {
if (body) {
xml2js.parseString(body, function (err, result) {
callback ? callback(result['device-info']) : null
})
}
})
}

module.exports = {
parseRoku,
parseAuth,
continueIfExists,
getAllSourceFiles
getAllSourceFiles,
getDeviceInfo
}

0 comments on commit bc8ee73

Please sign in to comment.