-
Notifications
You must be signed in to change notification settings - Fork 22
/
app.js
110 lines (102 loc) · 2.88 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
var express = require('express'),
path = require('path'),
config = require('./config'),
async = require('async'),
gpio = require('pi-gpio'),
app = express();
app.set('port', process.env.PORT || 3000);
app.use('/', express.static(__dirname + '/public'));
function delayPinWrite(pin, value, callback) {
setTimeout(function() {
gpio.write(pin, value, callback);
}, config.RELAY_TIMEOUT);
}
app.get("/api/ping", function(req, res) {
res.json("pong");
});
app.post("/api/garage/left", function(req, res) {
async.series([
function(callback) {
// Open pin for output
gpio.open(config.LEFT_GARAGE_PIN, "output", callback);
},
function(callback) {
// Turn the relay on
gpio.write(config.LEFT_GARAGE_PIN, config.RELAY_ON, callback);
},
function(callback) {
// Turn the relay off after delay to simulate button press
delayPinWrite(config.LEFT_GARAGE_PIN, config.RELAY_OFF, callback);
},
function(err, results) {
setTimeout(function() {
// Close pin from further writing
gpio.close(config.LEFT_GARAGE_PIN);
// Return json
res.json("ok");
}, config.RELAY_TIMEOUT);
}
]);
});
app.post("/api/garage/right", function(req, res) {
async.series([
function(callback) {
// Open pin for output
gpio.open(config.RIGHT_GARAGE_PIN, "output", callback);
},
function(callback) {
// Turn the relay on
gpio.write(config.RIGHT_GARAGE_PIN, config.RELAY_ON, callback);
},
function(callback) {
// Turn the relay off after delay to simulate button press
delayPinWrite(config.RIGHT_GARAGE_PIN, config.RELAY_OFF, callback);
},
function(err, results) {
setTimeout(function() {
// Close pin from further writing
gpio.close(config.RIGHT_GARAGE_PIN);
// Return json
res.json("ok");
}, config.RELAY_TIMEOUT);
}
]);
});
app.post("/api/garage/both", function(req, res) {
async.series([
function(callback) {
// Open pin for output
gpio.open(config.LEFT_GARAGE_PIN, "output", callback);
},
function(callback) {
// Open pin for output
gpio.open(config.RIGHT_GARAGE_PIN, "output", callback);
},
function(callback) {
// Turn the relay on
gpio.write(config.LEFT_GARAGE_PIN, config.RELAY_ON, callback);
},
function(callback) {
// Turn the relay on
gpio.write(config.RIGHT_GARAGE_PIN, config.RELAY_ON, callback);
},
function(callback) {
// Turn the relay off after delay to simulate button press
delayPinWrite(config.LEFT_GARAGE_PIN, config.RELAY_OFF, callback);
},
function(callback) {
// Turn the relay off after delay to simulate button press
delayPinWrite(config.RIGHT_GARAGE_PIN, config.RELAY_OFF, callback);
},
function(err, results) {
setTimeout(function() {
// Close pin from further writing
gpio.close(config.LEFT_GARAGE_PIN);
gpio.close(config.RIGHT_GARAGE_PIN);
// Return json
res.json("ok");
}, config.RELAY_TIMEOUT);
}
]);
});
app.listen(app.get('port'));