-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (62 loc) · 1.57 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
var express = require('express'),
app = express(),
GPIO = require('./RasPi_GPIO');
var pin7 = new GPIO(7, function(err) {
if (err) {
console.log(err);
}
});
var pin8 = new GPIO(8, function(err) {
if (err) {
console.log(err);
}
});
app.put('/api/gpio/:pin_num/:pin_value', function(req, res) {
var pin_num = parseInt(req.params.pin_num),
pin_value = req.params.pin_value.toLowerCase(),
io;
if (pin_num === 8) {
io = pin8;
} else if (pin_num === 7) {
io = pin7;
} else {
return res.status(404).send('Unsupported GPIO Pin: ' + pin_num);
}
io.write(pin_value, function(err, result) {
if (err) {
res.status(500).send(err.type + " : " + err.data);
} else {
res.status(200).end();
}
});
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log('RasPi app listening on port %s', port)
});
process.stdin.resume(); //so the program will not close instantly
function exitHandler(options, err) {
pin7.unexport();
pin8.unexport();
if (options.cleanup) {
//console.log('clean');
}
if (err) {
console.error(err.stack);
}
if (options.exit) {
process.exit();
}
}
//do something when app is closing
process.on('exit', exitHandler.bind(null, {
cleanup: true
}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {
exit: true
}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {
exit: true
}));