-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from cazacutudor/refactor-find-command
Refactor find command
- Loading branch information
Showing
4 changed files
with
42 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters