-
Notifications
You must be signed in to change notification settings - Fork 3
/
battery-status-graph-flow
executable file
·64 lines (54 loc) · 1.51 KB
/
battery-status-graph-flow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
# License: GNU Public License v2 or later at your choice.
input=/var/log/hjemmenett-battery-status.log
filename="${1}"
flowfilename=$(mktemp)
perl -MText::CSV <<'EOF' > "$flowfilename"
use strict;
use warnings;
my $csv = Text::CSV->new();
my $fh;
open $fh, "<:encoding(utf8)", '/var/log/hjemmenett-battery-status.log'
or die "open failed: $!";
my $last2 = undef;
my $lasttime2 = undef;
my $last = undef;
my $lasttime = undef;
my @fieldsrow = $csv->getline($fh);
while (my $row = $csv->getline($fh) ) {
if (defined $last2) {
my $diff = $row->[7] - $last2;
my $timediff = $row->[0] - $lasttime2;
if (0 < $timediff) {
printf "%s,%.2f\n", $row->[0], $diff / $timediff;
}
}
$last2 = $last;
$lasttime2 = $lasttime;
$last = $row->[7];
$lasttime = $row->[0];
}
$csv->eof or $csv->error_diag();
close($fh);
EOF
# manufacturer,model_name,technology
type=$(head -2 "$input" | tail -1 | cut -d, -f2-4 | tr , " ")
(
echo set xdata time
echo set timefmt \"%s\"
echo set format x \"%Y\"
echo set datafile separator \",\"
echo set title \'Battery statistics $type\'
echo set ylabel \'Flow per second\'
echo set xlabel \'Year\'
echo set grid
if [ "$filename" ]; then
echo set term png
echo set output \"$filename\"
fi
echo plot "\"$flowfilename\" using 1:2 smooth unique axis x1y1 title \"Energy flow\" with dots"
) | gnuplot -p
if [ "$filename" ] ; then
echo "PNG graph $filename created."
fi
rm $flowfilename