-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
76 lines (62 loc) · 1.94 KB
/
routes.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
const routes = require('express').Router();
const os = require('os');
const ip = require('ip');
routes.use(function timeLog (req, res, next) {
console.log(`${req.ip} @ ${new Date()} - ${req.originalUrl}`);
next();
})
// Default route - provide basic information about the API
routes.get('/', (req, res) => {
let responseMessage = {};
responseMessage.boot_status = req.app.locals.bootData;
responseMessage.current_status = {
date: new Date(),
ip: ip.address(),
load: os.loadavg(),
uptime: os.uptime(),
memory: {
total: os.totalmem(),
free: os.freemem()
}
}
responseMessage.api = [
{
name: 'air',
path: `http://${ip.address()}/air`,
description: 'Garage air temperature and humidity.'
},
{
name: 'door',
path: `http://${ip.address()}/door`,
description: 'Garage door status.'
},
{
name: 'trigger',
path: `http://${ip.address()}/trigger`,
description: 'Trigger the garage door button.'
}
]
res.status(200).json(responseMessage);
});
// Return the air temperature details (these are refreshed in the background every minute)
routes.get('/air', (req, res) => {
res.status(200).json({ response: { recent: req.app.locals.temperatureData, historic: req.app.locals.temperatureHistory } });
});
// Return the door status (this is updated in realtime)
routes.get('/door', (req, res) => {
res.status(200).json({ response: req.app.locals.doorStatus });
});
// Trigger the door button
routes.get('/trigger', (req, res) => {
req._gpio.write(req._PINS.RELAY, true, function(err, value) {
setTimeout(function() {
req._gpio.write(req._PINS.RELAY, false);
}, 500);
});
res.status(200).json({ response: 'door triggered' });
});
// 404 because very last route hit
routes.use(function(req, res, next){
res.status(404).json({ error: 'not found' });
});
module.exports = routes;