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



134
135
136
137
138
# File 'lib/iStats/battery.rb', line 134

def battery_charge
  charge = get_battery_charge
  result = charge ? charge : "Unknown"
  Printer.print_item_line("Battery charge", result, "%")
end

.battery_healthObject

Get the battery health Calls a C method from BATTERY_STATS module



117
118
119
# File 'lib/iStats/battery.rb', line 117

def battery_health
  Printer.print_item_line("Battery health", get_battery_health)
end

.battery_temperatureObject

Get the battery temperature



109
110
111
112
# File 'lib/iStats/battery.rb', line 109

def battery_temperature
  value, scale = Printer.parse_temperature(get_battery_temp)
  Printer.print_item_line("Battery temp", value, scale)
end

.battery_time_remainingObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/iStats/battery.rb', line 121

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

  Printer.print_item_line("Battery time remaining", time)
end

.cur_capacityObject

Current capacity



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

def cur_capacity
  grep_ioreg("CurrentCapacity")
end

.cur_max_capacityObject

Current max capacity



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

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
61
# File 'lib/iStats/battery.rb', line 48

def cycle_count
  @ioreg_out ||= %x( ioreg -rn AppleSmartBattery )
  cycle_count = @ioreg_out[/"CycleCount" = ([0-9]*)/, 1]
  if cycle_count == nil
    Printer.print_item_line("Cycle count", "unknown")
  else
    max_cycle_count = design_cycle_count
    percentage = (cycle_count.to_f/max_cycle_count.to_f)*100
    thresholds = Utils.abs_thresholds([0.45, 0.65, 0.85, 0.95], max_cycle_count)

    Printer.print_item_line("Cycle count", cycle_count, "", thresholds, "#{percentage.round(1)}%")
    Printer.print_item_line("Max cycles", 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



143
144
145
# File 'lib/iStats/battery.rb', line 143

def design_cycle_count
  get_battery_design_cycle_count
end

.grep_ioreg(keyword) ⇒ Object

Get information from ioreg



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

def grep_ioreg(keyword)
  @ioreg_out ||= %x( ioreg -rn AppleSmartBattery )
  capacity = @ioreg_out[/"#{keyword}" = ([0-9]*)/, 1]
end

.ori_max_capacityObject

Original max capacity



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

def ori_max_capacity
  grep_ioreg("DesignCapacity")
end

Print battery capacity info



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/iStats/battery.rb', line 90

def print_capacity_info
  cur_max = cur_max_capacity.to_f
  ori_max = ori_max_capacity.to_f

  percentage = (cur_max / ori_max)*100

  per_thresholds = [0.95, 0.85, 0.65, 0.45]
  cur_thresholds = Utils.abs_thresholds(per_thresholds, cur_max)
  ori_thresholds = Utils.abs_thresholds(per_thresholds, ori_max)

  charge = get_battery_charge
  result = charge ? "#{charge}%" : "Unknown"
  Printer.print_item_line("Current charge", cur_capacity, " mAh", cur_thresholds, "#{result}")
  Printer.print_item_line("Maximum charge", cur_max_capacity, " mAh", ori_thresholds, "#{percentage.round(1)}%")
  Printer.print_item_line("Design capacity", ori_max_capacity, " mAh")
end

.validate_batteryObject

Check if there’s a battery on the system



149
150
151
152
153
# File 'lib/iStats/battery.rb', line 149

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