-
Notifications
You must be signed in to change notification settings - Fork 1
/
clip.rb
126 lines (93 loc) · 3.77 KB
/
clip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# encoding: utf-8
require "logstash/inputs/base"
require "logstash/namespace"
require "oauth"
require "json"
# Poll CubeSensors for the given keys.
#
class LogStash::Inputs::Clip < LogStash::Inputs::Base
config_name "clip"
milestone 1
# Set this to true to enable debugging on an input.
config :debug, :validate => :boolean, :default => false
# Key
config :consumer_key, :validate => :string, :required => true
# Secret
config :consumer_secret, :validate => :string, :required => true
# Token
config :token, :validate => :string, :required => true
# Token Secret
config :token_secret, :validate => :string, :required => true
# Interval to run the command. Value is in seconds.
config :interval, :validate => :number, :required => true
CS_API = "https://api.cubesensors.com"
public
def register
@logger.info("Registering Cbsr Input", :type => @type,
:consumer_key => @consumer_key, :interval => @interval)
# Here would go the Oauth stuff...
end # def register
public
def run(queue)
loop do
start = Time.now
@logger.info? && @logger.info("Polling CBSR", :consumer_key => @consumer_key)
#GET data
# The consumer key and consumer secret are the identifiers for this particular application, and are
# issued when the application is registered with the site. Use your own.
@consumer=OAuth::Consumer.new consumer_key,
consumer_secret, {
:site => CS_API,
:scheme => :query_string,
:request_token_path => "/auth/request_token",
:access_token_path => "/auth/access_token",
:authorize_path => "/auth/authorize"
}
#this lets you see raw wire calls
if @debug
@consumer.http.set_debug_output($stdout)
end
# Create the access_token for all traffic
@access_token = OAuth::AccessToken.new(@consumer, token, token_secret)
# Use the access token for various commands. Although these take plain strings, other API methods
dev = JSON.parse(@access_token.get("/v1/devices/").body)
if @debug
puts JSON.pretty_generate(dev)
end
dev["devices"].each do |device|
cubeCur = JSON.parse(@access_token.get("/v1/devices/" + device["uid"] + "/current").body)
# get all data for the current device
if @debug
puts "Cube: " + device["extra"]["name"] + "(" + device["uid"] + ")"
end
# create an event
event = LogStash::Event.new(
"source" => CS_API + "/v1/devices/" + device["uid"] + "/current",
"cubeId" => device["uid"],
"cubeName" => device["extra"]["name"]
)
cubeCur["field_list"].each_index do |i|
if @debug
puts " " + cubeCur["field_list"][i] + ": " + cubeCur["results"][0][i].to_s
end
event[cubeCur["field_list"][i]] = cubeCur["results"][0][i]
end # device loop
# put the event to the queue
decorate(event)
queue << event
end # device loop
duration = Time.now - start
@logger.info? && @logger.info("Polling CBSR completed", :consumer_key => @consumer_key, :duration => duration)
# Sleep for the remainder of the interval, or 0 if the duration ran
# longer than the interval.
sleeptime = [0, @interval - duration].max
if sleeptime == 0
@logger.warn("Polling CBSR ran longer than the interval. Skipping sleep.",
:consumer_key => @consumer_key, :duration => duration,
:interval => @interval)
else
sleep(sleeptime)
end
end # loop
end # def run
end # class LogStash::Inputs::Clip