-
Notifications
You must be signed in to change notification settings - Fork 47
/
lightserver.js
65 lines (52 loc) · 1.79 KB
/
lightserver.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
// server.js
//Run with: node PORT=8081 server.js
//Run with ports below 1024 with sudo: node PORT=80 server.js
// BASE SETUP
// ==============================================
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
var Gpio = require('onoff').Gpio;
var redled = new Gpio(22, 'out');
var greenled = new Gpio(27, 'out');
// ROUTES
// ==============================================
app.post('/echoData', function(req, res){
response = JSON.parse(req.body.response);
//console.log(response.activity.creationTimestamp);
description = JSON.parse(response.activity.description);
summary = description.summary
console.log(summary);
if (summary.indexOf("on the green light")!=-1) {
greenled.writeSync(1);
}
if (summary.indexOf("off the green light")!=-1) {
greenled.writeSync(0);
}
if (summary.indexOf("on the red light")!=-1) {
redled.writeSync(1);
}
if (summary.indexOf("off the red light")!=-1) {
redled.writeSync(0);
}
res.sendStatus(200);
});
// START THE SERVER
//getIPAddress from http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js/15075395#15075395
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
// ==============================================
app.listen(port, getIPAddress());
console.log('echoMonitor watching ', port, "at address", getIPAddress());