#!/usr/bin/perl -w # This code generates some plain HTML code about consumed heat, either for # one or several months, depending on the command line parameter. By default, # the last monthly bill is generated, of a specific date given (must be # specified in the form mm/dd/yy, where dd=01), or all months if "all" is # given as parameter. # Directories where the log files reside in, adjusted by configure script # $sumdir="/home/chris/construction/BTUmetering/summarize"; $sumdir="MAINPROGROOT/log"; # extract commandline parameters; do first interpretation if ($#ARGV != 0) { $shm="last"; } else { $shm=$ARGV[0]; } # only leave alpanum and slashes in the string $shm =~ s/[^\w\/]//g; # summary file name $sumfile="$sumdir/summaryfile"; # silly date conversion functions @months=qw(x January February March April May June July August September October November December); sub mdytoymd { my @a=split '/', $_[0]; return "$a[2]-$a[0]-$a[1]";} sub ymdtomdy { my @a=split '-', $_[0]; return "$a[1]/$a[2]/$a[0]";} sub mdytotext { my @a=split '/', $_[0]; return "$months[$a[0]] 20$a[2]";} #clear hash for readings %readings = (); %unithash = (); %datehash = (); # read in summary file open(SUMFH, $sumfile) or die "cannot open file."; while ($line = ) { ($unit, $date, $rest) = split " ", $line, 3; $readings{"$unit:$date"}=$rest; $unithash{$unit}=1; $datehash{$date}=1; } # Generate sorted lists of units and dates @units = sort keys %unithash; @dates = map {ymdtomdy($_)} sort map {mdytoymd($_)} keys %datehash; # generate a list of months to display, depending on command line parameter if (exists $datehash{$shm}) { @showdate=($shm); } elsif ($shm eq "all") { @showdate=@dates;; } else { @showdate=($dates[-1]); } # Finally, generate HTML code foreach $month (reverse @showdate) { # convert into sensible time name $mtx=mdytotext($month); # Generate table header print "

Chilled water consumption for the $mtx reading

\n"; print "

\n"; # go through all units for a given date and generate table rows $totalheat = 0; foreach $unit (@units) { if (exists $readings{"$unit:$month"}) { @rd=split ' ', $readings{"$unit:$month"}; print ""; print "\n"; $totalheat += $rd[4]; $lastread = $rd[1]; } } # Generate total sum line and close the table print "\n
Unit This reading
(kWh)
Last reading
(kWh)
Consumption
(kWh)
$unit$rd[2]$rd[3]$rd[4]
Total heat dumped in chilled water: $totalheat

\n"; # final remark print "

Measurement time interval was from $lastread to $month.

\n"; }