cosm

How to query cosm json feed using the cosm javascript library


I am new to web development and I have bit off more than I can chew.

So far, I successfully have created a website to query the latest data at cosm.com

Now I am trying to save the last 10 data points from the cosm.com feed to an array using the cosm javascript library. I can't get the right syntax and I can't find examples to guide me.

cosm.feed.history( 12068, duration:'30seconds', callback(data) );
console.log(data);

http://jsfiddle.net/spuder/29cFT/12/

http://cosm.github.com/cosm-js/docs/


UPDATE 2013-4-14 After implementing @bjpirt's solution, I noticed I wasn't getting 'every' value returned inside the specified duration. Solved it by adding "interval:0" to the request.

  cosm.datastream.history( cosmFeed1, cosmDataStream1, {duration:'60seconds', interval:0}, getHistory );

http://jsfiddle.net/spuder/ft2MJ/1/


Solution

  • @lebreeze is correct with his advice. I got your JSFiddle working so that it is now fetching data from the Cosm API:

    http://jsfiddle.net/nLt33/2/

    I had to make a few changes to get it working, any of which would have been causing you errors:

    Also, that feed doesn't seem to have been updated recently.

    Here's the updated code which seems to be working fine now:

        //read only key
        cosm.setKey("-Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g");
        var cosmFeed = 120687;
        var cosmDataStream = "sensor_reading";
    
        $(document).ready( function()  {
            var success = function(data){
                for(var datapoint in data.datapoints){
                    var dp = data.datapoints[datapoint];
                    $('#stuff').append('<li>Value: ' + dp.value + ' At: ' + dp.at + '</li>');
                }
            }
    
            //Print out the last 10 readings or so
            cosm.datastream.history( cosmFeed, cosmDataStream, {duration:'1day'}, success ); 
        })
    

    It's difficult to get just the last x datapoints (that's something we should change in the API I think) - what you'd normally do is ask for a specific time period.

    Hope this helps.