/usr/sbin/laptopbat2ups


#!/bin/sh

unset LANG LANGUAGE LC_ALL
LC_ALL=C
export LC_ALL

PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

iguessed=1000000

# On a Compaq NX9000 with a P4M at 2 GHz and
# on an EeePC 1000H we know the things somewhat better.
# With the Super Hybrid Engine deactivated we guess for
# the condition of watching movies
test `uname -n` = "smobi" && iguessed=2500000 || : # Compaq NX9000 with a P4M at 2 GHz
test `uname -n` = "srosa" && iguessed=1700000 || : # EeePC 1000H

convertandwrite()
{
  echo "${1}"                                                                 \
    | tr '=;' ' \012'                                                         \
    | awk 'BEGIN{cf=1;cn=1;jg=1;jn=1;ol=1;pr=1;st="";vn=1;}                   \
                {if(NF<2)next;                                                \
                 if($1=="POWER_SUPPLY_CHARGE_FULL"){cf=$2;next;}              \
                 if($1=="POWER_SUPPLY_CHARGE_FULL_DESIGN"){                   \
                   printf("battery.capacity: %.1f\n",$2*1.0e-6);next;}        \
                 if($1=="POWER_SUPPLY_CHARGE_NOW"){cn=$2;next;}               \
                 if($1=="POWER_SUPPLY_CURRENT_GUESSED"){jg=$2;next;}          \
                 if($1=="POWER_SUPPLY_CURRENT_NOW"){jn=$2;next;}              \
                 if($1=="POWER_SUPPLY_MANUFACTURER"){                         \
                   s=$2;for(i=3;i<=NF;i++)s=s " " $(i);                       \
                   printf("device.mfr: %s\n",s);                              \
                   printf("ups.mfr: %s\n",s);                                 \
                   next;}                                                     \
                 if($1=="POWER_SUPPLY_MODEL_NAME"){                           \
                   s=$2;for(i=3;i<=NF;i++)s=s " " $(i);                       \
                   printf("device.model: %s\n",s);                            \
                   printf("ups.model: %s\n",s);                               \
                   next;}                                                     \
                 if($1=="POWER_SUPPLY_ONLINE"){ol=$2;next;}                   \
                 if($1=="POWER_SUPPLY_PRESENT"){pr=$2;next;}                  \
                 if($1=="POWER_SUPPLY_STATUS"){                               \
                   if($2=="Charging"){                                        \
                     if(length(st)!=0)st=st " ";                              \
                     st=st "CHRG";                                            \
                     next;}                                                   \
                   if($2=="Discharging"){                                     \
                     if(length(st)!=0)st=st " ";                              \
                     st=st "DISCHRG";                                         \
                     next;}}                                                  \
                 if($1=="POWER_SUPPLY_TECHNOLOGY"){                           \
                   s=$2;for(i=3;i<=NF;i++)s=s " " $(i);                       \
                   printf("battery.type: %s\n",s);                            \
                   next;}                                                     \
                 if($1=="POWER_SUPPLY_VOLTAGE_MIN_DESIGN"){                   \
                   printf("battery.voltage.nominal: %.1f\n",$2*1.0e-6);next;} \
                 if($1=="POWER_SUPPLY_VOLTAGE_NOW"){vn=$2;next;}              \
                 next;}                                                       \
             END{rtlow=300;bclow=20;                                          \
                 if(ol!=0){                                                   \
                     if(length(st)!=0)st=" " st;                              \
                     st="OL" st;}                                             \
                   else{                                                      \
                     if(length(st)!=0)st=" " st;                              \
                     st="OB" st;}                                             \
                 if(pr==0)                                                    \
                     st="OFF";                                                \
                   else{                                                      \
                     vn=vn*1.0e-6;                                            \
                     printf("battery.voltage: %.1f\n",vn);                    \
                     if(ol!=0)jn=jg;                                          \
                     po=vn*jn*1.0e-6;                                         \
                     printf("ups.power: %.1f\n",po);                          \
                     printf("ups.realpower: %.1f\n",po);                      \
                     printf("ups.load: 100\n");                               \
                     if(cf!=0)bc=1.0e+2*cn/cf;else bc=100;                    \
                     printf("battery.charge: %d\n",bc);                       \
                     if(jn!=0)rt=3600*cn/jn;else rt=rtlow+1;                  \
                     printf("battery.runtime: %d\n",rt);                      \
                     if((rt<rtlow)||(bc<bclow)){                              \
                       if(length(st)!=0)st=st " ";                            \
                       st=st "LB";}}                                          \
                 printf("battery.charge.low: %d\n",bclow);                    \
                 printf("battery.runtime.low: %d\n",rtlow);                   \
                 printf("ups.status: %s\n",st);                               \
                 }'                                                           \
    | sort > /var/tmp/laptop-battery-status
  chmod 644 /var/tmp/laptop-battery-status
  return 0
}

# create stub file as the last resort
txt=""
txt="${txt}POWER_SUPPLY_CHARGE_FULL=10000000;"
txt="${txt}POWER_SUPPLY_CHARGE_FULL_DESIGN=10000000;"
txt="${txt}POWER_SUPPLY_CHARGE_NOW=10000000;"
txt="${txt}POWER_SUPPLY_CURRENT_GUESSED=${iguessed};"
txt="${txt}POWER_SUPPLY_CURRENT_NOW=0;"
txt="${txt}POWER_SUPPLY_MANUFACTURER=?;"
txt="${txt}POWER_SUPPLY_MODEL_NAME=?;"
txt="${txt}POWER_SUPPLY_ONLINE=1;"
txt="${txt}POWER_SUPPLY_PRESENT=1;"
txt="${txt}POWER_SUPPLY_STATUS=Full;"
txt="${txt}POWER_SUPPLY_TECHNOLOGY=Li-ion;"
txt="${txt}POWER_SUPPLY_VOLTAGE_MIN_DESIGN=8400000;"
txt="${txt}POWER_SUPPLY_VOLTAGE_NOW=8400000;"
convertandwrite "${txt}"

acpath=""
btpath=""
for d in /sys/class/power_supply/ACAD \
         /sys/class/power_supply/?*[0-9] ; do
  test -d "${d}" || continue
  type=`cat ${d}/type 2>/dev/null || :`
  test "${type}" = "Mains"   && acpath="${d}" || :
  test "${type}" = "Battery" && btpath="${d}" || :
  done
test -z "${acpath}" && exit || :
test -z "${btpath}" && exit || :
acpathuevent="${acpath}/uevent"
btpathuevent="${btpath}/uevent"

while : ; do
  acuevent=`cat ${acpathuevent} 2>/dev/null || :`
  btuevent=`cat ${btpathuevent} 2>/dev/null || :`
  test -z "${acuevent}" && exit || :
  test -z "${btuevent}" && exit || :
  txt=""
  txt="${txt}${acuevent};"
  txt="${txt}${btuevent};"
  txt="${txt}POWER_SUPPLY_CURRENT_GUESSED=${iguessed};"
  txt=`echo "${txt}" | tr '\012' ';'`
  convertandwrite "${txt}"
  sleep 17
  done

exit 0


Stephan K.H. Seidl