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)"
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;
lastdate="$date";
laststate="$statenum";
done | tee "$out"
-kill $pid_test; sleep 0.2
+kill $(jobs -p); sleep 0.2
echo
echo "Your log: $out"
$graph && {
#set pointsize 0.5
plot\
"$in" using 2:5 title "Sensor state" with steps,\
-"" using 2:((\$6*3+\$7*6+\$8*6+\$9+\$10)/5) title "Avg." with lines\
+"$in.stats" using 1:((\$3*3+\$4*6+\$5*6+\$6+\$7)/5) title "Avg." smooth bezier\
+#"" using 2:((\$6*3+\$7*6+\$8*6+\$9+\$10)/5) title "Avg." with lines,\
+#"$in.stats" using 1:((\$3*3+\$4*6+\$5*6+\$6+\$7)/5) title "Avg." with lines,\
#"" using 2:(\$6*2) title "Avg. 10s" with lines,\
#"" using 2:(\$7*3) title "Avg. 30s" with lines,\
#"" using 2:(\$8*3) title "Avg. 60s" with lines,\
--- /dev/null
+#!/bin/bash
+
+export LC_ALL=C
+
+historymax=120;
+historylen='10 30 60 90 120'
+laststate=0;
+lastdate=''
+history='';
+
+#statsdone=$(tail -n $historymax "$1".stats | head -n 1 | cut -d ' ' -f 1);
+
+tail -f "$1" --pid=$$ | while read line; do
+ date="$(echo "$line" | cut -d ' ' -f 2)"
+ #test -n "$statsdone" && test "$date" -le "$statsdone" && continue;
+ state="$(echo "$line" | cut -d ' ' -f 5)"
+ test -z "$lastdate" && lastdate="$(( $date - 1 ))"
+
+ #echo $line
+ #echo $lastdate $date $state
+
+ for i in $(seq $lastdate $(( $date - 1 ))); do
+ echo -ne "$i $state\t"
+ history=$(echo -n "$laststate$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)"
+ printf " %.2f" "$on"
+ done
+ echo
+ done
+
+ #History
+ #after=$(( $date - $lastdate))
+ #test $historymax -gt 0 && {
+ #}
+
+
+ #Prepare invariants for next round
+ lastdate="$date";
+ laststate="$state";
+done > "$1.stats"