Class: Wms::Input::Filetype1

Inherits:
Base show all
Defined in:
lib/wms/input/filetype1.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#tags

Attributes included from Config::Mixin

#config

Attributes inherited from Plugin::Plugin

#logger, #params, #state

Instance Method Summary collapse

Methods inherited from Base

#initialize, #tag

Methods included from Config::Mixin

#get_config, included, #init_config, #set_config, #source

Methods inherited from Plugin::Plugin

#finished, #initialize, #shutdown

Constructor Details

This class inherits a constructor from Wms::Input::Base

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



9
10
11
# File 'lib/wms/input/filetype1.rb', line 9

def filepath
  @filepath
end

Instance Method Details

#register(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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