Module: LinuxStat::Battery
- Defined in:
- lib/linux_stat/battery.rb
Constant Summary collapse
- PATH =
"/sys/class/power_supply/BAT0"
Class Method Summary collapse
-
.charge ⇒ Object
Returns the charge of the battery.
-
.charging? ⇒ Boolean
Returns true if the battery is charging, false if the battery is not charging.
-
.discharging? ⇒ Boolean
Returns true if the battery is discharging, false if the battery is not discharging.
-
.full? ⇒ Boolean
Returns true if the battery status if full, false if the battery status is not full.
-
.manufacturer ⇒ Object
Returns the manufacturer of the battery.
-
.model ⇒ Object
Returns the model of the battery.
-
.present? ⇒ Boolean
Returns true or false based on the presence of the battery.
-
.stat ⇒ Object
Returns the details of the battery.
-
.status ⇒ Object
Returns the status of the battery.
-
.technology ⇒ Object
Returns the technology of the battery.
Class Method Details
.charge ⇒ Object
Returns the charge of the battery. If the battery is not present or the information is not available, it will return nil.
81 82 83 84 85 86 87 88 |
# File 'lib/linux_stat/battery.rb', line 81 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.
60 61 62 63 |
# File 'lib/linux_stat/battery.rb', line 60 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.
67 68 69 70 |
# File 'lib/linux_stat/battery.rb', line 67 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.
74 75 76 77 |
# File 'lib/linux_stat/battery.rb', line 74 def full? return nil if status.empty? status.downcase == 'full' end |
.manufacturer ⇒ Object
Returns the manufacturer of the battery. If the battery is not present or the information is not available, it will return an empty String.
38 39 40 41 |
# File 'lib/linux_stat/battery.rb', line 38 def manufacturer return ''.freeze unless manufacturer_readable? IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!) end |
.model ⇒ Object
Returns the model of the battery. If the battery is not present or the information isn’t available it will return an empty String.
31 32 33 34 |
# File 'lib/linux_stat/battery.rb', line 31 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.
7 8 9 |
# File 'lib/linux_stat/battery.rb', line 7 def present? @@present ||= Dir.exist?(PATH) end |
.stat ⇒ Object
Returns the details of the battery. If the battery is not present it will return an empty Hash.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/linux_stat/battery.rb', line 13 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 |
.status ⇒ Object
Returns the status of the battery. If the battery is not present or the information is not available, it will return an empty String. The status generally includes either of the full, charging, discharging and unknown states in most cases.
53 54 55 56 |
# File 'lib/linux_stat/battery.rb', line 53 def status return ''.freeze unless status_readable? IO.read(File.join(PATH, 'status')).tap(&:strip!) end |
.technology ⇒ Object
Returns the technology 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 technology return ''.freeze unless tech_readable? IO.read(File.join(PATH, 'technology')).tap(&:strip!) end |