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 = [84896, 84388, 70632]; var cosmFeeds = {84896 : ["003", "004", "006"], 84388 : ["Furdo", "Halo", "Gyerekszoba"], 70632 : ["househumid4", "humidity", "motion4"]}; var cosmClient = new cosm(cosmStreams, 'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'); var oscClient = new osc('localhost', 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 || 3000); 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) 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) io.sockets.emit('changedvalue', object); });