代码之家  ›  专栏  ›  技术社区  ›  spuder

如何使用cosm javascript库查询cosm-json提要

  •  0
  • spuder  · 技术社区  · 13 年前

    我是网络开发的新手,我有点力不从心。

    到目前为止,我已经成功创建了一个网站,在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 在实现@bjpirt的解决方案后,我注意到我没有在指定的持续时间内返回“every”值。 通过在请求中添加“interval:0”来解决此问题。

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

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

    2 回复  |  直到 13 年前
        1
  •  2
  •   bjpirt    13 年前

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

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

    为了让它正常工作,我不得不做了一些更改,其中任何一个都会给你带来错误:

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

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

    以下是更新后的代码,现在似乎运行良好:

        //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中更改的东西)-通常您会要求指定一个特定的时间段。

    希望这能有所帮助。

        2
  •  2
  •   lebreeze    13 年前

    你可能需要把你的 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>
    
    推荐文章