62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
var assert = require('assert'),
|
|
restify = require('restify'),
|
|
osc = require('node-osc');
|
|
|
|
var hubAddress = '192.168.23.43', hubPort = 7110;
|
|
var oscClient = new osc.Client(hubAddress, hubPort);
|
|
|
|
var streams = [ 91755, 70632, 53146, 45582, 64590 ];
|
|
|
|
var client = restify.createJsonClient({
|
|
url: 'http://api.cosm.com', headers: {'X-ApiKey':'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'},version:'*'
|
|
});
|
|
|
|
var recentvalues = {};
|
|
|
|
var getstream = function(streamId) {
|
|
client.get('/v2/feeds/' + streamId + ".json", function(err, req, res, obj) {
|
|
|
|
if(err == null && obj.datastreams != null) {
|
|
for(var i=0;i<obj.datastreams.length;i++) {
|
|
var dataStream = obj.datastreams[i];
|
|
if(dataStream.id != null) {
|
|
var streamName = dataStream.id;
|
|
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);
|
|
console.log(address + ": " + currentValue);
|
|
}
|
|
|
|
recentvalues[address] = currentValue;
|
|
}
|
|
}
|
|
} else {
|
|
console.error("receive failure %s", err);
|
|
}
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|