aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2019-06-09 23:01:49 -0400
committerB Stack <bgstack15@gmail.com>2019-06-09 23:01:49 -0400
commit8ae5eca160cf6ba99c091d8e9f981eb1222221c1 (patch)
treeaf159e442fe6329c35da92fa757c551a442c1a28
downloadon-battery-time-master.tar.gz
on-battery-time-master.tar.bz2
on-battery-time-master.zip
initial commitHEADmaster
-rw-r--r--99-on-battery-time.rules12
-rw-r--r--README6
-rwxr-xr-xon-battery-time.sh50
-rwxr-xr-xonac.sh4
-rwxr-xr-xonbattery.sh4
5 files changed, 76 insertions, 0 deletions
diff --git a/99-on-battery-time.rules b/99-on-battery-time.rules
new file mode 100644
index 0000000..0ad6f8d
--- /dev/null
+++ b/99-on-battery-time.rules
@@ -0,0 +1,12 @@
+# Rule for when switching to battery
+# File: /etc/udev/rules.d/99-on-battery-time.rules
+# References:
+# http://www.reactivated.net/writing_udev_rules.html
+# https://opensourceforu.com/2012/06/some-nifty-udev-rules-and-examples/
+# https://superuser.com/questions/1417292/udev-rule-to-start-a-command-on-ac-battery-plug-unplug-event
+# https://bbs.archlinux.org/viewtopic.php?id=136396
+# https://duckduckgo.com/?q=linux+how+long+has+laptop+been+running+on+battery+-life&ia=web
+SUBSYSTEM!="power_supply", GOTO="onbattery_end"
+ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/bin/onbattery.sh"
+ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/bin/onac.sh"
+LABEL="onbattery_end"
diff --git a/README b/README
new file mode 100644
index 0000000..979dfd6
--- /dev/null
+++ b/README
@@ -0,0 +1,6 @@
+Futureimprovements
+* add docs
+* document dependencies
+* build makefile
+* build rpm
+* build dpkg
diff --git a/on-battery-time.sh b/on-battery-time.sh
new file mode 100755
index 0000000..6e901dc
--- /dev/null
+++ b/on-battery-time.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+# File: /usr/local/bin/on-battery-time.sh
+# Author: bgstack15
+# Startdate: 2019-06-09 22:35
+# Title: Report how long this system has been on battery or AC power
+# Package: on-battery-time
+# Purpose:
+# History:
+# Usage:
+# Reference:
+# https://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds/18683834#18683834
+# Improve:
+# Dependencies:
+# bc
+# date
+# the udev rules from 99-on-battery-time.rules
+# Documentation:
+
+OBT_INFILE=/var/log/battery.log
+
+# FUNCTIONS
+convertsecs() {
+ _h=$( expr $1 / 3600 )
+ _m=$( expr $1 % 3600 / 60 )
+ _s=$( expr $1 % 60 )
+ printf "%02d:%02d:%02d\n" ${_h} ${_m} ${_s}
+}
+
+# learn most recent utility time
+last_utility_time="$( { tac "${OBT_INFILE}" | awk '/utility power/{print $2}' ; echo 0 ; } | head -n1 )"
+
+# learn most recent battery time
+last_battery_time="$( { tac "${OBT_INFILE}" | awk '/on battery/{print $2}' ; echo 0 ; } | head -n1 )"
+
+# learn current time
+OBT_current_time="$( date "+%s" )"
+
+if test ${last_utility_time} -ge ${last_battery_time} ;
+then
+ OBT_last="${last_utility_time}"
+ OBT_phrase="utility power"
+else
+ OBT_last="${last_battery_time}"
+ OBT_phrase="on battery"
+fi
+
+OBT_seconds="$( printf "%s-%s\n" "${OBT_current_time}" "${OBT_last}" | bc )"
+
+printf "%s seconds: %s\n" "${OBT_phrase}" "${OBT_seconds}"
+convertsecs "${OBT_seconds}"
diff --git a/onac.sh b/onac.sh
new file mode 100755
index 0000000..93ba4a8
--- /dev/null
+++ b/onac.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+# File: /usr/local/bin/onac.sh
+# the hash-bang is vital for eudev to be able to execute this script.
+date -u "+$( hostname -s ) %s %FT%TZ now using utility power" >> /var/log/battery.log
diff --git a/onbattery.sh b/onbattery.sh
new file mode 100755
index 0000000..51b0e4e
--- /dev/null
+++ b/onbattery.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+# File: /usr/local/bin/onbattery.sh
+# the hash-bang is vital for eudev to be able to execute this script.
+date -u "+$( hostname -s ) %s %FT%TZ now running on battery" >> /var/log/battery.log
bgstack15