Skip to content

Commit

Permalink
Add neutral wifi command to list Tessel Wifi Information
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNice committed Sep 7, 2015
1 parent 47219b6 commit b14873a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 16 deletions.
37 changes: 25 additions & 12 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ function closeSuccessfulCommand() {
process.exit(0);
}

function closeFailedCommand(err) {
// If the returned value is an error
// Allow options to be partially applied
function closeFailedCommand(opts, err) {
if (!err) {
err = opts;
opts = {};
}

if (err instanceof Error) {
// Throw it
throw err;
} else {
// Print a stern warning by default
opts.type = opts.type || 'warn';
logs[opts.type](err);
}
// Otherwise
else {
// Print a stern warning
logs.warn(err);
}
// Exit with non-zero code
// NOTE: Exit code is non-zero
process.exit(1);
}

Expand Down Expand Up @@ -186,12 +189,22 @@ parser.command('init')

parser.command('wifi')
.callback(function(opts) {
//TODO: Refactor switch case into controller.wifi
// TODO: Refactor switch case into controller.wifi
if (opts.list) {
controller.printAvailableNetworks(opts)
.then(closeSuccessfulCommand, closeFailedCommand);
} else if (opts.ssid && opts.password) {
controller.connectToNetwork(opts)
} else if (opts.ssid || opts.password) {
if (opts.ssid && opts.password) {
controller.connectToNetwork(opts)
.then(closeSuccessfulCommand, closeFailedCommand);
} else {
var msg = opts.ssid ?
'Please provide a password with -p <password>' :
'Please provide a network name (SSID) with -n <name>';
closeFailedCommand({type: 'err'}, msg);
}
} else {
controller.getWifiInfo(opts)
.then(closeSuccessfulCommand, closeFailedCommand);
}
})
Expand Down
25 changes: 25 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,31 @@ controller.printAvailableNetworks = function(opts) {
});
};

controller.getWifiInfo = function(opts) {
return Tessel.get(opts)
.then(function(selectedTessel) {
return selectedTessel.getWifiInfo()
.then(function(network) {
// Grab inet lines, flatmap them, remove empty
// Wanted to do this with awk and cut inside commands.js
var ips = network.ips.filter(function(item) { return /inet/.exec(item) })
.map(function(line) { return line.split(' ') })
.reduce(function(a, b) { return a.concat(b) })
.filter(function(item) { return /addr/.exec(item) })
.map(function(chunk) { return chunk.split(':')[1] })
.filter(function(addr) { return addr.length });

logs.info('Connected to "' + network.ssid + '"');
ips.forEach(function(ip) { logs.info('IP Address: ' + ip) });
logs.info('Signal Strength: (' + network.quality + '/' + network.quality_max + ')');
logs.info('Bitrate: ' + Math.round(network.bitrate / 1000) + 'mbps');
})
.then(function() {
return controller.closeTesselConnections(selectedTessel);
});
});
}

controller.connectToNetwork = function(opts) {
// Grab the preferred Tessel
return Tessel.get(opts)
Expand Down
6 changes: 6 additions & 0 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module.exports.readFile = function(filepath) {
module.exports.scanWiFi = function() {
return ['ubus', 'call', 'iwinfo', 'scan', '{"device":"wlan0"}'];
};
module.exports.getWifiInfo = function() {
return ['ubus', 'call', 'iwinfo', 'info', '{"device":"wlan0"}'];
}
module.exports.getIPAddress = function() {
return ['ifconfig', 'wlan0'];
};
module.exports.stopRunningScript = function() {
return ['/etc/init.d/tessel-app', 'stop'];
};
Expand Down
60 changes: 56 additions & 4 deletions lib/tessel/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var Tessel = require('./tessel'),
logs = require('../logs'),
Promise = require('bluebird');

function logErr(d) {
logs.err(d.toString());
}

Tessel.prototype.findAvailableNetworks = function() {
var self = this;
return new Promise(function(resolve, reject) {
Expand All @@ -12,9 +16,7 @@ Tessel.prototype.findAvailableNetworks = function() {

var resultsJSON = '';

remoteProcess.stderr.on('data', function(d) {
logs.err(d.toString());
});
remoteProcess.stderr.on('data', logErr);

// Gather the results
remoteProcess.stdout.on('data', function(d) {
Expand Down Expand Up @@ -56,6 +58,56 @@ Tessel.prototype.findAvailableNetworks = function() {
});
};

Tessel.prototype.getWifiInfo = function() {
var self = this;
return new Promise(function(resolve, reject) {
return self.connection.exec(commands.getWifiInfo())
.then(function(remoteProcess) {
var resultsJSON = '';

remoteProcess.stdout.on('data', function(d) {
resultsJSON += d;
});
remoteProcess.stderr.on('data', logErr);
remoteProcess.once('close', function() {
try {
var network = JSON.parse(resultsJSON);
} catch (err) {
return self.connection.end()
.then(function() {
reject(err);
});
}

return self.connection.exec(commands.getIPAddress())
.then(function(rp) {
var result = '';

rp.stdout.on('data', function(d) {
result += d;
});
rp.stderr.on('data', logErr);
rp.once('close', function() {
try {
network.ips = result.split('\n');
} catch (err) {
return self.connection.end()
.then(function() {
reject(err);
});
}

return self.connection.end()
.then(function() {
return resolve(network);
});
});
});
});
});
});
};

function compareBySignal(a, b) {
if ((a.quality / a.quality_max) > (b.quality / b.quality_max)) {
return -1;
Expand Down Expand Up @@ -95,7 +147,7 @@ Tessel.prototype.connectToNetwork = function(opts) {
.then(function(remoteProcess) {
// Once the credentials have been comitted
remoteProcess.once('close', function() {
// Restart the wifi
// Restart the wifi
return self.connection.exec(commands.reconnectWifi())
.then(function(remoteProcess) {
// Once the wifi restart process closes
Expand Down

0 comments on commit b14873a

Please sign in to comment.