-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (129 loc) · 3.98 KB
/
index.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
const dotenv = require('dotenv').config({path: __dirname + '/.env'})
if (dotenv.error) throw dotenv.error
const sys = require('sys')
const exec = require('child_process').exec
const Temp = require('./temp.js')
const plug = require('./plug.js')
const push = require('./push.js')
const Db = require('./db.js')
const utils = require('./utils.js')
const cors = require('cors')
const basicAuth = require('express-basic-auth')
const express = require('express')
const app = express()
const db = new Db()
const temp = new Temp()
if(process.env.BUGSNAG_API_KEY) {
const bugsnag = require("bugsnag")
bugsnag.register(process.env.BUGSNAG_API_KEY)
}
const port = (process.env.PORT) ? process.env.PORT : 80
app.set('view engine', 'pug')
app.set('views', __dirname + '/views')
app.use(express.static(__dirname + '/public'))
app.use(cors())
app.use(express.json());
const auth = basicAuth({
users: { 'admin': process.env.ADMIN_ACCESS_PASSWORD },
challenge: false,
unauthorizedResponse: (req) => {
return req.auth ? 'Incorrect login credentials' : 'No credentials provided'
}
})
app.get('/', (req, res) => {
temp.cachedRead(res, (err, c) => {
let f = temp.c2f(c)
res.render('index', {
c: c,
f: f,
formatted: {
c: `${utils.fmt(c)} C`,
f: `${utils.fmt(f)} F`
}
})
})
})
app.get('/api', (req, res) => {
db.historic((history) => {
temp.cachedRead(res, (err, c) => {
res.json({
c: utils.round(c),
f: utils.round(temp.c2f(c)),
history: history
})
})
})
})
app.post('/api/save', (req, res) => {
temp.cachedRead(res, (err, c) => {
if(err) { return res.json({error: "Error reading temperature"}) }
db.save(temp.c2f(c), (dbRes) => {
res.json(dbRes)
})
})
})
app.get('/api/plug/:hostname', (req, res) => {
plug.status(req.params.hostname, (device) => {
res.json({ online: device.relay_state, device: device })
})
})
app.post('/api/plug/:hostname/on', auth, (req, res) => {
plug.on(req.params.hostname, (device) => {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
let message = `${ip} turned ${device.alias} on!`
console.log(message)
push.event('plug-update', message, device)
res.json({ online: device.relay_state, device: device })
})
})
app.post('/api/plug/:hostname/off', auth, (req, res) => {
plug.off(req.params.hostname, (device) => {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
let message = `${ip} turned ${device.alias} off!`
console.log(message)
push.event('plug-update', message)
res.json({ online: device.relay_state, device: device })
})
})
app.get('/api/screen', (req, res) => {
exec("vcgencmd display_power", (error, stdout, stderr) => {
let screen = stdout.trim()
res.json({
screen: screen,
power: screen.slice(-1)
})
})
})
app.post('/api/screen/toggle', (req, res) => {
exec("vcgencmd display_power", (error, stdout, stderr) => {
let t = stdout.trim().slice(-1)
let toggle = (t == 1) ? 0 : 1
exec(`vcgencmd display_power ${toggle}`, (e, stdout, stderr) => {
let out = stdout.trim()
res.json({
screen: out,
power: toggle,
t: t
})
})
})
})
app.post('/api/pictureframe', (req, res) => {
let response = {
success: false,
request: req.body
}
// Are you human?
if(!req.body.captcha || req.body.captcha != 4) {
return res.status(403).json(response)
}
// What? No payload?
if(!req.body.message && !req.body.image) {
return res.status(418).json(response)
}
// All good!
push.trigger('pictureframe-update', req.body)
response.success = true
return res.status(200).json(response)
})
app.listen(port, () => console.log(`Temperature app listening on port ${port}!`))