class Wms::Input::Filetype1

Attributes

filepath[RW]

Public Instance Methods

register(options={}) click to toggle source
# File lib/wms/input/filetype1.rb, line 13
def register(options={})
  raise "#{self.class.name}: filepath required in options" unless options[:filepath]
  @filepath   = options[:filepath]
  @compressed = options[:compressed]
  @file_ext   = options[:file_ext]
  @is_gz      = options[:file_ext] == '.gz'
  
end
run(&block) click to toggle source

Read from csv file row by row. Callback function will be called when each row is done reading.

# File lib/wms/input/filetype1.rb, line 25
def run(&block)
  # adding options to make data manipulation easy
  total_lines = 0
  str_arr = []
  callback = block
  if @is_gz
    Zlib::GzipReader.open(@filepath) do |csv|
      csv.read.each_line do |line|
        splits    = line.split(',')
        type      = splits[0]
        timestr   = splits[1]
        timestamp = Time.at(splits[2].to_i / 1000.0, splits[2].to_i % 1000.0)
        device_id = splits[3]
        
        data = Hash.new
        data[:type] = type
        data[:timestamp] = timestamp
        data[:device_id] = device_id

        case type
        when "wifi_accesspoint_info"
          joined      = splits[4..splits.size].join(',')
          jsoned      = JSON.parse(joined)
          data[:list] = jsoned['list']
          callback.call(data)
        when "location"
          (4..splits.size-1).step(2).each  do |i| 
              data[splits[i]] = cascading_convert(splits[i+1])
              callback.call data
          end
        when "battery"
          (4..splits.size-1).step(2).each  do |i| 
              data[splits[i]] = cascading_convert(splits[i+1])
              callback.call data
          end 
        when "application"
          (4..splits.size-1).step(2).each  do |i| 
              data[splits[i]] = cascading_convert(splits[i+1])
              callback.call data
          end

        # when "wifiscan"

        else
          @logger.debug "Type #{type} is no supported!"
        end

        @logger.debug data


        # str_arr = []
        # str_arr << '['
        # str_arr << line
        # str_arr << ']'

        # joined_str = str_arr.join("")
        # @logger.debug "  ==> #{joined_str}"
        #
        # json_obj = JSON.parse(joined_str)

        # @logger.debug "  --> #{json_obj}"
        # norm_json = normlaize_json_obj(json_obj)
        # callback = block
        # callback.call(norm_json)
        # @logger.debug ">>>>>>#{norm_json}"

        total_lines += 1
      end
    end

    @logger.debug "Total line: %d" % total_lines
  else
    File.open(@filepath, "r").each_line do |line|
      str_arr = []
      str_arr << '['
      str_arr << line
      str_arr << ']'
      joined_str = str_arr.join("")
      json_obj = JSON.parse(joined_str)


      # norm_json = normlaize_json_obj(json_obj)
      # callback = block
      # callback.call(norm_json)
      # @logger.debug ">>>>>>#{norm_json}"

      total_lines += 1
    end
  end

end

Private Instance Methods

cascading_convert(value) click to toggle source

Try to convert in order Int > Float > String

# File lib/wms/input/filetype1.rb, line 120
def cascading_convert(value)
  result = nil
  begin
    result = Integer(value)
  rescue
    begin
      result = Float(value)
    rescue
      result = value
    end
  end
  result
end
convert_level_freq(wifi_list) click to toggle source
# File lib/wms/input/filetype1.rb, line 165
def convert_level_freq(wifi_list)
  wifi_list.each do |wifi|
    wifi['level'] = wifi['level'].to_i if wifi['level']
    wifi['frequency'] = wifi['frequency'].to_i if wifi['frequency']
  end
end
normlaize_json_obj(json_obj) click to toggle source
# File lib/wms/input/filetype1.rb, line 136
def normlaize_json_obj(json_obj)
  normlaized_json_obj = {}
  if json_obj[0] == 'wifi_accesspoint_info' 
    normlaized_json_obj['type'] = 'wifi_accesspoint_info'
    normlaized_json_obj['timestamp'] = Time.at(json_obj[2] / 1000.0, json_obj[2] % 1000.0)
    normlaized_json_obj['device_id'] = json_obj[3]
    normlaized_json_obj['wifi_list'] = json_obj[4]['list']

    # Convert string values into floats
    convert_level_freq(normlaized_json_obj['wifi_list'])


    
  elsif json_obj[0] == 'location'
    normlaized_json_obj['type'] = 'location'
    normlaized_json_obj['timestamp'] = Time.at(json_obj[2] / 1000.0, json_obj[2] % 1000.0)
    normlaized_json_obj['device_id'] = json_obj[3]
    stop_at = json_obj.length - 1
    range = (4..stop_at)
    range.step(2).each do |i|
      normlaized_json_obj[json_obj[i]] = json_obj[i+1]
    end
    # @logger.debug normlaized_json_obj
  end
            
  return normlaized_json_obj
end