Class: SensorPushApiConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/sensorpush.rb

Overview

Generate a repeating message.

This plugin is intented only as an example.

Instance Method Summary collapse

Constructor Details

#initialize(email, password, logger) ⇒ SensorPushApiConnection

Returns a new instance of SensorPushApiConnection.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/logstash/inputs/sensorpush.rb', line 11

def initialize(email, password, logger)
  @session = Net::HTTP.start("api.sensorpush.com", 443, use_ssl: true)
  
  @logger = logger
  @email = email
  @password = password

  @token_hash = {
    expires: 0
  }
end

Instance Method Details

#get_latest_values(sensor, to = nil, from = nil) ⇒ Object



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
# File 'lib/logstash/inputs/sensorpush.rb', line 88

def get_latest_values(sensor, to = nil, from = nil)
  sensor_id = sensor["id"]
  
  body = {
    sensors: [sensor_id],
    limit: 300
  }

  if !from.nil?
    body["startTime"] = from.iso8601(3)
  end
  
  if !to.nil?
    body["stopTime"] = to.iso8601(3)
  end
  
  req = Net::HTTP::Post.new("/api/v1/samples")
  req.body = JSON.generate(body)
  resp = self.send_request(req)

  ret = resp["sensors"][sensor_id]

  if ret.nil?
    return []
  end
  
  return ret
end

#get_sensors_mapObject



82
83
84
85
86
# File 'lib/logstash/inputs/sensorpush.rb', line 82

def get_sensors_map
  resp = self.send_request(Net::HTTP::Post.new("/api/v1/devices/sensors"))
  
  return resp
end

#get_tokenObject



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
# File 'lib/logstash/inputs/sensorpush.rb', line 23

def get_token
  now = Time.now.to_i

  expires = @token_hash[:expires]

  if expires < now
    @logger.debug("Getting SensorPush API authorization token")
    
    auth_req = Net::HTTP::Post.new("/api/v1/oauth/authorize")
    auth_req.body = JSON.generate({
      email: @email,
      password: @password.value
    })
    auth_req["Accept"] = "application/json"
    auth_req["Content-Type"] = "application/json"
    
    auth_resp = @session.request(auth_req)

    auth_resp_json = JSON.parse(auth_resp.body)


    @logger.debug("Getting SensorPush API access token")
    
    token_req = Net::HTTP::Post.new("/api/v1/oauth/accesstoken")
    token_req.body = JSON.generate({
      authorization: auth_resp_json["authorization"]
    })
    token_req["Accept"] = "application/json"
    token_req["Content-Type"] = "application/json"
    
    token_resp = @session.request(token_req)

    token_resp_json = JSON.parse(token_resp.body)

    @token_hash = {
      expires: now + 120,
      response: token_resp_json
    }
  end

  return @token_hash[:response]["accesstoken"]
end

#send_request(req) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/logstash/inputs/sensorpush.rb', line 66

def send_request(req)
  req["Accept"] = "application/json"
  req["Authorization"] = self.get_token
  
  resp = @session.request(req)

  resp_json = JSON.parse(resp.body)

  error = resp_json["error_description"]
  if error
    @logger.error("Error received from SensorPush API", :error => error)
  end

  return resp_json
end