2013-11-17 16:45:53 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
"use strict"
|
|
|
|
|
|
|
|
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 = require(program.config)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-10-02 16:27:20 +00:00
|
|
|
res.sendfile(__dirname + '/index.html')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/state/:universe', function(req, res) {
|
|
|
|
if(!(req.params.universe in dmx.universes)) {
|
|
|
|
res.status(404).json({"error": "universe not found"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var universe = dmx.universes[req.params.universe]
|
|
|
|
var u = {}
|
|
|
|
for(var i = 0; i < 256; i++) {
|
|
|
|
u[i] = universe.get(i)
|
|
|
|
}
|
|
|
|
res.json({"state": u})
|
2013-11-17 16:45:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/animation/:universe', function(req, res) {
|
|
|
|
try {
|
|
|
|
var universe = dmx.universes[req.params.universe]
|
|
|
|
|
|
|
|
// preserve old states
|
|
|
|
var old = {}
|
|
|
|
for(var i = 0; i < 256; i++) {
|
|
|
|
old[i] = universe.get(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var u = {}
|
|
|
|
for(var i = 0; i < 256; i++) {
|
|
|
|
u[i] = dmx.universes[universe].get(i)
|
|
|
|
}
|
|
|
|
socket.emit('update', universe, u)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('update', function(universe, update) {
|
|
|
|
dmx.update(universe, update)
|
|
|
|
})
|
|
|
|
|
|
|
|
dmx.on('update', function(universe, update) {
|
2016-10-02 16:27:20 +00:00
|
|
|
socket.emit('update', universe, update)
|
2013-11-17 16:45:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
DMXWeb()
|