Codebase list battery-stats / 54fc449e-addd-4955-979e-d7491bc60682/main src / battery-stats-collector
54fc449e-addd-4955-979e-d7491bc60682/main

Tree @54fc449e-addd-4955-979e-d7491bc60682/main (Download .tar.gz)

battery-stats-collector @54fc449e-addd-4955-979e-d7491bc60682/mainraw · history · blame

#!/bin/sh
#
# This file is part of the battery-stats package.
# Copyright (C) 2016 Petter Reinholdtsen <pere@hungry.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software

set -e

get_logline() {
    secstamp=$(date +%s)
    stamp=$(date +"%Y/%m/%d %H:%M:%S")

    aconline=notfound
    for ac in AC AC0 ACAD ADP0 ADP1; do
        if [ -f /sys/class/power_supply/$ac/online ]; then
            aconline=$(cat /sys/class/power_supply/$ac/online)
            break
        fi
    done
    if [ notfound = "$aconline" ]; then
        echo "No power supply found"
    fi

    if [ 1 = "$aconline" ]; then
        state=2
    else
        state=1
    fi

    now="energy_now"
    full="energy_full"
    for f in /sys/class/power_supply/BAT*; do
        [ -e "$f" ] || continue
        if [ ! -e $f/$now ] ; then now="charge_now"; fi
        if [ ! -e $f/$full ] ; then full="charge_full"; fi
        energy_now=$(cat $f/$now) # uWh
        energy_full=$(cat $f/$full) # uWh
        percent=$( (echo scale=2; echo "100 * $energy_now / $energy_full") | bc -l)

        # FIXME figure out how to calculate minutes remaining the same
        # way libacpi calculate it.
        # Ideas:
        if false; then
            power_now=$(cat $f/power_now) # uW
            voltage_now=$(cat $f/voltage_now) # uV
            rate=$(( 1000000 * $power_now / $voltage_now))
            if [ 0 = $rate ] ; then
            minremaining=0
            else
            minremaining=$(( 60 * $energy_now / $rate ))
            fi

                # 10^6 * uW / uV  = uA
            remaining_capasity=$((1000000 * $energy_now / $voltage_now ))
                # 10^6 * uA / uW = 1 / mV
            minremaining=$((1000000 * $remaining_capasity / $power_now))
        fi
        break
    done

    # Use '-' for minutes remaining, as this value isn't used by the
    # graph drawing system.
    echo $secstamp $percent $state $stamp -
}

usage() {
    cat <<EOF
Usage: $0 [-o logfile]
EOF
}

PROGRAM_NAME=$(basename $0)
logfile="/var/log/battery-stats"
sample_interval_secs=$((10 * 60)) # 10 minutes
syslog=false

TEMP=$(getopt -n $PROGRAM_NAME --options o:i:V1hsF:Ib: \
    --longoptions output:,interval:,version,once,help,syslog,\
flush:,ignore-missing-battery,battery-num: \
    -- "$@")
eval set -- "$TEMP"
while true; do
    case "$1" in
        -o|--output)
            logfile="$2"; shift 2; continue
            ;;
        -i|--interval)
            sample_interval_secs="$2"; shift 2; continue
            ;;
        -V|--version)
            echo "Version: someversion"
            exit 1
            ;;
        -1|--once)
            sample_interval_secs=0; shift; continue ;;
        -h|--help)
            usage; shift; exit 0 ;;
        -s|--syslog)
            # FIXME Ignored
            syslog=true; shift; continue ;; 
        -F|--flush)
            # FIXME Ignored
            shift 2; continue ;;
        -I|--ignore-missing-battery)
            # FIXME Ignored
            shift ; continue ;;
        -b|--battery-num)
            # FIXME Ignored
            shift 2; continue ;;
        --) # no more arguments to parse
            break
            ;;
        *)
            printf "Unknown option %s\n" "$1"
            exit 1
            ;;
    esac
done

while true; do
    if [ "$logfile" ] ; then
        get_logline >> $logfile
    else
        get_logline
    fi

    # Do a filesystem sync to increase the chance of the log line
    # actually making it to disk.
    sync

    if [ 0 -lt $sample_interval_secs ] ; then
        sleep $sample_interval_secs
    else
        break
    fi
done