chaosc-cosmdisplay/app.js

92 lines
2.9 KiB
JavaScript

var express = require('express')
, app = express()
, server = require('http').createServer(app)
, path = require('path')
, io = require('socket.io').listen(server)
, cosm = require('./cosm.js')
, osc = require('./osc.js');
var cosmStreams = [53146, 34606, 43573];
var cosmFeeds = {53146 : ["GearboxRPM", "Windspeed", "Pitch"],
34606 : ["CCChannel1,Import1", "CCSensor1,Servers2", "CCSensor2,Office3"],
43573 : ["Humidity1", "Air pressure2", "Thermo3"]};
var cosmClient = new cosm(cosmStreams, 'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g');
var oscClient = new osc('chaosc', 7110);
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.send(500, { error: 'Something blew up!' });
} else {
next(err);
}
}
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
app.configure(function () {
app.set('port', process.env.PORT || 16000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function () {
app.use(express.errorHandler());
app.locals.pretty = true;
});
app.get("/", function(req, res) {
res.render('index', { title: 'COSM display', streams: cosmStreams });
});
io.sockets.on('connection', function (socket) {
cosmClient.getStreams(cosmStreams, function(object) {
socket.emit('gotstream', object);
});
});
server.listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
// this event is send by cosm client when new data arrives (just when values changes)
cosmClient.on('updatevalue', function(object) {
console.log("updatevalue: " + JSON.stringify(object));
oscClient.send('/cosm/' + object.stream + "/" + object.displayname, object.value);
if (cosmFeeds.hasOwnProperty(object.stream) && cosmFeeds[object.stream].indexOf(object.displayname) != -1) {
console.log("sending " + object.displayname);
io.sockets.emit('updatevalue', object);
}
});
// this event is send by cosm client when new data arrives (just when values changes)
cosmClient.on('changedvalue', function(object) {
console.log("changedvalue: " + JSON.stringify(object));
oscClient.send('/cosm/' + object.stream + "/" + object.displayname, object.value);
if (cosmFeeds.hasOwnProperty(object.stream) && cosmFeeds[object.stream].indexOf(object.displayname) != -1) {
console.log("sending " + object.displayname);
io.sockets.emit('changedvalue', object);
}
});