2013-01-15 21:12:00 +00:00
|
|
|
var assert = require('assert'),
|
|
|
|
restify = require('restify'),
|
|
|
|
osc = require('node-osc');
|
2012-12-17 13:58:03 +00:00
|
|
|
|
2013-01-15 21:12:00 +00:00
|
|
|
var hubAddress = '192.168.23.43', hubPort = 7110;
|
|
|
|
var oscClient = new osc.Client(hubAddress, hubPort);
|
2012-12-17 13:58:03 +00:00
|
|
|
|
2013-01-15 21:12:00 +00:00
|
|
|
var streams = [ 91755, 70632, 53146, 45582 ];
|
2012-12-17 13:58:03 +00:00
|
|
|
|
|
|
|
var client = restify.createJsonClient({
|
2013-01-15 21:12:00 +00:00
|
|
|
url: 'http://api.cosm.com', headers: {'X-ApiKey':'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'},version:'*'
|
2012-12-17 13:58:03 +00:00
|
|
|
});
|
|
|
|
|
2013-01-15 21:12:00 +00:00
|
|
|
var recentvalues = {};
|
2012-12-17 13:58:03 +00:00
|
|
|
|
|
|
|
var getstream = function(streamId) {
|
2012-12-18 15:21:46 +00:00
|
|
|
client.get('/v2/feeds/' + streamId + ".json", function(err, req, res, obj) {
|
2012-12-17 13:58:03 +00:00
|
|
|
|
2012-12-18 15:21:46 +00:00
|
|
|
if(err == null && obj.datastreams != null) {
|
2012-12-17 13:58:03 +00:00
|
|
|
for(var i=0;i<obj.datastreams.length;i++) {
|
2013-01-15 21:12:00 +00:00
|
|
|
var dataStream = obj.datastreams[i];
|
|
|
|
if(dataStream.id != null) {
|
|
|
|
var streamName = dataStream.id;
|
|
|
|
if(streamName.length < 2 && dataStream.tags.length > 0) streamName = dataStream.tags[0] + streamName;
|
|
|
|
|
|
|
|
var address = '/cosm/' + obj.id + "/" + streamName;
|
|
|
|
var currentValue = dataStream.current_value;
|
|
|
|
|
|
|
|
|
|
|
|
if(isNumber(currentValue)) currentValue = parseFloat(currentValue);
|
|
|
|
|
|
|
|
if(recentvalues[address] != currentValue) {
|
|
|
|
oscClient.send(address, currentValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
recentvalues[address] = currentValue;
|
2012-12-17 13:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 15:21:46 +00:00
|
|
|
} else {
|
|
|
|
console.error("receive failure %s", err);
|
2012-12-17 13:58:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:12:00 +00:00
|
|
|
setInterval(function() {
|
|
|
|
for(var i=0;i<streams.length;i++) {
|
|
|
|
getstream(streams[i]);
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
function isNumber(value) {
|
|
|
|
if ((undefined === value) || (null === value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof value == 'number') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !isNaN(value - 0);
|
|
|
|
}
|