Class: Wms::Input::AndroidSensor

Inherits:
Base show all
Defined in:
lib/wms/input/android_sensor.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.



29
30
31
# File 'lib/wms/input/android_sensor.rb', line 29

def filepath
  @filepath
end

#headersObject

Returns the value of attribute headers.



29
30
31
# File 'lib/wms/input/android_sensor.rb', line 29

def headers
  @headers
end

Instance Method Details

#register(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/wms/input/android_sensor.rb', line 32

def register(options={})
  raise "#{self.class.name}: filepath required in options" unless options[:filepath]
  @filepath   = options[:filepath]
  @headers    = self.class.get_default(:headers)
  @converters = self.class.get_default(:converters)
  @compressed = options[:compressed]
  @file_ext   = options[:file_ext]
  @is_gz      = options[:file_ext] == '.gz'
end

#run(&block) ⇒ Object



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
# File 'lib/wms/input/android_sensor.rb', line 45

def run(&block)
  # adding options to make data manipulation easy
  total_lines = 0
  if @is_gz
    Zlib::GzipReader.open(@filepath) do |csv|
      CSV.parse( csv.read, { :headers => @headers, 
                                :converters => @converters
                                }) do |line|

        # @logger.debug line.to_hash

        # Covert timestamp field from epoc time to Time object.
        # Time object also include microsecond as the second arg.
        data = line.to_hash

        # Skip the first row
        if total_lines != 0
          begin
            data[:timestamp] = Time.at(data[:timestamp] / 1000, data[:timestamp] % 1000)
            data[:type] = 'sensor'

            # Call the callback function with the hash as params
            callback = block
            callback.call(data)
          rescue Exception => e
            @logger.error "ERROR #{self.class.name} while parsing #{e}"
          end
          
        end

        total_lines += 1
      end

    end
  else
    CSV.foreach( @filepath, { :headers => @headers, 
                              :converters => @converters
                              }) do |line|

      # @logger.debug line.to_hash

      # Covert timestamp field from epoc time to Time object.
      # Time object also include microsecond as the second arg.
      data = line.to_hash

      # Skip the first row
      if total_lines != 0
        begin
          data[:timestamp] = Time.at(data[:timestamp] / 1000, data[:timestamp] % 1000)
          data[:type] = 'sensor'

          # Call the callback function with the hash as params
          callback = block
          callback.call(data)
        rescue Exception => e
          @logger.error "ERROR #{self.class.name} while parsing #{e}"
        end
        
      end

      total_lines += 1
    end
  end
  @logger.debug "Total line: %d" % total_lines
end