chaosc-fetcher/cosmfetcher.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

var assert = require('assert'),
restify = require('restify'),
osc = require('node-osc');
2012-12-17 13:58:03 +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-22 12:59:56 +00:00
var streams = [ 91755, 70632, 53146, 45582, 64590 ];
2012-12-17 13:58:03 +00:00
var client = restify.createJsonClient({
url: 'http://api.cosm.com', headers: {'X-ApiKey':'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'},version:'*'
2012-12-17 13:58:03 +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++) {
var dataStream = obj.datastreams[i];
if(dataStream.id != null) {
var streamName = dataStream.id;
2013-01-22 12:59:56 +00:00
if(streamName.length < 2 && dataStream.tags != null) 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);
2013-01-22 12:59:56 +00:00
console.log(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
}
});
}
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);
}