Skip to content

Commit

Permalink
Wait until device is connected, delete profile if connection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Pau Moreno committed Nov 22, 2017
1 parent ac19e68 commit 9cf0c5f
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/windows-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@ var fs = require('fs');
var exec = require('child_process').exec;
var env = require('./env');

function execCommand(cmd) {
return new Promise(function(resolve, reject) {
exec(cmd, env, function(err, stdout, stderr) {
if (err) {
// Add command output to error, so it's easier to handle
err.stdout = stdout;
err.stderr = stderr;

reject(err);
} else {
resolve(stdout);
}
});
});
}

function connectToWifi(config) {

return function(ap, callback) {

var COMMANDS, com, connectToAPChain, i, j, l, len, ref, ssid, xmlContent;
var i, j, ref, ssid, xmlContent;
ssid = {
plaintext: ap.ssid,
hex: ""
Expand All @@ -21,24 +37,22 @@ function connectToWifi(config) {
xmlContent = win32WirelessProfileBuilder(ssid);
}
fs.writeFileSync(ap.ssid + ".xml", xmlContent);
COMMANDS = {
loadProfile: "netsh " + "wlan" + " add profile filename=\"" + ap.ssid + ".xml\"",
connect: "netsh " + "wlan" + " connect ssid=\"" + ap.ssid + "\" name=\"" + ap.ssid + "\""
};
connectToAPChain = ["loadProfile", "connect"];
var ERROR;
for (l = 0, len = connectToAPChain.length; l < len; l++) {
com = connectToAPChain[l];
exec(COMMANDS[com], env, function() {

exec("del \".\\" + ap.ssid + ".xml\"", env, function(err, resp) {

ERROR = err;
})
execCommand("netsh wlan add profile filename=\"" + ap.ssid + ".xml\"")
.then(function() {
return execCommand("netsh wlan connect ssid=\"" + ap.ssid + "\" name=\"" + ap.ssid + "\"");
})
.then(function() {
return execCommand("del \".\\" + ap.ssid + ".xml\"");
})
.then(function() {
callback && callback();
})
.catch(function(err) {
exec('netsh wlan delete profile "' + ap.ssid + '"', env, function() {
callback && callback(err);
});
})
}
var err = ERROR
callback && callback(err);
}
}

Expand Down

0 comments on commit 9cf0c5f

Please sign in to comment.