【问题标题】:how to use Xively API Library on Ruby?如何在 Ruby 上使用 Xively API 库?
【发布时间】:2013-09-11 14:19:43
【问题描述】:

我正在尝试将一些数据从 ruby​​ 上传到 xively,我确实安装了所有 gem,并且此测试代码运行正常,但我设备的 xively 图表没有任何变化。

这个小代码是从一个运行良好的大代码片段中分离出来的,并使用用 php 编写的接口将数据发布到我的服务器,但现在我想使用 xively 来记录数据。

我确实从该代码中删除了我的个人数据、API_KEY、Feed 编号和 Feed 名称。

#!/usr/bin/ruby

require 'rubygems'
require 'json'
require 'xively-rb'

##Creating the xively client instance
API_KEY = "MY_API_KEY_WAS_HERE"
client = Xively::Client.new(API_KEY)

#on an endless loop
while true

  #n is a random float between 0 y 1
  n = rand()

  ##Creating datapoint and sendig it to xively
  puts "Creating datapoint "+Time.now.to_s+", "+n.to_s+" and sending it to xively"

  datapoint = Xively::Datapoint.new(:at => Time.now, :value => n)

  client.post('/api/v2/feeds/[number]/datastreams/[name]', :body => {:datapoints => [datapoint]}.to_json)

end

如果能得到一个关于如何使用那个库的例子就好了,我没有找到任何简洁的例子。

(可能会在代码中发现一些愚蠢的错误,如果是这样,那没关系,因为我现在正在学习 ruby​​,如果不是很重要,请简要指出不要跑题,我很乐意以后研究和学习)

我真的很期待一些答案,所以提前谢谢。

【问题讨论】:

    标签: ruby xively


    【解决方案1】:

    我找到了可以帮助你说话 api 的链接

    https://github.com/xively/xively-rb/wiki/Talking-to-the-REST-API

    你可以使用

    client = Xively::Client.new(YOUR_API_KEY)
    response = client.post('/v2/feeds.json', :body => feed.to_json)
    puts response.headers['location'] # Will give us the location of the Xively feed including    the ID
    => "http://api.xively.com/v2/feeds/SOMEID"
    

    创建数据点

    数据点创建端点采用数据点数组

      datapoint = Xively::Datapoint.new(:at => Time.now, :value => "25")
      client.post('/v2/feeds/504/datastreams/temperature/datapoints', :body => {:datapoints => [datapoint]}.to_json)
    

    【讨论】:

    • 嗨,谢谢,但我确实读过这篇文章,发现它不像我正在寻找的答案那么明确。无论如何,感谢您成为第一个答案。我现在有一个解决方案,这是一些同学所做的旧/以前的 Cosm API 的修改示例,我将其作为解决方案发布在这里。谢谢!
    【解决方案2】:

    我从一位同学那里收到了一个可行的解决方案,它是在一篇关于 Cosm 的帖子中,现在是 xively,以前也是 pachube。

    我们花了大约两周的时间寻找这样的东西:

    afulki.net more-on-ruby-and-cosm

    #!/usr/bin/ruby
    
    require 'xively-rb'
    require 'json'
    require 'rubygems'
    
    class XivelyConnector
      API_KEY = 'MY_API_KEY_HARD-CODED_HERE'
    
      def initialize( xively_feed_id )
        @feed_id = xively_feed_id
    
        @xively_response = Xively::Client.get("/v2/feeds/#{@feed_id}.json", :headers => {"X-ApiKey" => API_KEY})
      end
    
      def post_polucion( sensor, polucion_en_mgxm3 )
        return unless has_sensor? sensor
    
        post_path          = "/v2/feeds/#{@feed_id}/datastreams/#{sensor}/datapoints"
        datapoint          = Xively::Datapoint.new(:at => Time.now, :value => polucion_en_mgxm3.to_s )
        response           = Xively::Client.post(post_path,
          :headers => {"X-ApiKey" => API_KEY},
          :body    => {:datapoints => [datapoint]}.to_json)
      end
    
      def has_sensor?( sensor )
        @xively_response["datastreams"].index { |ds| ds["id"] == sensor }
      end
    end
    

    使用该类:

    #!/usr/bin/ruby
    
    require 'rubygems'
    require 'json' 
    require 'xively-rb'
    require_relative 'XivelyConnector'
    
    xively_connector = XivelyConnector.new( MY_FEED_ID_HERE )
    
    while true
    n = rand()
    xively_connector.post_polucion 'Sensor-Asdf', n
    
    sleep 1
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      相关资源
      最近更新 更多