0

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/

spuder
  • 17,437
  • 19
  • 87
  • 153

2 Answers2

2

You may need to wrap your duration:'30seconds' json options in {}

Try something like:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://d23cj0cdvyoxg0.cloudfront.net/cosmjs-1.0.0.min.js"></script>
<script>..
  cosm.setKey( "APIKEY" );
  cosm.feed.history(40360, {duration:'30seconds'}, function(data){
    console.log(data);
    console.log(data.datastreams);
  });
</script>
lebreeze
  • 5,094
  • 26
  • 34
2

@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:

  • The feed ID and datastream ID were incorrect
  • You didn't have a callback function
  • The options weren't in a javascript object

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.

bjpirt
  • 236
  • 1
  • 1
  • Thanks so much. My end goal is to see if there has been any movement on the sensor in the last 30 seconds. (which there hasn't been lately) Pulling the last 10 readings may not be the best way as you suggested. I will research the time period option. – spuder Apr 06 '13 at 02:34