Method: Ardtweeno::API.retrievepackets

Defined in:
lib/ardtweeno/api.rb

.retrievepackets(nodeList, params) ⇒ Object

Ardtweeno::API#retrievepackets method to filter packet data according to REST API request

  • Args :

    • ++ ->

  • Returns : -

  • Raises :



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
187
188
189
190
191
192
193
# File 'lib/ardtweeno/api.rb', line 140

def retrievepackets(nodeList, params)
  @log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
  @log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
  
  params = params.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
  
  packetqueue = Array.new

  if params.has_key?(:node)
    @log.debug "the params has a node key"
    nodeList.each do |i|
      @log.debug "Matching #{i.node} with #{params[:node]}"
      if i.node == params[:node]
        @log.debug params
        @log.debug i.node            
        packetqueue = i.packetqueue # Found the node we are interested in,
                                    # extract the packets                        
        break # Node found break and continue
      end
    end
  else # Aggregate all

    @log.debug "the params does not have a node key"
    nodeList.each do |i|
      packetqueue += i.packetqueue
      packetqueue = packetqueue.sort_by {|x| x.seqNo} # Not exactly ideal.. but it works ;p
    end
  end
  
  # From most specific to least specific
  
  if params.has_key?(:seqno)
    packetqueue = handleSeqNo(packetqueue, params)
  end
  
  if params.has_key?(:minute)
    packetqueue = handleMinute(packetqueue, params)
  end
 
  if params.has_key?(:hour)
    packetqueue = handleHour(packetqueue, params)
  end
                
  if params.has_key?(:date)
    packetqueue = handleDate(packetqueue, params)
  end


  packets = handlePagination(packetqueue, params) # Perform pagination operations on results
  
  final = {:packets=>packets, :total=>packetqueue.size, :found=>packets.size}
  
  return final # Return the final results
end