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" /> <option name="VERSION" />
</GetOptions> </GetOptions>
</component> </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> </project>

View File

@ -1,53 +1,56 @@
var assert = require('assert'), // this is a json fetcher for streams from cosm.com
restify = require('restify'), // to use, simply start "node cosmfetcher.js <hubAddress> <hubPort>"
osc = require('node-osc'); // 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 oscClient = new osc.Client(hubAddress, hubPort);
var cosmClient = restify.createJsonClient({
var streams = [ 91755, 70632, 53146, 45582, 64590 ]; url: 'http://api.cosm.com',
headers: { 'X-ApiKey':'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g' },
var client = restify.createJsonClient({ version:'*'
url: 'http://api.cosm.com', headers: {'X-ApiKey':'orKBBdLAKuKJU-RxqmZpZB6q0baSAKxBTVhKdjhUNkdyVT0g'},version:'*'
}); });
var recentvalues = {}; var recentvalues = {};
var getstream = function(streamId) { setInterval(function() {
client.get('/v2/feeds/' + streamId + ".json", function(err, req, res, obj) { 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;
if(err == null && obj.datastreams != null) {
for(var i=0; i<obj.datastreams.length; i++) { for(var i=0; i<obj.datastreams.length; i++) {
var dataStream = obj.datastreams[i]; var dataStream = obj.datastreams[i];
if(dataStream.id != null) { if(dataStream.id == null) continue;
var streamName = dataStream.id; var streamName = dataStream.id;
if(streamName.length < 2 && dataStream.tags != null) streamName = dataStream.tags[0] + streamName; if(streamName.length < 2 && dataStream.tags != null) {
streamName = dataStream.tags[0] + streamName;
}
var address = '/cosm/' + obj.id + "/" + streamName; var address = '/cosm/' + obj.id + "/" + streamName;
var currentValue = dataStream.current_value; var currentValue = dataStream.current_value;
if(isNumber(currentValue)) currentValue = parseFloat(currentValue); if(isNumber(currentValue)) currentValue = parseFloat(currentValue);
if(recentvalues[address] != currentValue) { if(sendAlways || recentvalues[address] != currentValue) {
oscClient.send(address, currentValue); oscClient.send(address, currentValue);
console.log(address + ": " + currentValue); console.log(address + "," + currentValue);
} }
recentvalues[address] = currentValue; recentvalues[address] = currentValue;
} }
}
} else {
console.error("receive failure %s", err);
}
}); });
} }
setInterval(function() { }, pollingInterval);
for(var i=0;i<streams.length;i++) {
getstream(streams[i]);
}
}, 1000);
function isNumber(value) { function isNumber(value) {

View File

@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<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>