2013-01-30 23:09:00 +00:00
|
|
|
var restify = require('restify')
|
|
|
|
, util = require('util')
|
|
|
|
, EventEmitter = require('events').EventEmitter;
|
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
// constructor function
|
|
|
|
function Cosm(streams, key) {
|
2013-01-30 23:09:00 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
self._streams = streams;
|
|
|
|
self._interval = 5000;
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
self._jsonClient = restify.createJsonClient({
|
2013-01-30 23:09:00 +00:00
|
|
|
url: 'http://api.cosm.com',
|
|
|
|
headers: { 'X-ApiKey': key },
|
|
|
|
version:'*'
|
|
|
|
});
|
|
|
|
|
|
|
|
var recentvalues = {};
|
|
|
|
|
|
|
|
setInterval(function() {
|
|
|
|
|
|
|
|
for(var i=0; i<streams.length; i++) {
|
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
self._jsonClient.get('/v2/feeds/' + self._streams[i] + ".json", function(err, req, res, obj) {
|
2013-01-30 23:09:00 +00:00
|
|
|
if(err || obj.datastreams == null) {
|
|
|
|
console.error("error getting stream: " + err)
|
|
|
|
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 currentValue = dataStream.current_value;
|
|
|
|
|
|
|
|
if(isNumber(currentValue)) currentValue = parseFloat(currentValue);
|
|
|
|
|
|
|
|
var object = {
|
|
|
|
'stream': obj.id,
|
|
|
|
'feed': dataStream.id,
|
|
|
|
'tags': dataStream.tags,
|
|
|
|
'displayname': streamName,
|
|
|
|
'value': currentValue
|
|
|
|
};
|
|
|
|
|
2013-02-18 05:46:15 +00:00
|
|
|
var address = obj.id + ":" + dataStream.id;
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-18 05:46:15 +00:00
|
|
|
console.log(recentvalues);
|
|
|
|
if(recentvalues[address] != currentValue) {
|
2013-01-30 23:09:00 +00:00
|
|
|
self.emit('changedvalue', object);
|
|
|
|
}
|
2013-02-18 05:46:15 +00:00
|
|
|
else
|
|
|
|
self.emit('updatevalue', object);
|
2013-01-30 23:09:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
recentvalues[address] = currentValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
}, self._interval);
|
|
|
|
}
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
util.inherits(Cosm, EventEmitter);
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
Cosm.prototype.getStreams = function(streams, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
for(var i=0; i<streams.length; i++) {
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
self._jsonClient.get('/v2/feeds/' + streams[i] + ".json", function(err, req, res, obj) {
|
|
|
|
if(err || obj.datastreams == null) {
|
|
|
|
console.error("error getting stream: " + err)
|
|
|
|
return;
|
|
|
|
}
|
2013-02-18 05:46:15 +00:00
|
|
|
// console.dir(obj);
|
2013-02-12 23:12:44 +00:00
|
|
|
var object = {
|
2013-02-18 05:46:15 +00:00
|
|
|
id: obj.id,
|
2013-02-12 23:12:44 +00:00
|
|
|
description: obj.description,
|
|
|
|
lat: typeof obj.location === 'undefined' ? 'undefined' : obj.location.lat,
|
|
|
|
lon: typeof obj.location === 'undefined' ? 'undefined' : obj.location.lon,
|
|
|
|
location: typeof obj.location === 'undefined' ? 'undefined' :obj.location.name,
|
|
|
|
title: obj.title
|
|
|
|
};
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
if(callback != 'undefined') callback(object);
|
2013-01-30 23:09:00 +00:00
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Cosm;
|
2013-01-30 23:09:00 +00:00
|
|
|
|
|
|
|
|
2013-02-12 23:12:44 +00:00
|
|
|
|
|
|
|
function isNumber(value) {
|
|
|
|
if ((undefined === value) || (null === value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof value == 'number') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !isNaN(value - 0);
|
|
|
|
}
|