added cosmfetcher function into this module, so we can merge
This commit is contained in:
Lucas Pleß 2013-01-31 00:09:00 +01:00
parent 6b48ebee9e
commit ee8aba9b87
12 changed files with 168 additions and 43 deletions

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{Node.js v0.8.17 Core Modules}" />
</component>
</project>

36
app.js
View File

@ -1,15 +1,19 @@
var express = require('express') var express = require('express')
, routes = require('./routes') , app = express()
, http = require('http') , server = require('http').createServer(app)
, path = require('path'); , path = require('path')
, io = require('socket.io').listen(server)
, cosm = require('./cosm.js')
, osc = require('./osc.js');
var cosmClient = new cosm([91755, 70632], 'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g');
var oscClient = new osc('localhost', 8000);
var app = express();
app.configure(function () { app.configure(function () {
app.set('port', process.env.PORT || 3000); app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views'); app.set('views', __dirname + '/views');
app.set('view engine', 'jade'); app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev')); app.use(express.logger('dev'));
app.use(express.bodyParser()); app.use(express.bodyParser());
app.use(express.methodOverride()); app.use(express.methodOverride());
@ -22,9 +26,25 @@ app.configure('development', function () {
app.locals.pretty = true; app.locals.pretty = true;
}); });
app.get('/', routes.index); app.get("/", function(req, res) {
app.get("/blafasel", routes.blafasel); res.render('index', { title: 'COSM display' });
});
http.createServer(app).listen(app.get('port'), function () { io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
server.listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port')); console.log("Express server listening on port " + app.get('port'));
}); });
// this event is send by cosm client when new data arrives (just when values changed)
cosmClient.on('changedvalue', function(object) {
console.log("changedvalue: " + JSON.stringify(object));
oscClient.send('/cosm/' + object.stream + "/" + object.displayname, object.value);
});

85
cosm.js Normal file
View File

@ -0,0 +1,85 @@
var restify = require('restify')
, util = require('util')
, EventEmitter = require('events').EventEmitter;
var COSM = function(streams, key) {
var self = this;
var streams = streams;
var interval = 1000;
var jsonClient = restify.createJsonClient({
url: 'http://api.cosm.com',
headers: { 'X-ApiKey': key },
version:'*'
});
var recentvalues = {};
setInterval(function() {
for(var i=0; i<streams.length; i++) {
jsonClient.get('/v2/feeds/' + streams[i] + ".json", function(err, req, res, obj) {
if(err || obj.datastreams == null) {
console.error("error getting stream: " + err)
return;
}
for(var i=0; i<obj.datastreams.length; i++) {
var dataStream = obj.datastreams[i];
if(dataStream.id == null) continue;
var streamName = dataStream.id;
if(streamName.length < 2 && dataStream.tags != null) {
streamName = dataStream.tags[0] + streamName;
}
var currentValue = dataStream.current_value;
if(isNumber(currentValue)) currentValue = parseFloat(currentValue);
var object = {
'stream': obj.id,
'feed': dataStream.id,
'tags': dataStream.tags,
'displayname': streamName,
'value': currentValue
};
var address = streams[i] + ":" + dataStream.id;
if(recentvalues[address] != currentValue) {
self.emit('changedvalue', object);
}
self.emit('updatevalue', object);
recentvalues[address] = currentValue;
}
});
}
}, interval);
function isNumber(value) {
if ((undefined === value) || (null === value)) {
return false;
}
if (typeof value == 'number') {
return true;
}
return !isNaN(value - 0);
}
}
util.inherits(COSM, EventEmitter);
module.exports = COSM;

View File

@ -4,6 +4,7 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Node.js v0.8.17 Core Modules" level="application" />
</component> </component>
</module> </module>

View File

@ -11,3 +11,8 @@ in jeder Spalte die Infos von einem Stream
Dann bis zu drei? streams als Graph oder Zahl zeigen ->konfigurierbar? Dann bis zu drei? streams als Graph oder Zahl zeigen ->konfigurierbar?
- Karte ganz unten - Karte ganz unten
Anstelle der Graphen lieber die Anzeige als Zahlenwerte
Zahlen mit aktuellem Wert blinken dann auf,
Überschrift dann mit ID und Tags

