-
Notifications
You must be signed in to change notification settings - Fork 9
/
garagepi.js
140 lines (120 loc) · 3.78 KB
/
garagepi.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var GPIO = require("onoff").Gpio;
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var startTakingSnaps = false;
require('console-stamp')(console, 'yyyy/mm/dd HH:MM:ss');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
function customLog(tokens, req, res) {
return [
'[' + tokens.date(req, res, 'clf') + ']',
'"' + tokens.method(req, res),
tokens.url(req, res) + ' HTTP/' + tokens['http-version'](req, res) + '"',
tokens.status(req, res),
tokens.res(req, res, 'content-length')
].join(' ')
}
app.use(logger(customLog, {
skip: function(req, res) {
// don't bother logging status requests as they are too frequent
if(req.query.state === 'status') return true;
}
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.render('index.html');
});
let state = 'closed';
app.get('/api/clickbutton', function(req, res) {
console.log(`action: clickbutton state: ${state}`);
outputSequence(7, '10', 1000);
res.setHeader('Content-Type', 'application/json');
res.end(state);
});
app.get('/api/openclose', function(req, res) {
if((req.query.state === 'on' && state === 'closed') ||
(req.query.state === 'off' && state === 'open')) {
outputSequence(7, '10', 1000);
state = req.query.state === 'on' ? 'open' : 'closed';
console.log(`action: openclose state: ${state}`);
} else {
console.log(`action: openclose ${req.query.state} already matches current state`);
}
res.setHeader('Content-Type', 'application/json');
res.end(state);
});
app.get('/api/sensor', function(req, res) {
state = req.query.state === 'on' ? 'open' : 'closed';
console.log(`action: sensor state: ${state}`);
res.setHeader('Content-Type', 'application/json');
res.end(state);
});
app.get('/api/status', function(req, res) {
res.setHeader('Content-Type', 'application/json');
//console.log(`action: status state: ${state}`);
res.end(state);
});
function outputSequence(pin, seq, timeout) {
var gpio = new GPIO(4, 'out');
gpioWrite(gpio, pin, seq, timeout);
}
function gpioWrite(gpio, pin, seq, timeout) {
if (!seq || seq.length <= 0) {
console.log('closing pin:', pin);
gpio.unexport();
return;
}
var value = seq.substr(0, 1);
seq = seq.substr(1);
setTimeout(function() {
console.log('gpioWrite, value:', value, ' seq:', seq);
gpio.writeSync(value);
gpioWrite(gpio, pin, seq, timeout);
}, timeout);
}
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
function takeSnaps() {
var autoSnapshot = setTimeout(function() {
var imgPath = path.join(__dirname, 'public/images');
var cmd = 'raspistill -w 640 -h 480 -q 80 -o ' + imgPath + '/garage.jpg';
var exec = require('child_process').exec;
exec(cmd, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return;
}
io.emit('snapshot', 'ready');
console.log('snapshot created...');
if(startTakingSnaps) {
takeSnaps();
}
});
}, 0);
return autoSnapshot;
}
io.on('connection', function(socket){
console.log('a user connected');
startTakingSnaps = true;
takeSnaps();
socket.on('disconnect', function(){
console.log('user disconnected');
startTakingSnaps = false;
});
});
var port = process.env.PORT || 8000;
server.listen(port, function() {
console.log('GaragePi listening on port:', port);
});