2013-11-17 00:41:27 +00:00
|
|
|
"use strict"
|
|
|
|
|
2012-09-08 12:43:04 +00:00
|
|
|
var http = require('http'),
|
|
|
|
path = require('path'),
|
|
|
|
io = require('socket.io'),
|
2013-07-30 18:24:45 +00:00
|
|
|
fs = require('fs'),
|
|
|
|
A = require('./anim.js').Anim
|
2013-11-17 00:41:27 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
var DMX = require('./dmx')
|
|
|
|
var config = require('./config.js')
|
|
|
|
, web = require('./web.js')
|
|
|
|
, setup = require('./setup.js').setup
|
|
|
|
, devices = require('./devices.js').devices
|
2012-09-08 12:43:04 +00:00
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
|
|
|
|
function DMXWeb(dmx) {
|
2012-09-08 12:43:04 +00:00
|
|
|
function handler (request, response) {
|
|
|
|
var reqBody = '';
|
|
|
|
|
|
|
|
request.on("data", function (chunk) {
|
|
|
|
reqBody += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on("end", function () {
|
2013-07-31 10:28:45 +00:00
|
|
|
var urlData = require('url').parse(request.url),
|
|
|
|
urlPath = urlData.pathname.split('/');
|
2013-07-30 18:24:45 +00:00
|
|
|
|
2013-07-31 10:28:45 +00:00
|
|
|
if(urlPath.length == 3 && urlPath[1] == 'animation') {
|
|
|
|
try {
|
|
|
|
// save old states
|
|
|
|
var universe = dmx.drivers[urlPath[2]], old = {}, black = {};
|
|
|
|
for(var i = 0; i < 256; i++) {
|
|
|
|
old[i] = universe.get(i);
|
|
|
|
black[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonAnim = JSON.parse(reqBody), animation = new A();
|
|
|
|
for(var step in jsonAnim) {
|
|
|
|
animation.add(jsonAnim[step].to, jsonAnim[step].duration || 0, jsonAnim[step].options || {});
|
|
|
|
}
|
2013-07-30 18:24:45 +00:00
|
|
|
|
2013-07-31 10:28:45 +00:00
|
|
|
animation.add(old, 0);
|
|
|
|
animation.run(universe);
|
|
|
|
response.write('{ "success": true }');
|
|
|
|
} catch(e) {
|
|
|
|
response.write('{ "error": "broken json" }');
|
2013-07-30 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:28:45 +00:00
|
|
|
response.end();
|
|
|
|
|
2013-07-30 18:24:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filePath = '.' + urlData.pathname;
|
2012-09-08 12:43:04 +00:00
|
|
|
if (filePath == './')
|
|
|
|
filePath = './index.html';
|
|
|
|
|
|
|
|
var extname = path.extname(filePath);
|
|
|
|
var contentType = 'text/html';
|
|
|
|
switch (extname) {
|
|
|
|
case '.js': contentType = 'text/javascript'; break;
|
|
|
|
case '.css': contentType = 'text/css'; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.exists(filePath, function(exists) {
|
|
|
|
if(!exists) {
|
|
|
|
console.log('404: ' + request.url)
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(filePath, function(error, content) {
|
|
|
|
if (error) {
|
|
|
|
console.log('500: ' + request.url)
|
|
|
|
response.writeHead(500);
|
|
|
|
response.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.writeHead(200, { 'Content-Type': contentType });
|
|
|
|
response.end(content, 'utf-8');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var app = http.createServer(handler)
|
2013-11-17 00:41:27 +00:00
|
|
|
app.listen(config.port, '::', null, function() {
|
2012-12-05 09:34:03 +00:00
|
|
|
try {
|
2013-11-17 00:41:27 +00:00
|
|
|
process.setgid(config.gid);
|
|
|
|
process.setuid(config.uid);
|
2012-12-05 09:34:03 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
2012-09-08 12:43:04 +00:00
|
|
|
|
|
|
|
io.listen(app).sockets.on('connection', function (socket) {
|
2013-11-17 00:41:27 +00:00
|
|
|
socket.emit('init', {'devices': devices, 'setup': setup});
|
2012-09-08 12:43:04 +00:00
|
|
|
socket.on('request_refresh', function() {
|
2013-11-17 00:41:27 +00:00
|
|
|
for(var universe in setup.universes) {
|
|
|
|
var u = {}
|
2012-09-08 12:43:04 +00:00
|
|
|
for(var i = 0; i < 256; i++) {
|
2013-11-17 00:41:27 +00:00
|
|
|
u[i] = dmx.universes[universe].get(i);
|
2012-09-08 12:43:04 +00:00
|
|
|
}
|
|
|
|
console.log('sending update...')
|
|
|
|
console.log(u)
|
|
|
|
socket.emit('update', universe, u);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dmx.on('update', function(universe, update){
|
|
|
|
socket.emit('update', universe, update);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('update', function(universe, update) {
|
|
|
|
dmx.update(universe, update);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
var dmx = new DMX()
|
2012-09-08 12:43:04 +00:00
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
for(var universe in setup.universes) {
|
|
|
|
dmx.addUniverse(
|
|
|
|
universe,
|
|
|
|
setup.universes[universe].output.driver,
|
|
|
|
setup.universes[universe].output.device
|
|
|
|
)
|
|
|
|
}
|
2012-09-08 12:43:04 +00:00
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
var web = new DMXWeb(dmx)
|
2012-09-08 12:43:04 +00:00
|
|
|
|
|
|
|
|