Class: Wms::Input::AndroidSensor
- Inherits:
-
Base
- Object
- Plugin::Plugin
- Base
- Wms::Input::AndroidSensor
- Defined in:
- lib/wms/input/android_sensor.rb
Instance Attribute Summary collapse
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#headers ⇒ Object
Returns the value of attribute headers.
Attributes inherited from Base
Attributes included from Config::Mixin
Attributes inherited from Plugin::Plugin
Instance Method Summary collapse
Methods inherited from Base
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
#filepath ⇒ Object
Returns the value of attribute filepath.
29 30 31 |
# File 'lib/wms/input/android_sensor.rb', line 29 def filepath @filepath end |
#headers ⇒ Object
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(={}) raise "#{self.class.name}: filepath required in options" unless [:filepath] @filepath = [:filepath] @headers = self.class.get_default(:headers) @converters = self.class.get_default(:converters) @compressed = [:compressed] @file_ext = [:file_ext] @is_gz = [: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 |