/
index.js
75 lines (74 loc) · 3 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path')
const fs = require('fs')
var https = require('https');
module.exports = function(dmx,config = {port: 8080}){
const app = express();
app.use(express.static(path.join(__dirname, './dist/')));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/api/devices', (req, res) => {
if(!dmx.devices) return res.json([])
//let devices = //JSON.parse(JSON.stringify(dmx.devices))
let devices = dmx.devices.map(x => {
return {
name: x.name,
programs: x.getPrograms(),
isRgb: x.isRgb,
reactSound: x.reactSound,
channels: x.channels,
isGroup: x.isGroup,
state: x.getChannelState()
}
}).sort((a,b) => a.isGroup ? -1 : 1)
res.json(devices)
});
app.post('/api/blackout',(req,res) => {
dmx.devices.forEach(device => device.blackout())
res.json({success: true})
})
app.post('/api/device/set',(req,res) => {
let device = dmx.devices.find(x => x.name === req.body.name)
if(!req.body.name || !req.body.commands || !device) return res.json({success: false})
let results = req.body.commands.map(command => {
let result = false
if(!command.args) command.args = []
if(command.type && device[command.type] instanceof Function ) result = device[command.type](...command.args)
if(!(result instanceof Object) || result instanceof dmx.Device || result instanceof dmx.Group) result = false
return result
})
res.json({success: true,results: results})
})
app.get('/api/sequences',(req,res) => {
if(!dmx.seqences) return res.json([])
res.json(Object.keys(dmx.seqences).map(x => Object.assign({name: x, duration: dmx.seqences[x].duration || 100})))
})
app.post('/api/stopSequence/:name',(req,res) => {
if(!req.params.name) return res.json({success: false})
res.json({success: dmx.stopSeq(req.params.name)})
})
app.post('/api/runSequence/:name',(req,res) => {
if(!req.params.name) return res.json({success: false})
if(req.body.args) res.json({success: dmx.runSeq(req.params.name)})
else res.json({success: dmx.runSeq(req.params.name,...req.body.args)})
})
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, './dist/index.html'))
});
app.listen(config.port, () => console.log('Web server started at localhost: '+config.port+'!'));
if(config.https && config.credentials && config.httpsPort){
config.credentials = {
key: fs.readFileSync(config.credentials.key),
cert: fs.readFileSync(config.credentials.cert)
}
var httpsServer = https.createServer(config.credentials, app);
httpsServer.listen(config.httpsPort,() => console.log('HTTPS server started at localhost: '+config.httpsPort+'!'))
}
return app
}