-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2.17 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
require('./install');
const express = require('express');
const configHelper = require('./config');
const timezone = require('./timezone');
const ledControl = require('./ledControl');
const wifi = require('./wifi');
const clock = require('./clock');
const config = configHelper.getConfig();
const app = express();
app.use(express.json());
app.use(express.static('assets'));
app.use('/pickr', express.static('node_modules/@simonwebp/pickr/dist'))
app.post('/timezone', (req, res) => {
const {timezoneName, timezoneString} = req.query;
timezone.setTimezone(timezoneName, timezoneString)
.then(() => {
config.timezone = [timezoneName, timezoneString];
return configHelper.saveConfig();
})
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
app.get('/timezone', (req, res) => {
res.send(timezone.getTimezone().join(', '));
})
app.get('/timezones', (req, res) => {
timezone.getTimezones()
.then(timezones => res.send(timezones))
.catch(err => res.status(500).send(err));
});
app.get('/led/color', (req, res) => {
res.send(ledControl.getColor());
});
app.post('/led/color', (req, res) => {
const {r, g, b} = req.query;
ledControl.setColor(parseInt(r, 10), parseInt(g, 10), parseInt(b, 10))
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
app.post('/led/off', (req, res) => {
ledControl.off();
res.sendStatus(200);
});
app.get('/wifi/scan', (req, res) => {
wifi.scanForWifiNetworks()
.then(networks => res.send(networks))
.catch(err => res.status(500).send(err));
});
app.get('/wifi/list', (req, res) => {
wifi.getConfiguredWifiNetworks()
.then(ssids => res.send(ssids))
.catch(err => res.status(500).send(err));
});
app.delete('/wifi/:ssid', (req, res) => {
wifi.removeConfiguredWifiNetwork(req.params.ssid)
.then(() => res.sendStatus(200))
.catch(err => err.status(500).send(err));
});
app.post('/wifi/:ssid', (req,res) => {
wifi.addWifiConfiguration(req.params.ssid, req.body.encryption, req.body.password)
.then(() => res.sendStatus(200))
.catch(err => err.status(500).send(err));
});
app.listen(81, () => console.log('Webserver started'));
clock();