如何使用cosm javascript库查询cosm json feed

时间:2013-04-04 05:50:04

标签: cosm

我是网络开发的新手,我的嚼得比我嚼得多。

到目前为止,我已成功创建了一个网站,以查询cosm.com上的最新数据

现在我尝试使用cosm javascript库将cosm.com提要中的最后10个数据点保存到数组中。我无法获得正确的语法,我找不到示例来指导我。

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

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

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


更新2013-4-14 在实现@ b​​jpirt的解决方案之后,我注意到我没有在指定的持续时间内返回'每个'值。 通过在请求中添加“interval:0”解决了这个问题。

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

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

2 个答案:

答案 0 :(得分:2)

您可能需要在duration:'30seconds'

中包含{} json选项

尝试类似:

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

答案 1 :(得分:2)

@lebreeze对他的建议是正确的。我让你的JSFiddle工作,现在它正在从Cosm API中获取数据:

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

我必须进行一些更改才能使其正常工作,其中任何一项都会导致错误:

  • Feed ID和数据流ID不正确
  • 您没有回调函数
  • 这些选项不在javascript对象中

此外,该Feed最近似乎没有更新。

这是更新的代码,现在看起来工作正常:

    //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 ); 
    })

很难获得最后的x数据点(我认为这是我们应该在API中改变的) - 你通常要做的是询问特定的时间段。

希望这有帮助。