hackcentercontrol/nodeartnet/app.js

91 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2013-01-11 14:29:38 +00:00
var config = "ctdo"
2012-12-28 22:17:47 +00:00
var express = require('express');
var app = express();
var server = require('http').createServer(app), io = require('socket.io').listen(server);
var osc = require('node-osc');
2013-01-11 14:29:38 +00:00
var oscMap = require("./configs/" + config + "/oscMap.json");
var states = require("./configs/" + config + "/states.json");
var fs = require('fs');
2012-12-28 22:17:47 +00:00
2013-01-11 14:29:38 +00:00
var host = process.argv.length > 2 ? process.argv[2] : '195.160.169.33'
2013-01-10 01:18:25 +00:00
var port = process.argv.length > 3 ? process.argv[3] : '9000';
2012-12-28 22:17:47 +00:00
var oscSender = new osc.Client(host, port);
2013-01-10 01:18:25 +00:00
var oscReceiver = new osc.Server(3333, '0.0.0.0');
var artnetclient = require('./node_modules/artnet/lib/artnet_client');
2013-01-11 14:29:38 +00:00
var dmxClient = artnetclient.createClient("195.160.169.15", 6454);
2013-01-10 01:18:25 +00:00
2012-12-28 22:17:47 +00:00
2013-01-10 01:18:25 +00:00
var dmxData = new Array(512);
for(var i=0; i<512;i++) {
dmxData[i] = 0;
}
2012-12-28 22:17:47 +00:00
2013-01-10 01:18:25 +00:00
server.listen(8081);
2012-12-28 22:17:47 +00:00
app.use(express.static('bootstrap'));
app.get('/', function (req, res) {
2013-01-11 14:29:38 +00:00
res.sendfile(__dirname + '/configs/' + config + '/index.html');
2012-12-28 22:17:47 +00:00
});
io.sockets.on('connection', function (socket) {
2012-12-28 23:10:43 +00:00
var address = socket.handshake.address;
console.log("New connection from " + address.address + ":" + address.port);
2013-01-10 01:18:25 +00:00
socket.on('oscmessage', function (data) {
2012-12-28 22:17:47 +00:00
var chan = data.chan;
var value = data.val;
// hier noch nicht einkommentieren, das sendet das sofort ins hackcenter
//oscSender.send(chan, value);
2013-01-10 01:18:25 +00:00
handleMessage(new Array(chan, value))
//console.log(address, chan, value);
2012-12-28 22:17:47 +00:00
});
});
2013-01-10 01:18:25 +00:00
oscReceiver.on("message", function (msg, rinfo) {
handleMessage(msg)
});
function handleMessage(msg) {
console.log("Message:");
console.log(msg);
var chan = msg[0],
value = msg[1];
if(chan == null) return;
// sync states array with value
if(value == null) {
if(states[chan] == null) {
states[chan] = 1
} else if(states[chan] == 1) {
states[chan] = 0;
} else if(states[chan] == 0) {
states[chan] = 1;
}
value = states[chan];
} else {
states[chan] = value;
}
// get mapped dmx channel
2013-01-10 01:25:30 +00:00
if(oscMap[chan] != null) {
2013-01-11 14:29:38 +00:00
dmxData[oscMap[chan]-1] = Math.round(value*255);
2013-01-10 01:25:30 +00:00
console.log(Math.round(value*255));
dmxClient.send(dmxData);
}
2013-01-10 01:18:25 +00:00
// update osc devices to show the current state
// to be done in extra function (with whole states array?)
updateDevices();
2013-01-11 14:29:38 +00:00
fs.writeFile("./configs/" + config + "/states.json", JSON.stringify(states, null, 4));
}
function updateDevices() {
for(var chan in states) {
oscSender.send(chan, states[chan]);
console.log(chan);
}
2013-01-11 14:29:38 +00:00
}