Class: IStats::Battery

Inherits:
Object
  • Object
show all
Extended by:
BATTERY_STATS
Defined in:
lib/iStats/battery.rb

Class Method Summary collapse

Methods included from BATTERY_STATS

get_battery_charge, get_battery_design_cycle_count, get_battery_health, get_battery_temp, get_battery_time_remaining, has_battery?

Class Method Details

.allObject

Call all functions (stats)



37
38
39
40
41
42
43
44
# File 'lib/iStats/battery.rb', line 37

def all
  return unless validate_battery

  battery_health
  cycle_count
  print_capacity_info
  battery_temperature
end

.battery_chargeObject



123
124
125
126
127
# File 'lib/iStats/battery.rb', line 123

def battery_charge
  charge = get_battery_charge
  result = charge ? "#{charge}%" : "Unknown"
  puts "Battery charge: #{result}"
end

.battery_healthObject

Get the battery health Calls a C method from BATTERY_STATS module



106
107
108
# File 'lib/iStats/battery.rb', line 106

def battery_health
  puts "Battery health: #{get_battery_health}"
end

.battery_temperatureObject

Get the battery temperature



99
100
101
# File 'lib/iStats/battery.rb', line 99

def battery_temperature
  puts "Battery temp: #{get_battery_temp.round(2)}#{Symbols.degree}C  "
end

.battery_time_remainingObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/iStats/battery.rb', line 110

def battery_time_remaining
  time = get_battery_time_remaining

  if time.is_a? Integer
    hours   = time / 3600
    minutes = time / 60 - hours * 60

    time = "%i:%02i" % [hours, minutes]
  end

  puts "Battery time remaining: #{time}"
end

.cur_capacityObject

Current capacity



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

def cur_capacity
  grep_ioreg("CurrentCapacity")
end

.cur_max_capacityObject

Current max capacity



77
78
79
# File 'lib/iStats/battery.rb', line 77

def cur_max_capacity
  grep_ioreg("MaxCapacity")
end

.cycle_countObject

Prints the battery cycle count info



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iStats/battery.rb', line 48

def cycle_count
  data = %x( ioreg -l | grep "CycleCount" )
  cycle_count = data[/"CycleCount" = ([0-9]*)/, 1]
  if cycle_count == nil
    puts "Cycle count: unknown"
  else
    max_cycle_count = design_cycle_count
    percentage = (cycle_count.to_f/max_cycle_count.to_f)*100
    thresholds = [45, 65, 85, 95]
    puts "Cycle count: #{cycle_count}  " + Printer.gen_sparkline(percentage, thresholds) + "  #{percentage.round(1)}%"
    puts "Max cycle count: #{max_cycle_count}"
  end
end

.delegate(stat) ⇒ Object

Delegate CLI command to function



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iStats/battery.rb', line 11

def delegate(stat)
  # Before we deletage check if there's a battery on system
  return unless validate_battery

  case stat
  when 'all'
    all
  when 'temp', 'temperature'
    battery_temperature
  when 'health'
    battery_health
  when 'cycle_count', 'cc'
    cycle_count
  when 'time', 'remain'
    battery_time_remaining
  when 'charge'
    battery_charge
  when 'capacity'
    print_capacity_info
  else
    Command.help "Unknown stat for Battery: #{stat}"
  end
end

.design_cycle_countObject

Get the battery design cycle count Calls a C method from BATTERY_STATS module



132
133
134
# File 'lib/iStats/battery.rb', line 132

def design_cycle_count
  get_battery_design_cycle_count
end

.grep_ioreg(keyword) ⇒ Object

Get information from ioreg



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

def grep_ioreg(keyword)
  data = %x( ioreg -l | grep "#{keyword}" )
  capacity = data[/"#{keyword}" = ([0-9]*)/, 1]
end

.ori_max_capacityObject

Original max capacity



71
72
73
# File 'lib/iStats/battery.rb', line 71

def ori_max_capacity
  grep_ioreg("DesignCapacity")
end

Print battery capacity info



89
90
91
92
93
94
95
# File 'lib/iStats/battery.rb', line 89

def print_capacity_info
  percentage = (cur_max_capacity.to_f/ori_max_capacity.to_f)*100
  thresholds = [45, 65, 85, 95]
  puts "Current charge:  #{cur_capacity} mAh"
  puts "Maximum charge:  #{cur_max_capacity} mAh " + Printer.gen_sparkline(100-percentage, thresholds) + "  #{percentage.round(1)}%"
  puts "Design capacity: #{ori_max_capacity} mAh"
end

.validate_batteryObject

Check if there’s a battery on the system



138
139
140
141
142
# File 'lib/iStats/battery.rb', line 138

def validate_battery
  valid = has_battery?
  puts 'No battery on system' unless valid
  valid
end