diff --git a/anim.js b/anim.js index 103e951..614fff3 100644 --- a/anim.js +++ b/anim.js @@ -39,7 +39,7 @@ Anim.prototype.run = function(universe, onFinish) { } } var ani_step = function() { - new_vals = {} + var new_vals = {} for(var k in config) { new_vals[k] = Math.round(config[k].start + ease['linear'](t, 0, 1, d) * (config[k].end - config[k].start)) } diff --git a/config.js b/config.js deleted file mode 100644 index f65981c..0000000 --- a/config.js +++ /dev/null @@ -1,8 +0,0 @@ -// webserver configuration - -// listen port -exports.port = 80; - -// uid and gid to drop root priv. -exports.uid = 'light'; -exports.gid = 'users'; diff --git a/devices.js b/devices.js index e4d308e..70fc349 100644 --- a/devices.js +++ b/devices.js @@ -1,4 +1,10 @@ -exports.devices = { +module.exports = { + 'generic': { + channels: ['dimmer'] + }, + 'showtec-multidim2': { + channels: ['1', '2', '3', '4'] + }, 'eurolite-led-bar': { channels: ['ctrl', 'dimmer', 'strobe', 'red0', 'green0', 'blue0', 'red1', 'green1', 'blue1', 'red2', 'green2', 'blue2'], ranges: { @@ -31,9 +37,6 @@ exports.devices = { } } }, - 'showtec-multidim2': { - channels: ['1', '2', '3', '4'] - }, 'stairville-led-par-56': { channels: ['ctrl', 'red', 'green', 'blue', 'speed'], ranges: { diff --git a/setup.js b/dmx-web-example.conf similarity index 84% rename from setup.js rename to dmx-web-example.conf index 876b560..35ff63f 100644 --- a/setup.js +++ b/dmx-web-example.conf @@ -1,7 +1,15 @@ -exports.setup = { - presets: [ +module.exports = { + "server": { + "listen_port": 8080, + "listen_host": "::", + + // drop privileges to: + // "uid": "www-data", + // "gid": "www-data" + }, + "presets": [ { - label: 'White', + label: "White", values: { 'office': { 0:16, 1:255, 2:0, 3:255, 4: 255, 5:255, 15:16, 16:255, 17:0, 18:255, 19: 255, 20:255 } } @@ -34,7 +42,8 @@ exports.setup = { universes: { 'office': { 'output': { - 'driver': 'enttec-usb-dmx-pro', + // 'driver': 'enttec-usb-dmx-pro', + 'driver': 'null', 'device': 0 }, 'devices': [ diff --git a/dmx-web.js b/dmx-web.js new file mode 100755 index 0000000..d7c29ea --- /dev/null +++ b/dmx-web.js @@ -0,0 +1,109 @@ +#!/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 ', '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) { + res.sendfile(__dirname + '/index.html') + }) + + 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) { + socket.emit('update', universe, update) + }) + }) +} + +DMXWeb() diff --git a/dmx.js b/dmx.js index a722e75..a586e51 100644 --- a/dmx.js +++ b/dmx.js @@ -6,12 +6,16 @@ var EventEmitter = require('events').EventEmitter function DMX() { this.universes = {} this.drivers = {} + this.registerDriver('null', require('./drivers/null')) this.registerDriver('enttec-usb-dmx-pro', require('./drivers/enttec-usb-dmx-pro')) } util.inherits(DMX, EventEmitter) +DMX.devices = require('./devices') +DMX.Animation = require('./anim') + DMX.prototype.registerDriver = function(name, module) { this.drivers[name] = module } diff --git a/index.html b/index.html index 1eec641..dde46d6 100644 --- a/index.html +++ b/index.html @@ -5,8 +5,7 @@ DMX Lichtschalter - - + - +