Skip to content

Commit

Permalink
Minor changes to wifi configuration output.
Browse files Browse the repository at this point in the history
- Additional minor/trivial code refactoring.
- Updates/additions to tests

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jun 12, 2017
1 parent d2f7f71 commit 1c81a03
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 97 deletions.
113 changes: 66 additions & 47 deletions lib/tessel/wifi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

// System Objects
// ...

Expand Down Expand Up @@ -83,20 +85,21 @@ Tessel.prototype.getWifiInfo = function() {
};

function safeQualityExprEvaluation(expr) {
var parsed = /(\d.*)(?:\/)(\d.*)/.exec(expr);
var isNumber = parsed === null && typeof + expr === 'number' && !Number.isNaN(+expr);
const parsed = /(\d.*)(?:\/)(\d.*)/.exec(expr);
const nexpr = +expr;
const isNumber = parsed === null && typeof nexpr === 'number' && !Number.isNaN(nexpr);

// If the expression doesn't match "\d.*/\d.*",
// but IS a number, then return the number. Otherwise,
// evaluate the expression as division. ToNumber is
// applied implicitly. If the expression didn't parse
// safely, return 0.
return isNumber ? +expr : (parsed && parsed.length === 3 ? parsed[1] / parsed[2] : 0);
return isNumber ? nexpr : (parsed && parsed.length === 3 ? parsed[1] / parsed[2] : 0);
}

function compareBySignal(a, b) {
var ae = safeQualityExprEvaluation(a.quality);
var be = safeQualityExprEvaluation(b.quality);
const ae = safeQualityExprEvaluation(a.quality);
const be = safeQualityExprEvaluation(b.quality);

if (ae > be) {
return -1;
Expand All @@ -107,63 +110,78 @@ function compareBySignal(a, b) {
}
}

Tessel.prototype.connectToNetwork = function(opts) {
var ssid = opts.ssid;
var password = opts.password;
var security = opts.security;
var status = 'Wifi Connected.';
Tessel.prototype.connectToNetwork = function(options) {
const ssid = options.ssid;
const password = options.password;

let security = options.security;
let enable = true;

if (password && !security) {
security = 'psk2';
}

status += ` SSID: ${ssid}`;
let configured = 'Wifi Configured.';
let configuration = `SSID: ${ssid}`;

if (password && security) {
status += `, password: ${password}, security: ${security}`;
let passToDisplay;

if (options.showpassword) {
passToDisplay = password;
} else {
let rpass = /(^[a-z0-9])(.*?)([a-z0-9])$/ig;
passToDisplay = password.replace(rpass, (w, f, m, l) => `${f}${'*'.repeat(m.length)}${l}`);
}

configuration += `, password: ${passToDisplay}, security: ${security}`;
}

if (!password && !security) {
security = 'none';
}

var setSSID = () => this.simpleExec(commands.setNetworkSSID(ssid));

var setNetworkPassword = () => this.simpleExec(commands.setNetworkPassword(password));

var setNetworkSecurity = () => this.simpleExec(commands.setNetworkEncryption(security));

var turnWifiOn = () => this.setWiFiState(true);

var logStatus = () => {
log.info(status);
};
const setSSID = () => {
let resolution = Promise.resolve();

var setup = () => {
if (password) {
return setSSID()
.then(setNetworkPassword)
.then(setNetworkSecurity);
} else {
return setSSID()
.then(setNetworkSecurity);
resolution = this.simpleExec(commands.setNetworkPassword(password));
}
};

return resolution.then(
() => this.simpleExec(commands.setNetworkSSID(ssid))
);
};
const setNetworkSecurity = () => this.simpleExec(commands.setNetworkEncryption(security));
const turnWifiOn = () => this.setWiFiState({
enable
});

return setup()
return setSSID()
.then(setNetworkSecurity)
.then(turnWifiOn)
.then(logStatus);
.then(() => {
log.info(`${configured} (${configuration})`);
log.info(`Wifi Connected.`);
});
};

Tessel.prototype.resetMDNS = function() {
return this.simpleExec(commands.callMDNSDaemon('restart'))
.then(() => this.simpleExec(commands.callTesselMDNS('restart')));
};

Tessel.prototype.setWiFiState = function(enable) {
Tessel.prototype.setWiFiState = function(state) {
if (!state) {
return Promise.reject(new Error('Missing Wifi State: (empty).'));
}

if (typeof state.enable === 'undefined') {
return Promise.reject(new Error('Missing Wifi State: property "enable" not provided.'));
}

return new Promise((resolve, reject) => {
return this.simpleExec(commands.turnOnWifi(enable))
return this.simpleExec(commands.turnOnWifi(state.enable))
.then(() => this.simpleExec(commands.commitWirelessCredentials()))
.then(() => {
return this.simpleExec(commands.reconnectWifi())
Expand All @@ -178,11 +196,12 @@ Tessel.prototype.setWiFiState = function(enable) {
});
})
.then(() => {
var settle = (rejection) => {
const enabledOrDisabled = state.enable ? 'Enabled' : 'Disabled';
const settle = (rejection) => {
if (rejection) {
reject(rejection);
} else {
log.info('Wifi', enable ? 'Enabled.' : 'Disabled.');
log.info(`Wifi ${enabledOrDisabled}.`);
resolve();
}
};
Expand All @@ -207,19 +226,19 @@ Tessel.prototype.setWiFiState = function(enable) {
the full 10 second timeout, with a higher likelihood of success (that is, zero failures were
observed when using a valid ssid + password + security).
*/
var tries = 10;
var pollForWifiSignal = () => {
this.connection.exec(commands.getWifiInfo(), (err, remoteProcess) => {
if (err) {
return reject(err);
let tries = 10;
const pollForWifiSignal = () => {
this.connection.exec(commands.getWifiInfo(), (error, remoteProcess) => {
if (error) {
return reject(error);
}
this.receive(remoteProcess, (err, result) => {
if (err) {
if (err.toString().includes('Not found') && tries) {
this.receive(remoteProcess, (error, result) => {
if (error) {
if (error.toString().includes('Not found') && tries) {
tries--;
return pollForWifiSignal();
} else {
return reject(err);
return reject(error);
}
}
if (result.toString().includes('signal')) {
Expand All @@ -237,7 +256,7 @@ Tessel.prototype.setWiFiState = function(enable) {
});
};

if (enable) {
if (state.enable) {
pollForWifiSignal();
} else {
settle();
Expand Down
Loading

0 comments on commit 1c81a03

Please sign in to comment.