SA_RESTART do sigaction
[mirrors/Programs.git] / c / goertzel / sleepmon.sh
index 9ac62f9f7ce623abf46d5848f0a3e10799908475..cfa02973e5144863ac161dc9f38d5f857c6225c6 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Simple sleep monitor (Harvie 2012)
 #
 # You probably have soundcard with one output and input
 # Wake up
 # Enjoy your data
 
-
 out=/tmp/sleeplog-"$(date +%F_%T)".txt
+graphout="${out%%.*}.png"
+
+killall speaker-test &>/dev/null
 speaker-test -t sine &>/dev/null &
-pid_test=$!
+
+touch "$out"
+bash ./sleepstats.sh "$out" &>/dev/null &
+
 tresh=10
 lastdate="$(date +%s)"
-laststate=0
-history='';
-historymax=120;
-historylen='10 30 120'
 screen=false
-while getopts "s" OPT; do
+graph=false
+
+export LC_ALL=C
+
+#trap 'kill -9 $(jobs -p);' SIGINT
+
+while getopts "sg" OPT; do
        test "$OPT" == 's' && screen=true;
+       test "$OPT" == 'g' && graph=true;
 done
-echo "Writing to file: $out";
+
+echo "Writing log to: $out";
+$graph && echo "Writing graph to: $graphout"
+echo
 arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
        date="$(date +%s)"
        time="$(echo "$line" | cut -f 1)"
@@ -35,21 +46,11 @@ arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
        $state && statenum=1 || statenum=0;
        $state && statename='MOTION!' || statename='Nothing';
 
-       echo -ne "$time\t$date $(date '+%F %T') $statenum"
-
-       #History
-       after=$(( $date - $lastdate))
-       test $historymax -gt 0 && {
-               history=$(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" | head -c $historymax)
-               for len in $historylen; do
-                       on="$(echo -n ${history::$len} | tr -d 0 | wc -c)"
-                       on="$(echo "scale=2; $on/$len" | bc)"
-                       LC_ALL=C printf " %.2f" "$on"
-               done
-       }
+       printf  "%.2f\t %s %s %d" "$time" "$date" "$(date '+%F %T')" "$statenum"
 
        #Debug
-       echo -e "\t($statename $level After $after secs)";
+       after=$(( $date - $lastdate))
+       printf " (%s %3d After %4d secs)\n" "$statename" "$level" "$after";
 
        #Fun with values
        $state && {
@@ -57,12 +58,15 @@ arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
        } || {
                $screen && xset dpms force on;
        }
-       ./sleepplot.sh "$out" &>/dev/null &
+       $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null &
 
        #Prepare invariants for next round
        lastdate="$date";
-       laststate="$statenum";
 done | tee "$out"
-kill $pid_test
+kill $(jobs -p); sleep 0.2
 echo
-echo "Your file: $out"
+echo "Your log: $out"
+$graph && {
+       ./sleepplot.sh "$out" "$graphout" &>/dev/null
+       echo "Your graph: $graphout"
+}
This page took 0.133987 seconds and 4 git commands to generate.