#!/usr/bin/perl
# This script generates the trend graphs for the electricity reading of all
# installed units. It takes "length" as a parameter, with values 0...4,
# for day/week/month/quarter as arguments, passed by via GET method.
# Directories where the log files reside in
#$logdir="/home/chris/BTUmetering/log";
#$gnudir="/home/chris/BTUmetering/gnuplot";
$logdir="MAINPROGROOT/log";
$gnudir="MAINPROGROOT/gnuplot";
# Image directory:
# $imgdir="/srv/www/htdocs/img2";
$imgdir="HTDOCROOT/img2";
# Following http://www.tutorialspoint.com/perl/perl_cgi.htm, we extract the
# parameters sent via get:
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
# Extract the interesting ones
$timespan = $FORM{length};
# Generate page header
print "content-type: text/html\n\n";
print "
CQT utility consumption server
\n";
# Now, we need to check for validity of the timespan entry
# test if we have proper value 0...3 for length
if ($timespan !~ /[0123]/) {
# by default, take a week
$timespan=1;
}
# For debugging
# $timespan=1;
# Prepare flexible arguments for gnuplot for different sizes
@datapoints = ( 750, 5400, 23000, 60000);
@samplingdensity = (1, 5, 10, 30);
@outputformat = ("%m/%d\\n%H:%M", "%a\\n%m/%d\\n%H:%M", "%m/%d", "%d.%b");
$imagename = "emeter_".$datapoints[$timespan].".png";
# compose parameter set for gnuplot
$parset="num=$datapoints[$timespan]; evr=$samplingdensity[$timespan]; ofmt=\"$outputformat[$timespan]\"; outfilename=\"$imagename\"; logdir=\"$logdir\"; imgdir=\"$imgdir\" \n";
# generate image
open GNUPLOT, "| gnuplot - $gnudir/combined_emeter.gnu >/dev/null";
print GNUPLOT $parset;
close GNUPLOT;
# Generate web page
print "Electrical load trend
\n";
print "
\n";
# Select other resolutions/ units
print "
Show other time span:
day |
week |
month |
quarter
";
# Link to main page of this server
print "Back to main page.
";
# Some explanatory text
print "Explanations
";
print "- The apparent power shown here reflects the sum of the products of voltage and current for all three phases of each extraction point/meter. Due to non-ohmic loads, this quantity exceeds the actual real work consumed by 1-5%.
";
print "- The meters named MDBx reflect the main power connection for floor x, the E-MDBx show power consumption for emergency supplies, backed up with a diesel generator in S16, and the ones named EAHUx show the electrical consumption for the AHU equipment (mostly the fan).
";
print "- At a price of 30cts/kWh, an average electrical power of 10kVA leads to costs of about 2100SGD/month.
";
print "- The installation dates of the electricity meters at levels 1-4 were at the beginning of CQT, around November/December 2007, the electricity meters for level 6 on 22 Sept 2012.
";
# Finish html page
print "\n\n";