Module: Fluent::Plugin::EverySenseProxy

Included in:
EverySenseInput, EverySenseOutput
Defined in:
lib/fluent/plugin/everysense_proxy.rb

Defined Under Namespace

Classes: EverySenseProxyError

Instance Method Summary collapse

Instance Method Details

#create_sessionObject



54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/everysense_proxy.rb', line 54

def create_session
  return @session_key if valid_session?
  @session_req ||= create_session_request
  session_res = @https.request(@session_req)
  return nil if !error_handler(session_res, 'create_session failed.')
  @session_key = JSON.parse(session_res.body)["session_key"]
  @session_expires_in = Time.now + 60 * 60 * 12 # assuming time out in a half day
end

#create_session_requestObject



47
48
49
50
51
52
# File 'lib/fluent/plugin/everysense_proxy.rb', line 47

def create_session_request
  session_req = Net::HTTP::Post.new(@uri + '/session')
  session_req.body = {login_name: @login_name, password: @password}.to_json
  session_req.content_type = 'application/json'
  session_req
end

#delete_sessionObject



67
68
69
70
71
# File 'lib/fluent/plugin/everysense_proxy.rb', line 67

def delete_session
  return if !valid_session?
  del_session_res = @https.request(delete_session_request)
  error_handler(del_session_res, 'delete_session failed.')
end

#delete_session_requestObject



63
64
65
# File 'lib/fluent/plugin/everysense_proxy.rb', line 63

def delete_session_request
  Net::HTTP::Delete.new(@uri + "/session/#{@session_key}")
end

#error_handler(response, message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/fluent/plugin/everysense_proxy.rb', line 26

def error_handler(response, message)
  if response.code != "200"
    log.error error: message
    log.debug "code: #{response.code}"
    log.debug "message: #{response.message}"
    log.debug "body: #{response.body}"
    return false
  end
  return true
end

#get_messagesObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/everysense_proxy.rb', line 126

def get_messages
  if !valid_session?
    return nil if create_session.nil?
    log.debug "session #{@session_key} created."
  end
  get_messages_res = @https.get(get_messages_request)
  return nil if !error_handler(get_messages_res,"get_messages failed.")
  log.debug "get_message: #{get_messages_res.body}"
  get_messages_res.body
end

#get_messages_paramsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/everysense_proxy.rb', line 96

def get_messages_params
  params = {
    session_key: @session_key,
    from: @from,
    to: @to,
    limit: @limit
  }
  if !@device_id.nil?
    return params
  elsif !@recipe_id.nil?
    return params.merge({keep: @keep, inline: @inline, format: @format})
  else
    raise ConfigError, "device_id or recipe_id must be specified."
  end
end

#get_messages_requestObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/everysense_proxy.rb', line 112

def get_messages_request
  from = Time.parse(@from)
  to = from + @interval
  to = Time.now if to > Time.now
  @to = to.iso8601
  get_messages_req = @uri + target_path
  get_messages_req.query = URI.encode_www_form(get_messages_params)
  log.debug "#{get_messages_req}?#{get_messages_req.query}"
  # currently time window is automatically updated
  #@from = Time.now.iso8601
  @from = @to
  get_messages_req
end

#put_message(message) ⇒ Object



80
81
82
83
84
# File 'lib/fluent/plugin/everysense_proxy.rb', line 80

def put_message(message)
  log.debug "put_message: #{message}"
  put_message_res = @https.request(put_message_request(message))
  error_handler(put_message_res, "put_message: '#{message}' failed.")
end

#put_message_request(message) ⇒ Object



73
74
75
76
77
78
# File 'lib/fluent/plugin/everysense_proxy.rb', line 73

def put_message_request(message)
  put_message_req = Net::HTTP::Post.new(@uri + "/device_data/#{@device_id}")
  put_message_req.body = message
  put_message_req.content_type = "application/#{@format}"
  put_message_req
end

#shutdown_proxyObject



20
21
22
23
24
# File 'lib/fluent/plugin/everysense_proxy.rb', line 20

def shutdown_proxy
  log.debug "shutdown_proxy #{@session_key}"
  delete_session
  @https.finish() if @https.active?
end

#start_proxyObject



11
12
13
14
15
16
17
18
# File 'lib/fluent/plugin/everysense_proxy.rb', line 11

def start_proxy
  log.debug "start everysense proxy #{@url}"

  @uri = URI.parse(@url)
  @https = Net::HTTP.new(@uri.host, @uri.port)
  @https.use_ssl = (@uri.scheme == 'https')
  @session_key = nil
end

#target_pathObject



86
87
88
89
90
91
92
93
94
# File 'lib/fluent/plugin/everysense_proxy.rb', line 86

def target_path
  if !@device_id.nil?
    return "/device_data/#{@device_id}"
  elsif !@recipe_id.nil?
    return "/recipe_data/#{@recipe_id}.#{@format}"
  else
    raise Fluent::ConfigError, "device_id or recipe_id must be specified."
  end
end

#valid_session?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/everysense_proxy.rb', line 37

def valid_session?
  # TODO validate @session_key using EverySense API
  if !@session_key.nil?
    if Time.now < @session_expires_in
      return true
    end
  end
  return false
end