forked from skaringa/emeocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.pl
executable file
·163 lines (143 loc) · 3.04 KB
/
table.pl
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/perl
#
# CGI script to create a table of values from RRD
use CGI qw(:all *table *Tr);
use RRDs;
use POSIX qw(strftime);
use POSIX qw(locale_h);
$ENV{'LANG'}='de_DE.UTF-8';
setlocale(LC_TIME, 'de_DE.UTF-8');
# path to database
$rrdb='../../emeocv/emeter.rrd';
$query=new CGI;
#$query=new CGI("type=consumweek");
print header(-charset=>"utf-8");
$type=$query->param('type');
if ($type !~ /(count|consum)(day|week|month|year)/) {
print "Invalid type\n";
exit;
}
$sensor=$1;
$range=$2;
#print "Sensor: $sensor Range: $range\n";
$timeformat="%a, %d. %b %Y";
$resolution=24*60*60;
if ($range eq 'day') {
$resolution=60*60;
$timeformat="%a, %d. %b %Y %H:%M";
} elsif ($range eq 'year') {
$resolution=7*24*60*60;
$timeformat="Wo %d. %b %Y";
}
if ($sensor eq 'count') {
@indexes = (0);
@headings = ('kWh');
$title = "Zähler";
$factor = 1.0;
$cf = "LAST";
$timeformat="%a, %d. %b %Y %H:%M";
} elsif ($sensor eq 'consum') {
@indexes = (1);
$title = "Leistung";
if ($range eq 'day') {
$factor = 1.0; # consumption in Watt
@headings = ('W');
} else {
$factor = 24.0/1000.0; # consumption in kWh/day
@headings = ('kWh/d');
}
$cf = "AVERAGE";
}
($start,$step,$names,$data) = getdata($rrdb, $cf, $resolution, $range);
# print result
# print heading
$style=<<END;
<!--
body {
font: 10px/16px verdana, arial, helvetica, sans-serif;
background-color: white;
color: black;
}
th, td {
border-width: 1px 1px 0px 0px;
border-style: solid;
border-color: #998DB3;
text-align: right;
width: 50px;
}
th {
background-color: lightgreen;
font-weight: normal;
}
thead {
position: fixed;
top: 0px;
background-color: white;
z-index: 1;
}
tbody {
position: absolute;
top: 30px;
z-index: -1;
}
.mindata {
color: blue;
}
.maxdata {
color: red;
}
.time {
color: black;
text-align: left;
width: 100px;
}
-->
END
print start_html(-title=>"Stromverbrauch - $title",
-style=>{-code=>$style},
-meta=>{"viewport"=>"initial-scale=1, minimum-scale=0.75, width=device-width"}
);
print start_table();
print "\n";
print thead(Tr(th({-class=>'time'}, $title), th([@headings])));
print "\n";
# print data
$start -= 12*60*60 unless $timeformat =~ / %H:%M$/;
for $line (@$data) {
$hastime=0;
print start_Tr({-class=>'avrdata'});
if (! $hastime) {
print td({-class=>'time'}, strftime($timeformat, localtime($start)));
}
print formatline(@$line[@indexes]);
print end_Tr;
print "\n";
$start += $step;
}
print end_table();
print end_html;
sub getdata {
my ($db, $cf, $resolution, $range) = @_;
my $sarg = "now -1 $range";
if ($range eq 'month') {
$sarg = "now - 30 days";
}
my ($start,$step,$names,$data) = RRDs::fetch($db, $cf, '-r', $resolution, '-s', $sarg, '-e', 'now');
# check error
my $err=RRDs::error;
print "$err\n" if $err;
exit if $err;
return ($start,$step,$names,$data);
}
sub formatline {
my @line = @_;
my $str = '';
for $val (@line) {
if (defined $val) {
$str .= td(sprintf "%5.1f", $val * $factor);
} else {
$str .= td;
}
}
return $str;
}