forked from node-dmx/dmx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmx-web.js
More file actions
executable file
·132 lines (107 loc) · 3.13 KB
/
dmx-web.js
File metadata and controls
executable file
·132 lines (107 loc) · 3.13 KB
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
#!/usr/bin/env node
"use strict"
var fs = require('fs')
var http = require('http')
var connect = require('connect')
var express = require('express')
var socketio = require('socket.io')
var program = require('commander')
var DMX = require('./dmx')
var A = DMX.Animation
program
.version("0.0.1")
.option('-c, --config <file>', 'Read config from file [/etc/dmx-web.json]', '/etc/dmx-web.json')
.parse(process.argv)
var config = JSON.parse(fs.readFileSync(program.config, 'utf8'))
function DMXWeb() {
var app = express()
var server = http.createServer(app)
var io = socketio.listen(server)
var dmx = new DMX()
for(var universe in config.universes) {
dmx.addUniverse(
universe,
config.universes[universe].output.driver,
config.universes[universe].output.device,
config.universes[universe].output.options
)
}
var listen_port = config.server.listen_port || 8080
var listen_host = config.server.listen_host || '::'
server.listen(listen_port, listen_host, null, function() {
if(config.server.uid && config.server.gid) {
try {
process.setuid(config.server.uid)
process.setgid(config.server.gid)
} catch (err) {
console.log(err)
process.exit(1)
}
}
})
io.set('log level', 1)
app.configure(function() {
app.use(connect.json())
})
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html')
})
app.get('/config', function(req, res) {
var response = {"devices": DMX.devices, "universes": {}}
Object.keys(config.universes).forEach(function(key) {
response.universes[key] = config.universes[key].devices
})
res.json(response)
})
app.get('/state/:universe', function(req, res) {
if(!(req.params.universe in dmx.universes)) {
res.status(404).json({"error": "universe not found"})
return
}
res.json({"state": dmx.universeToObject(req.params.universe)})
})
app.post('/state/:universe', function(req, res) {
if(!(req.params.universe in dmx.universes)) {
res.status(404).json({"error": "universe not found"})
return
}
dmx.update(req.params.universe, req.body)
res.json({"state": dmx.universeToObject(req.params.universe)})
})
app.post('/animation/:universe', function(req, res) {
try {
var universe = dmx.universes[req.params.universe]
// preserve old states
var old = dmx.universeToObject(req.params.universe)
var animation = new A()
for(var step in req.body) {
animation.add(
req.body[step].to,
req.body[step].duration || 0,
req.body[step].options || {}
)
}
animation.add(old, 0)
animation.run(universe)
res.json({"success": true})
} catch(e) {
console.log(e)
res.json({"error": String(e)})
}
})
io.sockets.on('connection', function(socket) {
socket.emit('init', {'devices': DMX.devices, 'setup': config})
socket.on('request_refresh', function() {
for(var universe in config.universes) {
socket.emit('update', universe, dmx.universeToObject(universe))
}
})
socket.on('update', function(universe, update) {
dmx.update(universe, update)
})
dmx.on('update', function(universe, update) {
socket.emit('update', universe, update)
})
})
}
DMXWeb()