Class: Battman::AcpiBattery

Inherits:
Battery
  • Object
show all
Defined in:
lib/battman/acpi_battery.rb

Constant Summary

Constants inherited from Battery

Battery::CONVERSIONS

Instance Method Summary collapse

Methods inherited from Battery

#full_energy_in, #power_in, #remaining_charging_time_in, #remaining_energy_in, #remaining_running_time_in

Constructor Details

#initialize(index = 0, **opts) ⇒ AcpiBattery

Returns a new instance of AcpiBattery.



6
7
8
9
10
# File 'lib/battman/acpi_battery.rb', line 6

def initialize(index = 0, **opts)
  super(index)

  @precision = opts[:precision] || 1000
end

Instance Method Details

#full_energyObject



53
54
55
56
57
# File 'lib/battman/acpi_battery.rb', line 53

def full_energy
  energy_file = File.join(path, 'energy_full')

  File.read(energy_file).to_f / (1000 * @precision)
end

#pathObject



12
13
14
# File 'lib/battman/acpi_battery.rb', line 12

def path
  @path ||= "/sys/class/power_supply/BAT#{@index}"
end

#powerObject



26
27
28
29
30
31
32
# File 'lib/battman/acpi_battery.rb', line 26

def power
  power_now_file = File.join(path, 'power_now')

  power = File.read(power_now_file).to_f / (1000 * @precision)

  state == :discharging ? -1 * power : power
end

#remaining_charging_timeObject

Raises:



59
60
61
62
# File 'lib/battman/acpi_battery.rb', line 59

def remaining_charging_time
  raise WrongStateError if state != :charging
  ((full_energy - remaining_energy) / power) * 60
end

#remaining_energyObject



42
43
44
45
46
# File 'lib/battman/acpi_battery.rb', line 42

def remaining_energy
  energy_file = File.join(path, 'energy_now')

  File.read(energy_file).to_f / (1000 * @precision)
end

#remaining_percentObject



16
17
18
19
20
21
22
23
24
# File 'lib/battman/acpi_battery.rb', line 16

def remaining_percent
  energy_full_file = File.join(path, 'energy_full')
  energy_now_file = File.join(path, 'energy_now')

  energy_full = File.read(energy_full_file)
  energy_now = File.read(energy_now_file)

  (energy_now.to_f / energy_full.to_f) * 100
end

#remaining_running_timeObject

Raises:



48
49
50
51
# File 'lib/battman/acpi_battery.rb', line 48

def remaining_running_time
  raise WrongStateError if state != :discharging
  (remaining_energy / power) * 60
end

#stateObject



34
35
36
37
38
39
40
# File 'lib/battman/acpi_battery.rb', line 34

def state
  state_file = File.join(path, 'status')

  state = File.read(state_file).chomp.downcase.to_sym

  state == :unknown ? :idle : state
end