-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (63 loc) · 2.41 KB
/
Copy pathindex.js
File metadata and controls
66 lines (63 loc) · 2.41 KB
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
let express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io')(server),
port = (process.env.PORT || 8000),
Particle = require('particle-api-js'),
Toilet = require('./Toilet'),
DummyToilet = Toilet.DummyToilet,
PhotonToilet = Toilet.PhotonToilet,
NODE_ENV = process.env.NODE_ENV || 'development',
PHOTON_DEVICES = (process.env.PHOTON_DEVICES || '').split(','),
toilets = [];
if(NODE_ENV == 'development') {
toilets.push(
new DummyToilet('men', 'men', 'ground floor', 'busy', io.sockets),
new DummyToilet('women', 'women', 'ground floor', 'available', io.sockets)
);
} else {
let particle = new Particle();
particle.login({username: process.env.PARTICLE_USERNAME, password: process.env.PARTICLE_PASSWORD}).then(
function(data){
console.log('API call completed on promise resolve: ', data.body.access_token);
const token = data.body.access_token;
PHOTON_DEVICES.forEach(function(device, index){
particle.getVariable({ deviceId: device, name: 'state', auth: token }).then(function(data) {
let currentStatus = data.body.result,
gender = process.env[device+'_GENDER'],
location = process.env[device+'_LOCATION'],
photon = new PhotonToilet(
particle,
token,
device,
gender,
location,
currentStatus,
io.sockets);
toilets.push(photon);
}, function(err) {
console.log('An error occurred while getting attrs:', err);
});
});
},
function(err) {
console.log('API call completed on promise fail: ', err);
}
);
}
app.set('port', port);
app.use(express.static('public'));
app.get('/toilets', function(req, res) {
res.send(toilets.map(function(toilet){ return toilet.detail() }) );
});
// deprecated endpoint, support till end of mac app
app.get('/status', function(req, res) {
let status;
toilets.forEach(function(toilet) {
if (toilet.name == 'frodo'){
status = toilet.status;
}
})
res.send({ status: status });
})
server.listen(app.get('port'));