Class: LogStash::Inputs::Airthings

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

Instance Method Summary collapse

Instance Method Details

#registerObject



105
106
107
108
# File 'lib/logstash/inputs/airthings.rb', line 105

def register
  @logger.info("Connecting to Airthings API", :client_id => @client_id)
  @conn = AirThingsApiConnection.new(@client_id, @client_secret, @logger)
end

#run(queue) ⇒ Object

def register



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/logstash/inputs/airthings.rb', line 110

def run(queue)
  # we can abort the loop if stop? becomes true
  reload_devices_at = 0
  devices = []
  time_by_device = {}
  
  while !stop?
    now = Time.now.to_i

    if !devices || now >= reload_devices_at
      @logger.info("Getting devices from Airthings API")
      devices = @conn.get_devices

      unless devices
        devices = []
      end

      reload_devices_at = now + @device_list_interval
    end
    
    devices.each { |device|
      device_id = device["id"]
      
      @logger.debug("Getting latest values from Airthings API", :device_id => device_id)
      latest_values = @conn.get_latest_values(device_id)

      time = latest_values.delete("time")

      unless time_by_device[device_id] == time
        time_by_device[device_id] = time
        
        airthings = {
          device: {
            id: device_id,
            type: device["deviceType"],
            segment: device["segment"],
            location: device["location"]
          },
          time: time,
          metrics: latest_values
        }

        event = LogStash::Event.new("airthings" => airthings)
        decorate(event)
        queue << event
      end
    }
  
    # because the sleep interval can be big, when shutdown happens
    # we want to be able to abort the sleep
    # Stud.stoppable_sleep will frequently evaluate the given block
    # and abort the sleep(@interval) if the return value is true
    Stud.stoppable_sleep(@interval) { stop? }
  end # loop
end

#stopObject

def run



166
167
168
169
170
171
172
# File 'lib/logstash/inputs/airthings.rb', line 166

def stop
  # nothing to do in this case so it is not necessary to define stop
  # examples of common "stop" tasks:
  #  * close sockets (unblocking blocking reads/accepts)
  #  * cleanup temporary files
  #  * terminate spawned threads
end