-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
110 lines (87 loc) · 2.88 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
const express = require('express')
const favicon = require('serve-favicon')
const storage = require('node-persist')
const sdk = require('applights-sdk')
const version = require('./package').version
const EFFECTS = sdk.effects
const DISCONNECT_TIMEOUT = process.env.TIMEOUT || 10 * 1000
let app = express(), api = express()
let strings = new sdk.StringsController({ timeout: DISCONNECT_TIMEOUT })
storage.init().then(() => {
console.log('[server]',`Storage initialized`)
console.log(storage.values())
})
app.use(favicon(`${__dirname}/public/favicon.ico`))
// Rest API
api.get('/info', (req, res) => {
res.send({
version: version,
sdk_version: sdk.version,
ble_state: strings.service.state,
connected: strings.service.connected,
serviceUUID: strings.service.uuid
})
})
api.get('/effects', (req, res) => {
res.send(Object.keys(EFFECTS))
})
api.get('/effects/:id', (req, res, next) => {
let theme = EFFECTS[req.params.id]
if (typeof theme !== 'undefined') strings.setTheme(theme).then((all) => res.send(all)).catch(next)
else res.send({ error: `Invalid effect '${req.params.id}'`})
})
api.get('/on', async (req, res, next) => {
try {
const response = await strings.turnOn()
storage.setItem('status', 'on')
res.send(response)
} catch (err) {
next(err)
}
})
api.get('/off', async (req, res, next) => {
try {
const response = await strings.turnOff()
storage.setItem('status', 'off')
res.send(response)
} catch (err) {
next(err)
}
})
api.get('/color', async (req, res, next) => {
const currentColor = await storage.getItem('color')
res.send(currentColor)
})
api.get('/color/:hex', async (req, res, next) => {
try {
const color = await strings.getThemeFromHex(req.params.hex)
const response = await strings.setTheme(color.buffer)
console.log('FROM:', req.params.hex, ' setting theme:', response)
if (color && response) {
console.log('saving', color.value.substring(1))
storage.setItem('color', color.value.substring(1))
}
res.send({ ...color, ...response })
} catch (err) {
next(err)
}
})
api.get('/status', async (req, res, next) => {
const status = storage.getItem('status')
res.send(status === 'on' ? '1' : '0')
})
// /debug route allows passing custom buffer values
if (process.env.DEBUG) {
api.get('/debug/:a/:b/:c/:d', (req, res, next) => {
let theme = Buffer.from([req.params.a, req.params.b, req.params.c, req.params.d])
if (typeof theme !== 'undefined') strings.setTheme(theme).then(res.send).catch(next)
else res.send({ error: `Invalid theme '${JSON.stringify(theme)}'`})
})
}
api.use((err, req, res, next) => {
if (err) res.status(500)
else res.status(400)
res.send({ error: err.message || 'Bad Request' })
})
app.use('/api/v1', api)
const server = app.listen(3000, () => { console.info('\x1b[33m%s\x1b[0m', `[express] Listening on ${server.address().port}`) })