From 720d982e756ad4c84b5cb6b88b1d55b56db0f1d6 Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Fri, 3 Aug 2012 04:30:36 +0200 Subject: [PATCH] New sleepmon statistics --- c/goertzel/sleepcount.sh | 14 -------------- c/goertzel/sleepmon.sh | 42 +++++++++++++++++++++++++++++----------- c/goertzel/sleepplot.sh | 12 +++--------- 3 files changed, 34 insertions(+), 34 deletions(-) delete mode 100755 c/goertzel/sleepcount.sh diff --git a/c/goertzel/sleepcount.sh b/c/goertzel/sleepcount.sh deleted file mode 100755 index 920e42b..0000000 --- a/c/goertzel/sleepcount.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -seconds="$2" -START=$(head -n 1 "$1" | cut -f 2) -STARTOK=$({ tail -n 1 "$1.counts.$seconds" || echo 0; } | cut -f 1) -STOP=$( tail -n 1 "$1" | cut -f 2) - -fuser "$1.counts.$seconds" &>/dev/null && exit 23; - -test "$STARTOK" -gt "$START" && START="$STARTOK"; -seq $START $STOP | while read MAX; do - MIN=$(( $MAX - $seconds )) - echo -ne "$MAX\t" - cat "$1" | cut -f 2,5 | grep -v '\s0' | cut -f 1 | egrep $(echo $(seq $MIN $MAX) | tr ' ' '|') | wc -l -done >> "$1.counts.$seconds" diff --git a/c/goertzel/sleepmon.sh b/c/goertzel/sleepmon.sh index 0d32b65..9ac62f9 100755 --- a/c/goertzel/sleepmon.sh +++ b/c/goertzel/sleepmon.sh @@ -17,7 +17,11 @@ out=/tmp/sleeplog-"$(date +%F_%T)".txt speaker-test -t sine &>/dev/null & pid_test=$! tresh=10 -lastdate=0 +lastdate="$(date +%s)" +laststate=0 +history=''; +historymax=120; +historylen='10 30 120' screen=false while getopts "s" OPT; do test "$OPT" == 's' && screen=true; @@ -27,21 +31,37 @@ arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do date="$(date +%s)" time="$(echo "$line" | cut -f 1)" level="$(echo "$line" | cut -f 2)" - echo -ne "$time\t$date\t$(date '+%F%t%T')\t" - test "$level" -gt "$tresh" && { - echo -n "0 Nothing detected..."; + test "$level" -gt "$tresh" && state=false || state=true + $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 + } + + #Debug + echo -e "\t($statename $level After $after secs)"; + + #Fun with values + $state && { $screen && xset dpms force off || true; } || { - echo -n "1 Motion detected!!!!"; $screen && xset dpms force on; } - test "$lastdate" != 0 && { - after=$(( $date - $lastdate)) - echo -ne "\t$level After $after secs"; - } - echo; - ./sleepplot.sh "$out" &>/dev/null + ./sleepplot.sh "$out" &>/dev/null & + + #Prepare invariants for next round lastdate="$date"; + laststate="$statenum"; done | tee "$out" kill $pid_test echo diff --git a/c/goertzel/sleepplot.sh b/c/goertzel/sleepplot.sh index fe1c9af..3dcfec6 100755 --- a/c/goertzel/sleepplot.sh +++ b/c/goertzel/sleepplot.sh @@ -5,12 +5,6 @@ test -z "$1" && { exit 23 } -#Count events: -seconds='60 300' -for i in $seconds; do - ./sleepcount.sh "$1" "$i" & -done - #Approximate size of graph size="$(tail -n 1 "$1" | cut -d . -f 1)" test $size -gt 3600 && size="$(( $size/10 ))" #For prolonged periods @@ -34,10 +28,10 @@ set timefmt "%s" set grid #set pointsize 0.5 -#"$1.counts.$seconds" using 1:(\$2/5) title "Activations in last $seconds seconds" smooth csplines plot "$1" using 2:5 title "Sensor state" with steps,\ -"$1.counts.60" using 1:(\$2/5) title "Activations in last 60 seconds" smooth csplines,\ -"$1.counts.300" using 1:(\$2/10) title "Activations in last 300 seconds" smooth csplines\ +"" using 2:(\$6*3) title "Avg last last 10 seconds" with lines,\ +"" using 2:(\$7*3) title "Avg last last 30 seconds" with lines,\ +"" using 2:(\$8*3) title "Avg last last 120 seconds" with lines\ EOF -- 2.30.2