This commit is contained in:
Lucas Pleß 2013-02-13 00:12:44 +01:00
parent ee8aba9b87
commit 201edd7a9b
4 changed files with 72 additions and 38 deletions

17
app.js
View File

@ -6,7 +6,9 @@ var express = require('express')
, cosm = require('./cosm.js') , cosm = require('./cosm.js')
, osc = require('./osc.js'); , osc = require('./osc.js');
var cosmClient = new cosm([91755, 70632], 'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'); var cosmStreams = [ 70632 ];
var cosmClient = new cosm(cosmStreams, 'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g');
var oscClient = new osc('localhost', 8000); var oscClient = new osc('localhost', 8000);
@ -27,13 +29,17 @@ app.configure('development', function () {
}); });
app.get("/", function(req, res) { app.get("/", function(req, res) {
res.render('index', { title: 'COSM display' }); res.render('index', { title: 'COSM display', streams: cosmStreams });
}); });
io.sockets.on('connection', function (socket) { io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' }); socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) { socket.on('my other event', function (data) {
console.log(data); console.log("bla" + data);
});
cosmClient.getStreams(cosmStreams, function(object) {
socket.emit('gotstream', object);
}); });
}); });
@ -42,9 +48,10 @@ server.listen(app.get('port'), function () {
}); });
// this event is send by cosm client when new data arrives (just when values changed)
// this event is send by cosm client when new data arrives (just when values changes)
cosmClient.on('changedvalue', function(object) { cosmClient.on('changedvalue', function(object) {
console.log("changedvalue: " + JSON.stringify(object)); console.log("changedvalue: " + JSON.stringify(object));
oscClient.send('/cosm/' + object.stream + "/" + object.displayname, object.value); oscClient.send('/cosm/' + object.stream + "/" + object.displayname, object.value);
io.sockets.emit('changedvalue', object);
}); });

60
cosm.js
View File

@ -2,13 +2,14 @@ var restify = require('restify')
, util = require('util') , util = require('util')
, EventEmitter = require('events').EventEmitter; , EventEmitter = require('events').EventEmitter;
var COSM = function(streams, key) { // constructor function
function Cosm(streams, key) {
var self = this; var self = this;
var streams = streams; self._streams = streams;
var interval = 1000; self._interval = 5000;
var jsonClient = restify.createJsonClient({ self._jsonClient = restify.createJsonClient({
url: 'http://api.cosm.com', url: 'http://api.cosm.com',
headers: { 'X-ApiKey': key }, headers: { 'X-ApiKey': key },
version:'*' version:'*'
@ -20,7 +21,7 @@ var COSM = function(streams, key) {
for(var i=0; i<streams.length; i++) { for(var i=0; i<streams.length; i++) {
jsonClient.get('/v2/feeds/' + streams[i] + ".json", function(err, req, res, obj) { self._jsonClient.get('/v2/feeds/' + self._streams[i] + ".json", function(err, req, res, obj) {
if(err || obj.datastreams == null) { if(err || obj.datastreams == null) {
console.error("error getting stream: " + err) console.error("error getting stream: " + err)
return; return;
@ -50,7 +51,7 @@ var COSM = function(streams, key) {
var address = streams[i] + ":" + dataStream.id; var address = streams[i] + ":" + dataStream.id;
if(recentvalues[address] != currentValue) { if(true || recentvalues[address] != currentValue) {
self.emit('changedvalue', object); self.emit('changedvalue', object);
} }
@ -62,7 +63,42 @@ var COSM = function(streams, key) {
}); });
} }
}, interval); }, self._interval);
}
util.inherits(Cosm, EventEmitter);
Cosm.prototype.getStreams = function(streams, callback) {
var self = this;
for(var i=0; i<streams.length; i++) {
self._jsonClient.get('/v2/feeds/' + streams[i] + ".json", function(err, req, res, obj) {
if(err || obj.datastreams == null) {
console.error("error getting stream: " + err)
return;
}
var object = {
id: streams[i],
description: obj.description,
lat: typeof obj.location === 'undefined' ? 'undefined' : obj.location.lat,
lon: typeof obj.location === 'undefined' ? 'undefined' : obj.location.lon,
location: typeof obj.location === 'undefined' ? 'undefined' :obj.location.name,
title: obj.title
};
if(callback != 'undefined') callback(object);
});
}
};
module.exports = Cosm;
function isNumber(value) { function isNumber(value) {
if ((undefined === value) || (null === value)) { if ((undefined === value) || (null === value)) {
@ -73,13 +109,3 @@ var COSM = function(streams, key) {
} }
return !isNaN(value - 0); return !isNaN(value - 0);
} }
}
util.inherits(COSM, EventEmitter);
module.exports = COSM;

View File

@ -8,7 +8,6 @@
"dependencies": { "dependencies": {
"express": "3.1.0", "express": "3.1.0",
"jade": "*", "jade": "*",
"cosm": "*",
"socket.io": "*", "socket.io": "*",
"restify": "*", "restify": "*",
"node-osc": "*" "node-osc": "*"

View File

@ -7,26 +7,26 @@ block content
div.container div.container
div.row div.row
for nr in [ 1,2,3] for nr in streams
div.cell div.cell
div.kasten div.kasten
h3 Freakduino + Netrad SBM-20 h3 Freakduino + Netrad SBM-20
table table
tr tr
td Location: td Location:
td Montreal td --
tr tr
td Elevation: td Elevation:
td 8m td --
tr tr
td Latitude: td Latitude:
td 51.6693413745977 td --
tr tr
td Longitude: td Longitude:
td -1.28627210855484 td --
tr tr
td Update: td Update:
td 23 Jun 2012 08:01:47 td --
div.mapbox(id="map#{nr}") div.mapbox(id="map#{nr}")
@ -48,14 +48,16 @@ block content
} }
$(function() { $(function() {
loadMap(document.getElementById("map1"), -34.397, 150.644);
loadMap(document.getElementById("map2"), -34.397, 150.644);
loadMap(document.getElementById("map3"), -34.397, 150.644);
}); });
var socket = io.connect('http://localhost'); var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data); socket.on('gotstream', function(data) {
socket.emit('my other event', { my: 'data' }); console.log("got stream: " + data);
});
socket.on('changedvalue', function(data) {
console.log("changedvalue: " + data);
}); });