This commit is contained in:
Lucas Pleß 2013-01-25 15:59:00 +01:00
parent aa89630f7c
commit eb808ae405
4 changed files with 53 additions and 58 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>

View File

@ -55,21 +55,5 @@
<option name="VERSION" />
</GetOptions>
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.6</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.20000003" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

@ -1,53 +1,56 @@
var assert = require('assert'),
restify = require('restify'),
osc = require('node-osc');
// this is a json fetcher for streams from cosm.com
// to use, simply start "node cosmfetcher.js <hubAddress> <hubPort>"
// configuration
var streams = [ 91755, 70632, 53146, 45582, 64590 ]; // configure which cosm streams to fetch
var pollingInterval = 1000; // in ms
var sendAlways = false; // if set to true, fetcher will always send osc messages
// regardless if value changed
// end configuration
var hubAddress = '192.168.23.43', hubPort = 7110;
var restify = require('restify'), osc = require('node-osc');
var hubAddress = process.argv.length > 2 ? process.argv[2] : '192.168.23.43';
var hubPort = process.argv.length > 3 ? process.argv[3] : 7110;
console.log("using " + hubAddress + ":" + hubPort + " as hub");
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 cosmClient = 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]);
for(var i=0; i<streams.length; i++) {
cosmClient.get('/v2/feeds/' + streams[i] + ".json", function(err, req, res, obj) {
if(err || obj.datastreams == null) 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 address = '/cosm/' + obj.id + "/" + streamName;
var currentValue = dataStream.current_value;
if(isNumber(currentValue)) currentValue = parseFloat(currentValue);
if(sendAlways || recentvalues[address] != currentValue) {
oscClient.send(address, currentValue);
console.log(address + "," + currentValue);
}
recentvalues[address] = currentValue;
}
});
}
}, 1000);
}, pollingInterval);
function isNumber(value) {

View File

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