首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ruby上使用Xively库?

如何在Ruby上使用Xively库?
EN

Stack Overflow用户
提问于 2013-09-11 13:09:35
回答 2查看 268关注 0票数 1

我试着从ruby上传一些数据到xively,我确实安装了所有的gem,这个测试代码运行正常,但是我的设备的xively图形没有任何变化。

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

我确实从代码中删除了我的个人数据,API_KEY,提要编号和提要名称。

代码语言:javascript
复制
#!/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,如果不是很关键,只要简单地指出它,不要离题,我会很乐意稍后研究和学习)

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

EN

回答 2

Stack Overflow用户

发布于 2013-09-11 13:26:18

我已经找到了可以帮助你使用talking api的链接

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

您可以使用

代码语言:javascript
复制
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"

创建一个数据点

数据点创建端点需要一个数据点数组

代码语言:javascript
复制
  datapoint = Xively::Datapoint.new(:at => Time.now, :value => "25")
  client.post('/v2/feeds/504/datastreams/temperature/datapoints', :body => {:datapoints => [datapoint]}.to_json)
票数 2
EN

Stack Overflow用户

发布于 2013-09-11 13:53:15

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

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

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

代码语言:javascript
复制
#!/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

使用这个类:

代码语言:javascript
复制
#!/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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18733081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档