Skip to content

Commit

Permalink
Uses LAN and USB flags properly
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Oct 2, 2015
1 parent 85b1138 commit e4e8c0f
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 59 deletions.
32 changes: 22 additions & 10 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ Tessel.list = function(opts) {

// Keep a list of all the Tessels we discovered
var foundTessels = [];

// Options for Tessel discovery
var seekerOpts = {
timeout: opts.timeout * 1000,
usb: opts.usb,
lan: opts.lan
};

// Start looking for Tessels
var seeker = new discover.TesselSeeker().start(opts.timeout * 1000);
var seeker = new discover.TesselSeeker().start(seekerOpts);

// When a Tessel is found
seeker.on('tessel', function displayResults(tessel) {
if ((tessel.connection.connectionType === 'LAN' && opts.usb && !opts.lan) ||
(tessel.connection.connectionType === 'USB' && opts.lan && !opts.usb)) {
return;
}

var note = '';

Expand Down Expand Up @@ -68,7 +72,7 @@ Tessel.list = function(opts) {
// Report that selected Tessel to the user
logs.info('Multiple Tessels found.');
if (tessel) {
logs.info('Will default to', tessel.name, '.');
logs.info('Will default to %s.', tessel.name);
}
// Helpful instructions on how to switch
logs.info('Set default Tessel with environment variable (e.g. \'export TESSEL=bulbasaur\') or use the --name flag.');
Expand All @@ -94,12 +98,18 @@ Tessel.list = function(opts) {
Tessel.get = function(opts) {
return new Promise(function(resolve, reject) {
logs.info('Looking for your Tessel...');
// Store the amount of time to look for Tessel in seconds
var timeout = (opts.timeout || 2) * 1000;
// Collection variable as more Tessels are found
var tessels = [];

// Store the amount of time to look for Tessel in seconds
var seekerOpts = {
timeout: (opts.timeout || 2) * 1000,
usb: opts.usb,
lan: opts.lan
};

// Create a seeker object and start detecting any Tessels
var seeker = new discover.TesselSeeker().start(timeout);
var seeker = new discover.TesselSeeker().start(seekerOpts);

function searchComplete() {
// If we found no Tessels
Expand Down Expand Up @@ -425,6 +435,9 @@ controller.provisionTessel = function(opts) {
}
})
.then(function executeProvision() {
// We should only be using a USB connection
opts.usb = true;
// Fetch a Tessel
return controller.standardTesselCommand(opts, function(tessel) {
// Provision Tessel with SSH keys
return tessel.provisionTessel(opts);
Expand Down Expand Up @@ -455,7 +468,6 @@ controller.eraseScript = function(opts) {

controller.renameTessel = function(opts) {
opts = opts || {};

// Grab the preferred tessel
return new Promise(function(resolve, reject) {
if (!opts.reset && !opts.newName) {
Expand Down
34 changes: 26 additions & 8 deletions lib/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,37 @@ function TesselSeeker() {

util.inherits(TesselSeeker, EventEmitter);

TesselSeeker.prototype.start = function(timeout) {
TesselSeeker.prototype.start = function(opts) {
var self = this;
// Initialize the opts if it wasn't provided
opts = opts || {};

// An array of pending open connections
var pendingOpen = [];

self.lanScan = lan.startScan();

self.lanScan.on('connection', connectionHandler.bind(self));
// If no connection preference was supplied
if (!opts.usb && !opts.lan) {
// Default to all connections
opts.usb = opts.lan = true;
}

self.usbScan = usb.startScan();
// If the user has specifically requested lan devices
if (opts.lan) {
debug('Will scan for LAN devices');
// Start the scan
self.lanScan = lan.startScan();
// When we get a connection, handle it
self.lanScan.on('connection', connectionHandler.bind(self));
}

self.usbScan.on('connection', connectionHandler.bind(self));
// If the user has explicitly requested a USB search
if (opts.usb) {
debug('Will scan for USB devices');
// Start the scan
self.usbScan = usb.startScan();
// When we get a connection, handle it
self.usbScan.on('connection', connectionHandler.bind(self));
}

function connectionHandler(conn) {
var tessel = new Tessel(conn);
Expand Down Expand Up @@ -54,7 +72,7 @@ TesselSeeker.prototype.start = function(timeout) {
}

// If a timeout was provided
if (timeout && typeof timeout === 'number') {
if (opts.timeout && typeof opts.timeout === 'number') {
// Set a timeout function
self.scanTimeout = setTimeout(function() {
debug('Timeout hit! Waiting for pending to finish...');
Expand All @@ -65,7 +83,7 @@ TesselSeeker.prototype.start = function(timeout) {
debug('Done! Stopping.');
self.stop();
});
}, timeout);
}, opts.timeout);
}

return self;
Expand Down
12 changes: 6 additions & 6 deletions test/unit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ exports['Tessel.list'] = {
this.processOn = this.sandbox.stub(process, 'on');
this.activeSeeker = undefined;
this.seeker = this.sandbox.stub(Seeker, 'TesselSeeker', function Seeker() {
this.start = function(timeout) {
this.start = function(opts) {
self.activeSeeker = this;
if (timeout && typeof timeout === 'number') {
setTimeout(this.stop, timeout);
if (opts.timeout && typeof opts.timeout === 'number') {
setTimeout(this.stop, opts.timeout);
}
return this;
};
Expand Down Expand Up @@ -274,10 +274,10 @@ exports['Tessel.get'] = {
this.processOn = this.sandbox.stub(process, 'on');
this.activeSeeker = undefined;
this.seeker = this.sandbox.stub(Seeker, 'TesselSeeker', function Seeker() {
this.start = function(timeout) {
this.start = function(opts) {
self.activeSeeker = this;
if (timeout && typeof timeout === 'number') {
setTimeout(this.stop, timeout);
if (opts.timeout && typeof opts.timeout === 'number') {
setTimeout(this.stop, opts.timeout);
}
return this;
};
Expand Down

0 comments on commit e4e8c0f

Please sign in to comment.