Class: LogStash::Inputs::SensorPush

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

Instance Method Summary collapse

Instance Method Details

#registerObject



134
135
136
137
# File 'lib/logstash/inputs/sensorpush.rb', line 134

def register
  @logger.info("Connecting to SensorPush API", :email => @email)
  @conn = SensorPushApiConnection.new(@email, @password, @logger)
end

#run(queue) ⇒ Object

def register



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/logstash/inputs/sensorpush.rb', line 139

def run(queue)
  # we can abort the loop if stop? becomes true
  time_by_sensor = {}
  
  while !stop?
    now = DateTime.now
    
    @logger.debug("Getting sensors from SensorPush API")
    sensors = @conn.get_sensors_map

    sensors.each_value { |sensor|
      sensor_id = sensor["id"]

      last_read = time_by_sensor[sensor_id]

      @logger.debug("Getting latest values from SensorPush API", :sensor_id => sensor_id, :since => last_read)

      if last_read
        latest_values = @conn.get_latest_values(sensor, now, last_read)
      else
        latest_values = @conn.get_latest_values(sensor)
      end

      unless latest_values.empty?
        time_by_sensor[sensor_id] = DateTime.parse(latest_values.map { |v| v["observed"] }.max)
  
        latest_values.each { |reading|
          timestamp = reading["observed"]
  
          sensorpush = {
            sensor: sensor,
            reading: reading
          }

          event = LogStash::Event.new("@timestamp" => timestamp, "sensorpush" => sensorpush)
          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



188
189
190
191
192
193
194
# File 'lib/logstash/inputs/sensorpush.rb', line 188

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