mirror of
https://github.com/lloeki/toolbelt.git
synced 2025-12-06 01:54:41 +01:00
battery cruft cleanup
This commit is contained in:
parent
e7833c745a
commit
f18becd77b
2 changed files with 63 additions and 168 deletions
|
|
@ -5,36 +5,43 @@ from __future__ import division
|
||||||
|
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
|
|
||||||
def ioreg_battery_info():
|
def ioreg_battery_info():
|
||||||
output = Popen(["ioreg", "-r", "-k", "LegacyBatteryInfo", "-w", "0"], stdout=PIPE).communicate()[0]
|
output = Popen(["ioreg", "-r", "-k", "LegacyBatteryInfo", "-w", "0"],
|
||||||
try: #python3
|
stdout=PIPE).communicate()[0]
|
||||||
|
try: # python3
|
||||||
return str(output, encoding='utf-8')
|
return str(output, encoding='utf-8')
|
||||||
except TypeError: #python2
|
except TypeError: # python2
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def parse_ioreg_dict(output):
|
def parse_ioreg_dict(output):
|
||||||
return dict(
|
return dict(
|
||||||
[ (kw.strip().strip('"'), vw.strip())
|
[(kw.strip().strip('"'), vw.strip())
|
||||||
for kw, vw
|
for kw, vw
|
||||||
in [ line.split("=", 1)
|
in [line.split("=", 1)
|
||||||
for line
|
for line
|
||||||
in output.split('\n') if line.find('=')>0
|
in output.split('\n') if line.find('=') > 0
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def is_two_complement_negative(value):
|
def is_two_complement_negative(value):
|
||||||
return value > (2**63-1)
|
return value > (2**63-1)
|
||||||
|
|
||||||
|
|
||||||
def two_complement(value):
|
def two_complement(value):
|
||||||
return 2**64 - value
|
return 2**64 - value
|
||||||
|
|
||||||
|
|
||||||
def fix_negative(value):
|
def fix_negative(value):
|
||||||
if is_two_complement_negative(value):
|
if is_two_complement_negative(value):
|
||||||
return -two_complement(value)
|
return -two_complement(value)
|
||||||
else:
|
else:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def ioreg_battery_dict():
|
def ioreg_battery_dict():
|
||||||
output = ioreg_battery_info()
|
output = ioreg_battery_info()
|
||||||
return parse_ioreg_dict(output)
|
return parse_ioreg_dict(output)
|
||||||
|
|
@ -43,25 +50,28 @@ def ioreg_battery_dict():
|
||||||
def fix_integer(string):
|
def fix_integer(string):
|
||||||
return fix_negative(int(string))
|
return fix_negative(int(string))
|
||||||
|
|
||||||
|
|
||||||
def format_time(string):
|
def format_time(string):
|
||||||
minutes = int(string)
|
minutes = int(string)
|
||||||
if minutes == 65535:
|
if minutes == 65535:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return "%s:%s" % (minutes//60, minutes%60)
|
return "%s:%s" % (minutes // 60, minutes % 60)
|
||||||
|
|
||||||
|
|
||||||
def percentage(ratio):
|
def percentage(ratio):
|
||||||
return "%s%%" % (int(ratio*100))
|
return "%s%%" % (int(ratio * 100))
|
||||||
|
|
||||||
humanize_index = {
|
humanize_index = {
|
||||||
"TimeRemaining": format_time,
|
"TimeRemaining": format_time,
|
||||||
"AvgTimeToEmpty": format_time,
|
"AvgTimeToEmpty": format_time,
|
||||||
"AvgTimeToFull": format_time,
|
"AvgTimeToFull": format_time,
|
||||||
"InstantTimeToEmpty": format_time,
|
"InstantTimeToEmpty": format_time,
|
||||||
"FullToEmptyTime": format_time,
|
"FullToEmptyTime": format_time,
|
||||||
"WearRatio": percentage,
|
"WearRatio": percentage,
|
||||||
"ChargeRatio": percentage,
|
"ChargeRatio": percentage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def humanize_data(k, v):
|
def humanize_data(k, v):
|
||||||
if k in humanize_index:
|
if k in humanize_index:
|
||||||
|
|
@ -69,12 +79,15 @@ def humanize_data(k, v):
|
||||||
else:
|
else:
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
def wear_ratio(info):
|
def wear_ratio(info):
|
||||||
return int(info["MaxCapacity"]) / int(info["DesignCapacity"])
|
return int(info["MaxCapacity"]) / int(info["DesignCapacity"])
|
||||||
|
|
||||||
|
|
||||||
def charge_ratio(info):
|
def charge_ratio(info):
|
||||||
return int(info["CurrentCapacity"]) / int(info["MaxCapacity"])
|
return int(info["CurrentCapacity"]) / int(info["MaxCapacity"])
|
||||||
|
|
||||||
|
|
||||||
def full_to_empty_time(info):
|
def full_to_empty_time(info):
|
||||||
if get_data(info, "Amperage") < 0:
|
if get_data(info, "Amperage") < 0:
|
||||||
return -int(info["MaxCapacity"])*60 / get_data(info, "Amperage")
|
return -int(info["MaxCapacity"])*60 / get_data(info, "Amperage")
|
||||||
|
|
@ -82,17 +95,19 @@ def full_to_empty_time(info):
|
||||||
return 65535
|
return 65535
|
||||||
|
|
||||||
synthetize_index = {
|
synthetize_index = {
|
||||||
"Amperage": lambda i: fix_integer(i["Amperage"]),
|
"Amperage": lambda i: fix_integer(i["Amperage"]),
|
||||||
"InstantAmperage": lambda i: fix_integer(i["InstantAmperage"]),
|
"InstantAmperage": lambda i: fix_integer(i["InstantAmperage"]),
|
||||||
"WearRatio": wear_ratio,
|
"WearRatio": wear_ratio,
|
||||||
"ChargeRatio": charge_ratio,
|
"ChargeRatio": charge_ratio,
|
||||||
"FullToEmptyTime": full_to_empty_time,
|
"FullToEmptyTime": full_to_empty_time,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def synthetize_data(battery_info, k):
|
def synthetize_data(battery_info, k):
|
||||||
if k in synthetize_index:
|
if k in synthetize_index:
|
||||||
return synthetize_index[k](battery_info)
|
return synthetize_index[k](battery_info)
|
||||||
|
|
||||||
|
|
||||||
def get_data(battery_info, k):
|
def get_data(battery_info, k):
|
||||||
if k in synthetize_index:
|
if k in synthetize_index:
|
||||||
return synthetize_data(battery_info, k)
|
return synthetize_data(battery_info, k)
|
||||||
|
|
@ -101,31 +116,33 @@ def get_data(battery_info, k):
|
||||||
else:
|
else:
|
||||||
raise KeyError("%s" % k)
|
raise KeyError("%s" % k)
|
||||||
|
|
||||||
|
|
||||||
keys_to_show = [
|
keys_to_show = [
|
||||||
"Temperature",
|
"Temperature",
|
||||||
"CycleCount",
|
"CycleCount",
|
||||||
#"DesignCycleCount9C",
|
#"DesignCycleCount9C",
|
||||||
"DesignCapacity",
|
"DesignCapacity",
|
||||||
"MaxCapacity",
|
"MaxCapacity",
|
||||||
"WearRatio",
|
"WearRatio",
|
||||||
"CurrentCapacity",
|
"CurrentCapacity",
|
||||||
"ChargeRatio",
|
"ChargeRatio",
|
||||||
"Voltage",
|
"Voltage",
|
||||||
"Amperage",
|
"Amperage",
|
||||||
"InstantAmperage",
|
"InstantAmperage",
|
||||||
"InstantTimeToEmpty",
|
"InstantTimeToEmpty",
|
||||||
"TimeRemaining",
|
"TimeRemaining",
|
||||||
"AvgTimeToEmpty",
|
"AvgTimeToEmpty",
|
||||||
"AvgTimeToFull",
|
"AvgTimeToFull",
|
||||||
"FullToEmptyTime",
|
"FullToEmptyTime",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def print_key_value(k, v):
|
def print_key_value(k, v):
|
||||||
if v is not None:
|
if v is not None:
|
||||||
print("%s = %s" % (k, v))
|
print("%s = %s" % (k, v))
|
||||||
|
|
||||||
battery_info = ioreg_battery_dict()
|
|
||||||
|
battery_info = ioreg_battery_dict()
|
||||||
|
|
||||||
for k in keys_to_show:
|
for k in keys_to_show:
|
||||||
print_key_value(k, humanize_data(k, get_data(battery_info, k)))
|
print_key_value(k, humanize_data(k, get_data(battery_info, k)))
|
||||||
|
|
||||||
122
battery.sh
122
battery.sh
|
|
@ -1,122 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
legacyinfo=$(ioreg -l -w 2048 | grep 'LegacyBatteryInfo')
|
|
||||||
|
|
||||||
get_attribute()
|
|
||||||
{
|
|
||||||
echo $legacyinfo | sed "s/.*\"$@\"=\([^,}][^,}]*\)[,}].*/\1/"
|
|
||||||
}
|
|
||||||
|
|
||||||
is_negative()
|
|
||||||
{
|
|
||||||
[ $(echo "$@ > (2^63-1)" | bc) == "1" ]
|
|
||||||
}
|
|
||||||
|
|
||||||
two_complement()
|
|
||||||
{
|
|
||||||
echo "2^64 - $@" | bc
|
|
||||||
}
|
|
||||||
|
|
||||||
fix_negative()
|
|
||||||
{
|
|
||||||
# is the value negative?
|
|
||||||
if is_negative "$@"; then
|
|
||||||
# compute two's complement
|
|
||||||
echo "-$(two_complement "$@")"
|
|
||||||
else
|
|
||||||
echo "$@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
get_integer_attribute()
|
|
||||||
{
|
|
||||||
fix_negative "$(get_attribute "$@")"
|
|
||||||
}
|
|
||||||
|
|
||||||
amperage()
|
|
||||||
{
|
|
||||||
get_integer_attribute 'Amperage'
|
|
||||||
}
|
|
||||||
|
|
||||||
full_capacity()
|
|
||||||
{
|
|
||||||
get_integer_attribute 'Capacity'
|
|
||||||
}
|
|
||||||
|
|
||||||
current_capacity()
|
|
||||||
{
|
|
||||||
get_integer_attribute 'Current'
|
|
||||||
}
|
|
||||||
|
|
||||||
voltage()
|
|
||||||
{
|
|
||||||
get_integer_attribute 'Voltage'
|
|
||||||
}
|
|
||||||
|
|
||||||
cycle_count()
|
|
||||||
{
|
|
||||||
get_integer_attribute 'Cycle Count'
|
|
||||||
}
|
|
||||||
|
|
||||||
discharge_time()
|
|
||||||
{
|
|
||||||
hours=$(echo "-$(current_capacity) / $(amperage)" | bc)
|
|
||||||
minutes=$(echo "-( $(current_capacity) % $(amperage) ) * 60 / $(amperage)" | bc)
|
|
||||||
echo "$hours:$minutes"
|
|
||||||
}
|
|
||||||
|
|
||||||
full_discharge_time()
|
|
||||||
{
|
|
||||||
hours=$(echo "-$(full_capacity) / $(amperage)" | bc)
|
|
||||||
minutes=$(echo "-( $(full_capacity) % $(amperage) ) * 60 / $(amperage)" | bc)
|
|
||||||
echo "$hours:$minutes"
|
|
||||||
}
|
|
||||||
|
|
||||||
charge_time()
|
|
||||||
{
|
|
||||||
hours=$(echo "( $(full_capacity) - $(current_capacity) ) / $(amperage)" | bc)
|
|
||||||
minutes=$(echo "( ( $(full_capacity) - $(current_capacity) ) % $(amperage) ) * 60 / $(amperage)" | bc)
|
|
||||||
echo "$hours:$minutes"
|
|
||||||
}
|
|
||||||
|
|
||||||
full_charge_time()
|
|
||||||
{
|
|
||||||
hours=$(echo "$(full_capacity) / $(amperage)" | bc)
|
|
||||||
minutes=$(echo "( $(full_capacity) % $(amperage) ) * 60 / $(amperage)" | bc)
|
|
||||||
echo "$hours:$minutes"
|
|
||||||
}
|
|
||||||
|
|
||||||
remaining_time()
|
|
||||||
{
|
|
||||||
if [ $(echo "$(amperage) < 0" | bc) == "1" ]; then
|
|
||||||
discharge_time
|
|
||||||
elif [ $(echo "$(amperage) > 0" | bc) == "1" ]; then
|
|
||||||
charge_time
|
|
||||||
else
|
|
||||||
echo "0:00"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
full_time()
|
|
||||||
{
|
|
||||||
if [ $(echo "$(amperage) < 0" | bc) == "1" ]; then
|
|
||||||
full_discharge_time
|
|
||||||
elif [ $(echo "$(amperage) > 0" | bc) == "1" ]; then
|
|
||||||
full_charge_time
|
|
||||||
else
|
|
||||||
echo "0:00"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
charge_ratio()
|
|
||||||
{
|
|
||||||
echo "$(echo "( $(current_capacity) * 100 / $(full_capacity) )" | bc)%"
|
|
||||||
}
|
|
||||||
|
|
||||||
attributes=('amperage' 'full_capacity' 'current_capacity' 'voltage' 'cycle_count' 'remaining_time' 'charge_ratio' 'full_time')
|
|
||||||
|
|
||||||
for ((i=0;i<${#attributes[*]};i++)); do
|
|
||||||
data=$(${attributes[$i]})
|
|
||||||
echo "${attributes[$i]}: $data"
|
|
||||||
done
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue