-
Notifications
You must be signed in to change notification settings - Fork 168
/
server.js
79 lines (71 loc) · 3.11 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
var async = require("async"),
wifi_manager = require("./app/wifi_manager")(),
dependency_manager = require("./app/dependency_manager")(),
config = require("./config.json");
/*****************************************************************************\
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.
\*****************************************************************************/
async.series([
// 1. Check if we have the required dependencies installed
function test_deps(next_step) {
dependency_manager.check_deps({
"binaries": ["dnsmasq", "hostapd", "iw"],
"files": ["/etc/dnsmasq.conf"]
}, 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.");
var reconfigure = config.access_point.force_reconfigure || false;
if (reconfigure) {
console.log("\nForce reconfigure enabled - try to enable access point");
} else {
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) {
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(error);
});
},
// 4. 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) {
console.log("\nHTTP server running...");
require("./app/api.js")(wifi_manager, next_step);
},
], function(error) {
if (error) {
console.log("ERROR: " + error);
}
});