Skip to content

Instantly share code, notes, and snippets.

@jschr
Last active September 14, 2018 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschr/99cc87ee79ff976222869ac9d138a922 to your computer and use it in GitHub Desktop.
Save jschr/99cc87ee79ff976222869ac9d138a922 to your computer and use it in GitHub Desktop.
Kill resin-wifi-connect on failed attempt.
import { spawn } from 'child-process-promise';
import readline from 'readline';
export default function wifiConnect(ssid) {
const promise = spawn('wifi-connect', [`--portal-ssid=${ssid}`]);
const childProcess = promise.childProcess;
readline
.createInterface({
input: childProcess.stderr,
terminal: false,
})
.on('line', line => {
// Kill wifi-connect when we fail to connect to the network to restart
// the connect loop so we can display on-screen feedback to the user.
// This is a workaround for https://github.com/resin-io/resin-wifi-connect/issues/224.
if (/Connection to access point not activated/i.test(line)) {
// Resolves the promise and stops wifi-connect.
childProcess.kill('SIGTERM');
}
});
return promise;
}
@jschr
Copy link
Author

jschr commented Sep 14, 2018

This module will launch resin-wifi-connect and kill the process when an unsuccessful attempt is made. resin-wifi-connect will exit normally with a successful connection.

Once the promise resolves you can ping an external service (ie. google.com) to check whether a connection was successful or failed — at which point you can surface an error message and restart wifi-connect.

The feedback loop for the user is fairly delayed but it's better than no feedback at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment