forked from mcampa/raspberry-wifi-conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
115 lines (103 loc) · 4.25 KB
/
server.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var async = require("async"),
wifi_manager = require("./app/wifi_manager")(),
dependency_manager = require("./app/dependency_manager")(),
exec = require("child_process").exec,
config = require("./config.json"),
GPIO = require('onoff').Gpio,
button = new GPIO(18, 'in', 'both');
/*****************************************************************************\
1. Check for dependencies
2. Check to see if we are connected to a wifi AP
3. If connected to a wifi, do nothing -> exit
4. Convert RPI to act as a AP (with a configurable SSID)
5. Host a lightweight HTTP server which allows for the user to connect and
configure the RPIs wifi connection. The interfaces exposed are RESTy so
other applications can similarly implement their own UIs around the
data returned.
6. Once the RPI is successfully configured, reset it to act as a wifi
device (not AP anymore), and setup its wifi network based on what the
user picked.
7. At this stage, the RPI is named, and has a valid wifi connection which
its bound to, reboot the pi and re-run this script on startup.
\*****************************************************************************/
var configuring = 0;
function buttonCallback(err, state) {
// check the state of the button
// 1 == pressed, 0 == not pressed
// console.log('conf:'+configuring);
if( (state == 1) && (configuring == 0) ) {
console.log('pressed');
configuring = 1;
wifi_manager.enable_ap_mode(config.access_point.ssid, function(error) {
if(error) {
console.log("BUTTON... AP Enable ERROR: " + error);
}else{
console.log("BUTTON... AP Enable Success!");
configuring = 0;
exec("systemctl enable hostapd.service", function(error, stdout, stderr) { });
exec("systemctl enable isc-dhcp-server.service", function(error, stdout, stderr) { });
exec("systemctl disable wpa_supplicant.service", function(error, stdout, stderr) { });
exec("reboot", function(error, stdout, stderr) { });
}
});
} /* else {
console.log('unpressed');
}*/
}
async.series([
// 1. Check if we have the required dependencies installed
function test_deps(next_step) {
dependency_manager.check_deps({
"binaries": ["dhcpd", "hostapd", "iw"],
"files": ["/etc/init.d/isc-dhcp-server"]
}, function(error) {
if (error) console.log(" * Dependency error, did you run `sudo npm run-script provision`?");
next_step(error);
});
},
// 2. Check if wifi is enabled / connected
function test_is_wifi_enabled(next_step) {
wifi_manager.is_wifi_enabled(function(error, result_ip) {
if (result_ip) {
console.log("\nWifi is enabled, and IP " + result_ip + " assigned");
if(config.button != 'on') {
process.exit(0);
}
} else {
console.log("\nWifi is not enabled, Enabling AP for self-configure");
}
next_step(error);
});
},
// 3. Turn RPI into an access point
function enable_rpi_ap(next_step) {
if(config.button != 'on') {
wifi_manager.enable_ap_mode(config.access_point.ssid, function(error) {
if(error) {
console.log("... AP Enable ERROR: " + error);
} else {
console.log("... AP Enable Success!");
}
});
}
next_step('');
},
// 4. attach a callback that check if the button is pressed
function button_check(next_step) {
if(config.button == 'on') {
button.watch(buttonCallback);
}
next_step('');
},
// 5. Host HTTP server while functioning as AP, the "api.js"
// file contains all the needed logic to get a basic express
// server up. It uses a small angular application which allows
// us to choose the wifi of our choosing.
function start_http_server(next_step) {
require("./app/api.js")(wifi_manager, next_step);
},
], function(error) {
if (error) {
console.log("ERROR: " + error);
}
});