-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaemon.js
54 lines (48 loc) · 1.56 KB
/
daemon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require('shelljs/global');
var request = require('request');
if (startWPA() === 'failed') {
exec(__dirname + '/scripts/wpa_util.sh stop', {silent:true});
setTimeout(startAPMode, 0);
} else {
postInfo();
}
function startWPA() {
console.error('starting wpa_supplicant...');
var wpaOut = exec(__dirname + '/scripts/wpa_util.sh start', {silent:true}).stdout;
console.error('wpa:', wpaOut.toString().trim());
return wpaOut.toString().trim();
}
function startAPMode() {
//need to enable AP mode
console.error('enable AP mode');
var hostapOut = exec(__dirname + '/scripts/hostap_util.sh start', {silent:true}).stdout;
if (hostapOut.toString().trim() === 'connected') {
//start web server for setting ssid/passwd
console.error('starting webserver...');
exec('node ' + __dirname + '/server.js', function(code, stdout, stderr) {
console.error('webserver stopped');
//web server stopped, lets try to start wpa
exec(__dirname + '/scripts/hostap_util.sh stop', {silent:true});
if (startWPA() === 'failed') {
exec(__dirname + '/scripts/wpa_util.sh stop', {silent:true});
setTimeout(startAPMode,0);
} else {
postInfo();
}
});
}
}
function postInfo() {
var info = getNICInfo().split(',');
request.post('http://humix-wireless-board.mybluemix.net/add',
{'form':
{
'hwaddr':info[0].trim(), 'ipaddr': info[1].trim()
}
}
);
}
function getNICInfo() {
var output = exec(__dirname + '/scripts/getNICInfo.sh wlan0', {silent:true}).stdout;
return output.toString().trim();
}