Class: UPowerDataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/battery/upower_data_source.rb

Defined Under Namespace

Classes: Boolean, UPowerCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UPowerDataSource

Returns a new instance of UPowerDataSource.



7
8
9
10
# File 'lib/battery/upower_data_source.rb', line 7

def initialize( options = {} )
  self.upower_command = options[:upower_command] || UPowerCommand.new
  update
end

Instance Attribute Details

#upower_commandObject

Returns the value of attribute upower_command.



5
6
7
# File 'lib/battery/upower_data_source.rb', line 5

def upower_command
  @upower_command
end

Instance Method Details

#charging?Boolean

Returns:



44
45
46
# File 'lib/battery/upower_data_source.rb', line 44

def charging?
  fetch( 'state' ) == 'charging'
end

#design_max_energy_whObject



64
65
66
# File 'lib/battery/upower_data_source.rb', line 64

def design_max_energy_wh
  fetch( 'energy-full-design', :as => Float )
end

#discharging?Boolean

Returns:



48
49
50
# File 'lib/battery/upower_data_source.rb', line 48

def discharging?
  fetch( 'state' ) == 'discharging'
end

#energy_leftObject



56
57
58
# File 'lib/battery/upower_data_source.rb', line 56

def energy_left
  fetch( 'percentage', :as => Float ) / 100.0
end

#energy_left_whObject



60
61
62
# File 'lib/battery/upower_data_source.rb', line 60

def energy_left_wh
  fetch( 'energy', :as => Float )
end

#fully_charged?Boolean

Returns:



52
53
54
# File 'lib/battery/upower_data_source.rb', line 52

def fully_charged?
  fetch( 'state' ) == 'fully-charged'
end

#has_history?Boolean

Returns:



32
33
34
# File 'lib/battery/upower_data_source.rb', line 32

def has_history?
  fetch( 'has history', :as => Boolean )
end

#has_stats?Boolean

Returns:



36
37
38
# File 'lib/battery/upower_data_source.rb', line 36

def has_stats?
  fetch( 'has statistics', :as => Boolean )
end

#last_updateObject



28
29
30
# File 'lib/battery/upower_data_source.rb', line 28

def last_update
  fetch( 'updated', :as => DateTime )
end

#max_energy_whObject



68
69
70
# File 'lib/battery/upower_data_source.rb', line 68

def max_energy_wh
  fetch( 'energy-full', :as => Float )
end

#modelObject



16
17
18
# File 'lib/battery/upower_data_source.rb', line 16

def model
  fetch( 'model' )
end

#present?Boolean

Returns:



40
41
42
# File 'lib/battery/upower_data_source.rb', line 40

def present?
  fetch( 'present', :as => Boolean )
end

#serialObject



20
21
22
# File 'lib/battery/upower_data_source.rb', line 20

def serial
  fetch( 'serial' )
end

#technologyObject



24
25
26
# File 'lib/battery/upower_data_source.rb', line 24

def technology
  fetch( 'technology' )
end

#updateObject



76
77
78
79
80
81
82
83
# File 'lib/battery/upower_data_source.rb', line 76

def update
  output = upower_command.exec
  first_battery = output.scan( /Device:.*battery_.*\n((.|\n)+)\n(Daemon:|Device:)/ )
  first_battery = first_battery.first.first rescue ''
  first_battery_rows = first_battery.strip.split( /$/ ).map( &:strip ).reject{ |r| !r.include?( ':' ) }
  first_battery_key_value_array = first_battery_rows.map{ |r| r.split( ':', 2 ) }.flatten.map( &:strip )
  @values = Hash[*first_battery_key_value_array]
end

#vendorObject



12
13
14
# File 'lib/battery/upower_data_source.rb', line 12

def vendor
  fetch( 'vendor' )
end

#voltageObject



72
73
74
# File 'lib/battery/upower_data_source.rb', line 72

def voltage
  fetch( 'voltage', :as => Float )
end