forked from pasiaj/garage-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (64 loc) · 1.93 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
var express = require('express'),
path = require('path'),
config = require('./config'),
async = require('async'),
gpio = require('pi-gpio'),
rpio = require('rpio');
app = express();
app.set('port', process.env.PORT || 3000);
app.use('/', express.static(__dirname + '/public'));
var options = {
gpiomem: true, /* Use /dev/gpiomem */
mapping: 'physical', /* Use the P1-P40 numbering scheme */
}
rpio.init( options );
function delayPinWrite(pin, value, callback) {
setTimeout(function() {
rpio.write(pin, value);
callback();
//gpio.write(pin, value, callback);
}, config.RELAY_TIMEOUT);
}
app.get("/api/ping", function(req, res) {
res.json("pong");
});
app.get("/api/garage/doorStatus", function(req, res) {
var doorStatus = { openPin: "", closedPin: ""} ;
rpio.open(config.DOOR_OPEN_PIN, rpio.INPUT);
doorStatus.openPin = rpio.read(config.DOOR_OPEN_PIN);
rpio.close(config.DOOR_OPEN_PIN);
rpio.open(config.DOOR_CLOSED_PIN, rpio.INPUT);
doorStatus.closedPin = rpio.read(config.DOOR_CLOSED_PIN);
rpio.close(config.DOOR_CLOSED_PIN);
res.json( doorStatus );
});
app.post("/api/garage/left", function(req, res) {
async.series([
function(callback) {
// Open pin for output
rpio.open(config.GARAGE_PIN, rpio.OUTPUT, rpio.LOW);
callback();
//gpio.open(config.GARAGE_PIN, "output", callback);
},
function(callback) {
// Turn the relay on
rpio.write(config.GARAGE_PIN, config.RELAY_ON);
callback();
//gpio.write(config.GARAGE_PIN, config.RELAY_ON, callback);
},
function(callback) {
// Turn the relay off after delay to simulate button press
delayPinWrite(config.GARAGE_PIN, config.RELAY_OFF, callback);
},
function(err, results) {
setTimeout(function() {
// Close pin from further writing
rpio.close(config.GARAGE_PIN);
//gpio.close(config.GARAGE_PIN);
// Return json
res.json("ok");
}, config.RELAY_TIMEOUT);
}
]);
});
app.listen(app.get('port'));