Module: LinuxStat::Battery

Defined in:
lib/linux_stat/battery.rb

Constant Summary collapse

PATH =
"/sys/class/power_supply/BAT0"

Class Method Summary collapse

Class Method Details

.chargeObject

Returns the charge of the battery.

If the battery is not present or the information is not available, it will return nil.



100
101
102
103
104
105
106
107
# File 'lib/linux_stat/battery.rb', line 100

def charge
  return nil unless charge_now_readable?
  charge_now = IO.read(File.join(PATH, 'charge_now')).to_i
  charge_full = IO.read(File.join(PATH, 'charge_full')).to_i

  percentage = charge_now.*(100).fdiv(charge_full)
  percentage = percentage > 100 ? 100.0 : percentage < 0 ? 0.0 : percentage
end

.charging?Boolean

Returns true if the battery is charging, false if the battery is not charging.

If the battery is not present or the information is not available, it will return nil.

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/linux_stat/battery.rb', line 73

def charging?
  return nil if status.empty?
  %w(full charging unknown).each(&:freeze).include?(status.downcase)
end

.discharging?Boolean

Returns true if the battery is discharging, false if the battery is not discharging.

If the battery is not present or the information is not available, it will return nil.

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/linux_stat/battery.rb', line 82

def discharging?
  return nil if status.empty?
  status.downcase == 'discharging'
end

.full?Boolean

Returns true if the battery status if full, false if the battery status is not full.

If the battery is not present or the information is not available, it will return nil.

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/linux_stat/battery.rb', line 91

def full?
  return nil if status.empty?
  status.downcase == 'full'
end

.manufacturerObject

Returns the manufacturer of the battery.

If the battery is not present or the information is not available, it will return an empty String.



45
46
47
48
# File 'lib/linux_stat/battery.rb', line 45

def manufacturer
  return ''.freeze unless manufacturer_readable?
  IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
end

.modelObject

Returns the model of the battery.

If the battery is not present or the information isn’t available it will return an empty String.



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

def model
  return ''.freeze unless model_readable?
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
end

.present?Boolean

Returns true or false based on the presence of the battery.

Returns:

  • (Boolean)


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

def present?
  @@present ||= Dir.exist?(PATH)
end

.statObject

Returns the details of the battery.

If the battery is not present it will return an empty Hash.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/linux_stat/battery.rb', line 16

def stat
  st = status.downcase
  return {} unless present?

  {
    model: model,
    manufacturer: manufacturer,
    technology: technology,
    status: status,
    charge: charge,
    charging: %w(full charging unknown).each(&:freeze).include?(st),
    discharging: st == 'discharging'.freeze,
    full: st == 'full'.freeze,
  }
end

.statusObject

Returns the status of the battery. The status generally includes either of the full, charging, discharging and unknown states in most cases.

If the battery is not present or the information is not available, it will return an empty frozen String.



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

def status
  return ''.freeze unless status_readable?
  IO.read(File.join(PATH, 'status')).tap(&:strip!)
end

.technologyObject

Returns the technology of the battery.

If the battery is not present or the information is not available, it will return an empty String.



54
55
56
57
# File 'lib/linux_stat/battery.rb', line 54

def technology
  return ''.freeze unless tech_readable?
  IO.read(File.join(PATH, 'technology')).tap(&:strip!)
end