-
Notifications
You must be signed in to change notification settings - Fork 1
/
garage.js
200 lines (166 loc) · 4.32 KB
/
garage.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var express = require('express');
var bodyParser = require('body-parser');
var https = require('https');
var fs = require('fs');
var keys = require('./keys');
var currentVersion = '0.5.3';
var currentName = 'Open Garage';
var capi = 'v1';
var httpsPort = 8000;
// GPIO setup
var Gpio = require('onoff').Gpio,
//door status GPIO
doorStatus = new Gpio(4, 'out', 'both'),
// door relay GPIO
doorRelay = new Gpio(17, 'high');
var currentDoorStatus = 0;
// watch the door status
doorStatus.watch(function(err, value) {
if (err) {
logAPICall('Door status changed', true, garageStateToString(currentDoorStatus));
return;
}
if (currentDoorStatus != value) {
currentDoorStatus = value;
logAPICall('Door status changed', false, garageStateToString(currentDoorStatus));
}
});
// permited keys
var permitedKeys = keys.permitedKeys;
// SSL key / cert
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var app = express();
// default route
app.get('/', function (req, res) {
res.send(currentName + ' ' + currentVersion);
});
app.use(bodyParser.json());
// standard html site
app.get(['/', '/api/' + capi], function(req, res) {
result = { name: currentName, version: currentVersion }
res.contentType('application/json');
res.send(JSON.stringify(result));
});
/**
* API TOGGLE RETURN CODES:
* 0: Everything is OK
* -1: Wrong or missing access token
* -2: Wrong or missing state value
*/
// api call: toggle
app.post('/api/' + capi + '/toggle', function (req, res) {
var statuscode = 0;
var token = req.body.token;
var debug = req.body.debug;
var doorState = req.body.state;
if (isTokenValid(token)) {
currentStatus = doorStatus.readSync();
switch (doorState) {
case 'close': // close
if (currentStatus != 0) {
if (debug == 'on') {
logAPICall('DEBUG Close', false, 'token: ' + token + ' state: ' + doorState);
} else {
logAPICall('Close', false, 'token: ' + token + ' state: ' + doorState);
toggleDoor();
}
}
break;
case 'open': // open
if (currentStatus != 1) {
if (debug == 'on') {
logAPICall('DEBUG Open', false, 'token: ' + token + ' state: ' + doorState);
} else {
logAPICall('Open', false, 'token: ' + token + ' state: ' + doorState);
toggleDoor();
}
}
break;
case 'toggle': // toggle
if (debug == 'on') {
logAPICall('DEBUG Toggle', false, 'token: ' + token + ' state: ' + doorState);
} else {
logAPICall('Toggle Open', false, 'token: ' + token + ' state: ' + doorState);
toggleDoor();
}
break;
default:
statuscode = -2;
logAPICall('Toggle', true, 'token: ' + token + ' state: ' + doorState);
break;
}
} else {
statuscode = -1;
logAPICall('Toggle', true, 'token: ' + token + ' state: ' + doorState);
}
// create response
res.contentType('application/json');
result = { status: statuscode };
res.send(JSON.stringify(result));
});
/**
* API STATUS CODES:
* 0: Door is closed
* 1: Door is open
* -1: Wrong or missing access token
*/
// api call: status
app.post('/api/' + capi + '/status', function(req, res) {
var statuscode = -1;
var token = req.body.token;
if (isTokenValid(token)) {
statuscode = doorStatus.readSync();
logAPICall('Status', false, 'token: ' + token + ' state: ' + statuscode);
}
// create response
res.contentType('application/json');
result = { status: statuscode };
res.send(JSON.stringify(result));
});
// start the server
https.createServer(options, app).listen(httpsPort);
// toggle the relay for the garage door
function toggleDoor() {
doorRelay.writeSync(0);
// wait 1 second
setTimeout(function() {
doorRelay.writeSync(1);
}, 1000);
}
// check if auth token is valid
function isTokenValid(token) {
if (permitedKeys.indexOf(token) > -1) {
return true;
} else {
return false;
}
}
// write into log file
function logAPICall(apiCall, error, message) {
var apiTxt = 'API';
if (error) {
apiTxt = 'ERROR';
}
var logText = apiTxt + ': ' + apiCall + ' ' + message + ' Date: ' + Date().toString();
fs.writeFile("/tmp/garage.log", logText, function(err) {
if (err) {
return console.log(err);
}
console.log(logText);
});
}
function garageStateToString(doorState) {
var stateString = 'ERROR';
switch (doorState) {
case 0:
stateString = 'CLOSED';
break;
case 1:
stateString = 'OPEN';
break;
}
return stateString;
}