-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
189 lines (149 loc) · 4.78 KB
/
app.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var express = require('express');
var bodyParser = require('body-parser');
var child_process = require('child_process');
var fs = require('fs');
var hardware = require('./hardware.js')();
function isUUID(value) {
return value && typeof(value) === 'string';
}
function isSSID(value) {
return value && typeof(value) === 'string';
}
function isPassphrase(value) {
return value && typeof(value) === 'string';
}
function isURL(value) {
return value && typeof(value) === 'string';
}
function isToken(value) {
return value && typeof(value) === 'string';
}
var commissioning = module.exports = function(options, M) {
var app = express.Router();
M = M || { };
M.fs = M.fs || fs;
M.exec = M.exec || child_process.exec;
M.pifi = M.pifi || 'pifi';
M.interface = M.interface || 'wlan0';
M.tokenFile = M.tokenFile || './share/chillhub.json';
var base_command = [
'cd ./share &&',
M.pifi,
M.interface
];
function pifi_state(callback) {
var command = base_command.concat('-s').join(' ');
M.exec(command, function(e, stdout, stderr) {
if (e) callback(e);
else callback(null, stdout.trim());
});
}
function idle(callback) {
var command = base_command.concat('-i').join(' ');
M.exec(command, function(e, stdout, stderr) {
callback(e);
});
}
function access_point(callback) {
var ssid = 'ChillHub-' + options.uuid.substr(0, 8);
var passphrase = options.passphrase;
var command = base_command.concat('-a', '"' + ssid + '"', '"' + passphrase + '"').join(' ');
M.exec(command, function(e, stdout, stderr) {
callback(e);
});
}
function wireless_connect(ssid, passphrase, callback) {
var command = base_command.concat('-w', '"' + ssid + '"', '"' + passphrase + '"').join(' ');
M.exec(command, function(e, stdout, stderr) {
callback(e);
});
}
function update_led() {
pifi_state(function(e, state) {
if (state == 'Idle') hardware.ledOff();
else if (state == 'WiFi_Connected') hardware.ledOn();
else hardware.ledFlash();
});
setTimeout(update_led, 1000);
}
update_led();
hardware.listen(function(error, event) {
if (error) return console.error('hardware error:', error);
if (event == 'BUTTON_PRESS_SHORT') {
pifi_state(function(e, state) {
if (state == 'Idle') {
access_point(function(e) {
console.log('Started access point');
});
}
else {
console.log('Ignoring short button press in state', state);
}
});
}
else if (event == 'BUTTON_PRESS_LONG') {
console.log('Going to idle state');
idle(function() { });
}
else {
console.error('Unknown event:', event);
}
});
app.use(bodyParser.json());
app.get('/', function(req, res) {
var uuid = options.uuid;
if (!isUUID(uuid)) {
return res.status(400).json({ kind: 'error#input-validation', property: 'uuid' });
}
else {
return res.status(200).json({ uuid: uuid });
}
});
app.post('/auth', function(req, res) {
var token = req.body.token;
var url = req.body.url;
if (!isToken(token)) {
return res.status(400).json({ kind: 'error#input-validation', property: 'token' });
}
else if (!isURL(url)) {
return res.status(400).json({ kind: 'error#input-validation', property: 'url' });
}
else {
pifi_state(function(e, state) {
if (e) return res.status(500).json({ kind: 'error#auth', error: e });
if (state != "AccessPoint_Hosting") return res.status(403).json({ kind: 'error#permission-denied' });
options.firebaseUrl = url;
options.token = token;
M.fs.writeFileSync(M.tokenFile, JSON.stringify(options));
return res.status(200).json({ });
});
}
});
app.get('/networks', function(req, res) {
var command = base_command.concat('-l');
M.exec(command.join(' '), function(e, stdout, stderr) {
if (e) return res.status(500).json({ kind: 'error#network-list', error: e });
stdout = stdout.trim();
var networks = (stdout ? stdout.split(',') : []).map(function(ssid) {
return { ssid: ssid };
});
return res.status(200).json(networks);
});
});
app.post('/networks', function(req, res) {
var ssid = req.body.ssid, passphrase = req.body.passphrase;
if (!isSSID(ssid)) {
return res.status(400).json({ kind: 'error#input-validation', property: 'ssid' });
}
if (!isPassphrase(passphrase)) {
return res.status(400).json({ kind: 'error#input-validation', property: 'passphrase' });
}
res.status(200).json({ ssid: ssid });
wireless_connect(ssid, passphrase, function() { });
});
app.delete('/networks', function(req, res) {
res.status(204).end();
access_point(function() { });
});
return app;
};