20
osc.js Normal file
View File

@ -0,0 +1,20 @@
var nodeosc = require('node-osc');
var osc = function(host, port) {
var self = this;
var hubAddress = host || '192.168.23.43';
var hubPort = port || 7110;
console.log("using " + hubAddress + ":" + hubPort + " as hub");
self._client = new nodeosc.Client(hubAddress, hubPort);
}
osc.prototype.send = function(path, value) {
var self = this;
self._client.send(path, value)
}
module.exports = osc;

View File

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

View File

@ -3,7 +3,7 @@ html, body {
margin: 0; margin: 0;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
background-color: black; background-color: black;
color: white; color: #ffcbb0;
height: 100%; height: 100%;
} }
@ -34,17 +34,17 @@ a {
.kasten { .kasten {
padding: 10px; padding: 10px;
margin: 5px; margin: 24px;
height: 90%; height: 90%;
background-color: #333; background-color: #444;
border: 1px solid #e5e5e5; border: 1px solid #7b7b7b;
-webkit-border-radius: 5px; -webkit-border-radius: 5px;
-moz-border-radius: 5px; -moz-border-radius: 5px;
border-radius: 5px; border-radius: 5px;
} }
div.mapbox { div.mapbox {
width: 200px; width: 270px;
height: 150px; height: 150px;
margin-top: 20px; margin-top: 20px;
} }

View File

@ -1,13 +0,0 @@
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
exports.blafasel = function(req, res){
res.render('index', { title: 'Blafasel' });
};

View File

@ -1,12 +0,0 @@
/*
* GET users listing.
*/
exports.list = function(req, res){
res.send("respond with a resource");
};
//exports.index2 = function(req, res){
// res.render('index', { title: 'Express' });
//};

View File

@ -1,10 +1,13 @@
extends layout extends layout
block head
script(src="/socket.io/socket.io.js")
block content block content
div.container div.container
div.row div.row
for nr in [ 1,2,3,4,5 ] for nr in [ 1,2,3]
div.cell div.cell
div.kasten div.kasten
h3 Freakduino + Netrad SBM-20 h3 Freakduino + Netrad SBM-20
@ -27,7 +30,7 @@ block content
div.mapbox(id="map#{nr}") div.mapbox(id="map#{nr}")
img(src="https://api.cosm.com/v2/feeds/64590/datastreams/microsievertsperhour.png?width=200&height=100&colour=%23f15a24&duration=5minutes&show_axis_labels=true&detailed_grid=true&scale=auto&timezone=Berlin") //img(src="https://api.cosm.com/v2/feeds/64590/datastreams/microsievertsperhour.png?width=200&height=100&colour=%23f15a24&duration=5minutes&show_axis_labels=true&detailed_grid=true&scale=auto&timezone=Berlin")
@ -48,7 +51,11 @@ block content
loadMap(document.getElementById("map1"), -34.397, 150.644); loadMap(document.getElementById("map1"), -34.397, 150.644);
loadMap(document.getElementById("map2"), -34.397, 150.644); loadMap(document.getElementById("map2"), -34.397, 150.644);
loadMap(document.getElementById("map3"), -34.397, 150.644); loadMap(document.getElementById("map3"), -34.397, 150.644);
loadMap(document.getElementById("map4"), -34.397, 150.644); });
loadMap(document.getElementById("map5"), -34.397, 150.644);
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
}); });

View File

@ -5,6 +5,8 @@ html
meta(name='viewport', content='width=device-width, initial-scale=1.0') meta(name='viewport', content='width=device-width, initial-scale=1.0')
link(rel='stylesheet', href='/stylesheets/style.css') link(rel='stylesheet', href='/stylesheets/style.css')
script(type="text/javascript",src="/javascripts/jquery-1.9.0.min.js") script(type="text/javascript",src="/javascripts/jquery-1.9.0.min.js")
block head
body body
block content block content