#!/usr/bin/perl

# use strict;
# use warnings;
# use diagnostics;

use DBI;
use Expect;

use lib '/usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi/';

use RRDs;

my $rrd = '/var/lib/rrd';
my $img = '/mnt/disk2/www/html/mon';

fork and exit;

&RunDIGITEMP("temp", "pbarp");

sub RunDIGITEMP
{
    $timeout = 15;

    $pollInterval = 300;
#    $pollInterval = 100;

    my $t5ok = 0;
    my $h3ok = 0;
    my $r2ok = 0;
    my $badreading;
    
    my $ntry = 5;

    my $t1;
    my $t2;
    my $t3;
    my $t4;
    my $t5;

    my $h2;
    my $h3;

    my $r1;
    my $r2;

    my $p1;
    
    my $l1;

    my $total;
    my $rate;

# Database info
    my $db_name     = "weather";
    my $db_user     = "weatherDAQ";
    my $db_pass     = "Wea^tHe8r";

    my $debug = 0;


    for (;;) {

      $badreading = 0;


      &CreateRRD();

      print "===> Start\n";

#------ Library Temperature
      $ok = 0;
      $icount = 0;
      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 0 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );
	
	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);
	
	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  sleep 2;
	  $ok = 0;
	}
	if ($mytempArray1[0] eq "CRC") {          
	  print ">>> CRC check $mytempArray1[0]...\n";
	  sleep 2;
	  $ok = 0;
	}

	if ($mytempArray1[0] == 0) {
	  $t2       = $mytempArray1[8];
	} 

	if ($icount++ == 5) {
	  $ok = 1;
	}

      }

#------ Computer Exhaust
      $ok = 0;
      $icount = 0;
      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 3 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );

	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);

	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  sleep 2;
	  $ok = 0;
	}
      
	if ($mytempArray1[0] eq "CRC") {          
	  print ">>> CRC check $mytempArray1[0]...\n";
	  sleep 2;
	  $ok = 0;
	}

	if ($mytempArray1[0] == 0) { 
	  $t1       = $mytempArray1[8];
	}

	if ($icount++ == 2) {
	  $ok = 1;
	}
	
      }
	
#------ Outside Temperature
      $ok = 0;
      $icount = 0;
      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 1 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );
	
	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);

	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  sleep 2;
	  $ok = 0;
	}
	if ($mytempArray1[0] eq "CRC") {          
	  print ">>> CRC check $mytempArray1[0]...\n";
	  sleep 2;
	  $ok = 0;
	}
      
	if ($mytempArray1[0] == 0) {
	  $t3       = $mytempArray1[8];
	}
	
	if ($icount++ == 2) {
	  $ok = 1;
	}
	
      }


#------ Living Room
      $ok = 0;
      $icount = 0;
      
      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 2 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );
	
	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);

	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  sleep 2;
	  $ok = 0;
	}      
	if ($mytempArray1[0] eq "CRC") {          
	  print ">>> CRC check $mytempArray1[0]...\n";
	  sleep 2;
	  $ok = 0;
	}

	if ($mytempArray1[0] == 0) {
	  $t4 = $mytempArray1[8];
	} 

	if ($icount++ == 2) {
	  $ok = 1;
	}

      }

#------ Inside Humidity
      $ok = 0;
      $icount = 0;
      
      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 5 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );

	my $str1 = $exp->before();
	$str1  =~ tr/\%/ /;	
	@mytempArray1 = split(" ",$str1);

	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  sleep 2;
	  $ok = 0;
	}
	
	if ($mytempArray1[0] == 0) {
	  $h2       = $mytempArray1[10];
	}

	if ($h2 == 100) {          
	  print ">>> Humidity check $h2...\n";
	  sleep 2;
	  $ok = 0;
	}

	if ($icount++ == 2) {
	  $ok = 1;
	}

      }



#------ Outside Temperature, Humidity and Solar 
      $ok = 0;
      $icount = 0;

      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 6 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	  
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );
	  
	my $str1 = $exp->before();
	$str1  =~ tr/\%/ /;
	@mytempArray1 = split(" ",$str1);

	if ((length $str1) == 0) {
	  print ">>> Null string\n";
	  $ok   = 0;
#	  sleep 1;
	}

	if ($mytempArray1[3] eq "Sensor") {

	  $tc5  = $mytempArray1[6];

	  if ($tc5 == -1.) {          
	    print ">>> Temp check $tc5...\n";
#	    sleep 2;
	    $ok = 0;
	  }

	  if ($ok == 1) {
	    $t5   = $mytempArray1[8];
	    $h3   = $mytempArray1[10];
	    $r2   = $mytempArray1[12];
	   
	    $t5ok = $t5;
	    $h3ok = $h3;
	    $r2ok = $r2;

	    if ($h3 == 100) {          
	      print ">>> Humidity check $h3...\n";
	      sleep 1;
	      $ok = 0;
	    }
	    if ($r2 > 65000) {          
	      print ">>> Radiation check $r2...\n";
	      sleep 1;
	      $ok = 0;
	    }


	  }
	  	  
	} else {
	  print ">>> Null no Sensor string\n";
	  $ok   = 0;

	  $t5   = $t5ok;
	  $h3   = $h3ok;
	  $r2   = $r2ok;
	}

	if ($mytempArray1[0] eq "CRC") {          
	  print ">>> CRC check $mytempArray1[0]...\n";
#	  sleep 2;
	  $ok = 0;
	}

	if ($icount++ == 15) {
	  print ">>> Bad Reading...\n";
	  $badreading = 1;
	  $ok = 1;
	}
	
      }




#------ Inside Solar Monitor
      $ok = 1;
      $icount = 0;
      $r1 = 0;

      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 4 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );

	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);
	
	if ($mytempArray1[0] == 0) {
	  $r1 = $mytempArray1[12];
	}

        if ($r1 > 60000) {
	  print ">>> Solar check $r1...\n";
	  $ok = 0;
	}

#	if ($r1 == 0) {
#	  print ">>> Solar check $r1...\n";
#	  sleep 2;
#	  $ok = 0;
#	}

	if ($icount++ == 3) {
	  $ok = 1;
	}

      }


#------ Inside Pressure
      $ok = 0;
      $icount = 0;

      while ($ok == 0) {
	$ok = 1;  
	my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 7 -A -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
	  or die "Cannot spawn unreliable process $!\n";
	
	$exp->expect($timeout,
		     [
		      timeout =>
		      sub {
			print "Process timed out.\n";
		      }
		     ]
		    );
	  
	my $str1 = $exp->before();
	@mytempArray1 = split(" ",$str1);

	$p1x  = $mytempArray1[8];
	if ($p1x == -1.) {
	  print ">>> Pressure check $p1x...\n";
	  $ok = 0;
	  sleep 2;
	}


	if ($icount++ == 2) {
	  $ok = 1;
	}
      }


      if ($mytempArray1[0] == 0) {

	$p1x  = $mytempArray1[8];
	$p1  = 26.6566+0.6717*$p1x;
#	print ("P1 = $p1 P1x = $p1x\n");

      }


#------ Lightning Counter
      $ok = 0;
      $icount = 0;

      while ($ok == 0) {
        $ok = 1;

        my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 8 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
            or die "Cannot spawn unreliable process $!\n";

        $exp->expect($timeout,
                     [
                      timeout =>
                      sub {
                        print "Process timed out.\n";
                        $ok = 0;
                      }
                      ]
                     );


        my $str = $exp->before();
        $str  =~ tr/\%/ /;

        @mytempArray = split(" ",$str);

        if ((length $str) == 0) {
          print ">>> Null string\n";
          $ok = 0;
        }

        if ($mytempArray[0] eq "CRC") {
          print ">>> CRC check $mytempArray[0]...\n";
          $ok = 0;
        }

        if ($ok == 1) {

          $l1       = $mytempArray[6];
          $l2       = $mytempArray[10];
        }


        if ($icount++ == 10) {
          $badReading = 1;
          $ok         = 1;
        }

      }

#------ Rain Gauge
      $ok     = 0;
      $icount = 0;
      
      while ($ok == 0) {
        $ok = 1;

        my $exp = Expect->spawn("/home/chlebana/bin/digitemp_DS9097U -t 9 -q -c /home/chlebana/rrd/config/digitemp_ow-10.conf")
            or die "Cannot spawn unreliable process $!\n";

        $exp->expect($timeout,
                     [
                      timeout =>
                      sub {
                        print "Process timed out.\n";
                        $ok = 0;
                      }
                      ]
                     );


        my $str = $exp->before();
        $str  =~ tr/\%/ /;

        @mytempArray = split(" ",$str);

        if ((length $str) == 0) {
          print ">>> Null string\n";
          $ok = 0;
        }
        if ($mytempArray[0] eq "CRC") {
          print ">>> CRC check $mytempArray[0]...\n";
          $ok = 0;
        }

        if ($ok == 1) {
          $total       = $mytempArray[13];
          $rate        = $mytempArray[13];
        }

        if ($icount++ == 10) {
          $badReading = 1;
          $ok         = 1;
        }

      }

# --- Update RRD database

      print (">>> T1 = $t1 T2 = $t2 T3 = $t3 T4 = $t4 T5 = $t5 H2 = $h2 H3 = $h3\n");
      RRDs::update ("$rrd/environ.rrd",
		    "-t",
		    "t1:t2:t3:t4:t5:h2:h3",
		    "N:$t1:$t2:$t3:$t4:$t5:$h2:$h3" );
      my $error =  RRDs::error;

      print (">>> Solar Radiation: R1 = $r1 R2 = $r2\n");
      RRDs::update ("$rrd/solar1.rrd",
		    "-t", 
		    "r1:r2",
		    "N:$r1:$r2" );
      my $error =  RRDs::error;
#      print (">>> RRDs:error = $error\n");


      if (($p1 > 0) && ($p1 < 100))  {
	  RRDs::update ("$rrd/pressure.rrd",
			"-t", 
			"p1",
			"N:$p1" );
      }    


      print (">>> Lightning Count: L1 = $l1\n");
      RRDs::update ("$rrd/lightning.rrd",
                    "-t",
                    "l1",
                    "N:$l1");
      my $ERR=RRDs::error;
      if ($ERR) {print ">>> RRDs:error = $error\n";}



      print (">>> Rain Count = $total\n");
      RRDs::update ("$rrd/rain.rrd",
                    "-t",
                    "total:rate",
                    "N:$total:$rate");

      my $ERR=RRDs::error;
      if ($ERR) {print ">>> RRDs:error = $error\n";}



# --- Update mysql database
      my $dbh = DBI->connect("dbi:mysql:$db_name","$db_user","$db_pass")
          or die "I cannot connect to dbi:mysql:$db_name as $db_user - $DBI::errstr\n";

      # --- temperature
      if ($t1) {
	my $sql="INSERT INTO temperature SET Location='Exhaust',Fahrenheit=$t1";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      if ($t2) {
	my $sql="INSERT INTO temperature SET Location='Library',Fahrenheit=$t2";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }
      if ($t3) {
	my $sql="INSERT INTO temperature SET Location='Outside',Fahrenheit=$t3";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }
      if ($t4) {
	my $sql="INSERT INTO temperature SET Location='Living Room',Fahrenheit=$t4";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }


      # --- humidity
      if ($h2) {
	my $sql="INSERT INTO humidity SET Location='Inside',Humidity=$h2";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      if ($h3) {
	my $sql="INSERT INTO humidity SET Location='Outside',Humidity=$h3";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      # --- pressure
      if ($p1) {
	my $sql="INSERT INTO pressure SET Location='Inside',Pressure=$p1";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }
      
      # --- solar
      if ($r2) {
	my $sql="INSERT INTO solar SET Location='Outside',Solar=$r2";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      # --- lightning
      if ($l1) {
	my $sql="INSERT INTO lightning SET Location='Outside',Lightning=$l1";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      # --- rain
      if ($total) {
	my $sql="INSERT INTO rain SET Location='Outside',Rain=$total";
	print "SQL: $sql\n" if($debug);
	$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
      }

      $dbh->disconnect;


      # create graphs
      &CreateGraph("1day", $_[1]);
      &CreateGraph("1week", $_[1]);
      &CreateGraph("1month", $_[1]);
      &CreateGraph("1year", $_[1]);
      
      &CreateGraph("2day", $_[1]);
      &CreateGraph("2week", $_[1]);
      &CreateGraph("2month", $_[1]);
      &CreateGraph("2year", $_[1]);

      sleep $pollInterval;
    }

  }



# ***********************************
sub CreateRRD
{
#------ Create database if it does not exist
    if (! -e "$rrd/environ.rrd") {
	print "creating rrd database for environ...\n";	
	RRDs::create "$rrd/environ.rrd",
	    "-s 300",
	    "DS:t1:GAUGE:600:0:100",
	    "DS:t2:GAUGE:600:0:100",
	    "DS:t3:GAUGE:600:0:100",
	    "DS:t4:GAUGE:600:0:100",
	    "DS:t5:GAUGE:600:0:100",
	    "DS:h1:GAUGE:600:0:100",
	    "DS:h2:GAUGE:600:0:100",
	    "DS:h3:GAUGE:600:0:100",
	    "DS:h4:GAUGE:600:0:100",
	    "DS:h5:GAUGE:600:0:100",
	    "DS:p1:GAUGE:600:0:100",
	    "DS:p2:GAUGE:600:0:100",
	    "DS:p3:GAUGE:600:0:100",
	    "DS:p4:GAUGE:600:0:100",
	    "DS:p5:GAUGE:600:0:100",
	    "DS:r1:GAUGE:600:0:100",
	    "DS:r2:GAUGE:600:0:100",
	    "DS:r3:GAUGE:600:0:100",
	    "DS:r4:GAUGE:600:0:100",
	    "DS:r5:GAUGE:600:0:100",
	    "DS:l1:GAUGE:600:0:100",
	    "DS:l2:GAUGE:600:0:100",
	    "DS:l3:GAUGE:600:0:100",
	    "DS:l4:GAUGE:600:0:100",
	    "DS:l5:GAUGE:600:0:100",
	    "RRA:AVERAGE:0.5:1:576",
	    "RRA:AVERAGE:0.5:6:672",
	    "RRA:AVERAGE:0.5:24:732",
	    "RRA:AVERAGE:0.5:144:1460";
      }

      if (! -e "$rrd/solar1.rrd") {
	print "creating rrd database for solar1...\n";
	  
	RRDs::create "$rrd/solar1.rrd",
	    "-s 300",
	    "DS:r1:GAUGE:600:0:2000",
	    "DS:r2:GAUGE:600:0:2000",
            "DS:r3:GAUGE:600:0:2000",
            "DS:r4:GAUGE:600:0:2000",
            "DS:r5:GAUGE:600:0:2000",
            "RRA:AVERAGE:0.5:1:576",
            "RRA:AVERAGE:0.5:6:672",
            "RRA:AVERAGE:0.5:24:732",
            "RRA:AVERAGE:0.5:144:1460";
      }

      if (! -e "$rrd/pressure.rrd") {
	print "creating rrd database for pressure...\n";

	RRDs::create "$rrd/pressure.rrd",
	    "-s 300",
            "DS:p1:GAUGE:600:0:100",
	    "DS:p2:GAUGE:600:0:100",
	    "DS:p3:GAUGE:600:0:100",
            "DS:p4:GAUGE:600:0:100",
            "DS:p5:GAUGE:600:0:100",
            "RRA:AVERAGE:0.5:1:576",
            "RRA:AVERAGE:0.5:6:672",
            "RRA:AVERAGE:0.5:24:732",
            "RRA:AVERAGE:0.5:144:1460";
      }
      if (! -e "$rrd/lightning.rrd") {
        print "creating rrd database for lightning...\n";

        RRDs::create "$rrd/lightning.rrd",
            "-s 300",
	    "DS:l1:COUNTER:600:U:U",
            "RRA:AVERAGE:0.5:1:576",
            "RRA:AVERAGE:0.5:6:672",
            "RRA:AVERAGE:0.5:24:732",
	    "RRA:AVERAGE:0.5:144:1460";
      }

      if (! -e "$rrd/rain.rrd") {
        print "creating rrd database for rain...\n";

        RRDs::create "$rrd/rain.rrd",
            "-s 300",
	    "DS:total:GAUGE:600:U:U",
	    "DS:rate:COUNTER:600:U:U",
            "RRA:AVERAGE:0.5:1:576",
            "RRA:AVERAGE:0.5:6:672",
            "RRA:AVERAGE:0.5:24:732",
	    "RRA:AVERAGE:0.5:144:1460";
      }

}


# ***********************************
sub CreateGraph
{

#----- OW Graph
	RRDs::graph "$img/ow-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-t $_[1]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v Temperature",
		"DEF:t1=$rrd/temp.rrd:t1:AVERAGE",
		"LINE2:t1#0000FF:Temp 1",
		"GPRINT:t1:MIN: Min\\: %2.lf",
		"GPRINT:t1:MAX: Max\\: %2.lf",
		"GPRINT:t1:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:t1:LAST: Current\\: %2.lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate temp graph: $ERROR\n"; }

#----- Temperature Graph
	RRDs::graph "$img/temp-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v Temperature",
		"DEF:t1=$rrd/environ.rrd:t1:AVERAGE",
		"DEF:t2=$rrd/environ.rrd:t2:AVERAGE",
		"DEF:t3=$rrd/environ.rrd:t3:AVERAGE",
		"DEF:t4=$rrd/environ.rrd:t4:AVERAGE",
		"LINE2:t1#0000FF:Exhaust    ",
		"GPRINT:t1:MIN: Min\\: %2.lf",
		"GPRINT:t1:MAX: Max\\: %2.lf",
		"GPRINT:t1:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:t1:LAST: Current\\: %2.lf\\n",
		"LINE2:t2#00FF00:Library    ",
		"GPRINT:t2:MIN: Min\\: %2.lf",
		"GPRINT:t2:MAX: Max\\: %2.lf",
		"GPRINT:t2:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:t2:LAST: Current\\: %2.lf\\n",
		"LINE2:t4#ff6eb4:Living Room",
		"GPRINT:t4:MIN: Min\\: %2.lf",
		"GPRINT:t4:MAX: Max\\: %2.lf",
		"GPRINT:t4:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:t4:LAST: Current\\: %2.lf\\n",
		"LINE2:t3#FF0000:Outside    ",
		"GPRINT:t3:MIN: Min\\: %2.lf",
		"GPRINT:t3:MAX: Max\\: %2.lf",
		"GPRINT:t3:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:t3:LAST: Current\\: %2.lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Temperature graph: $ERROR\n"; }

#----- Humidity Graph
	RRDs::graph "$img/humidity-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v Humidity",
		"DEF:h2=$rrd/environ.rrd:h2:AVERAGE",
		"DEF:h3=$rrd/environ.rrd:h3:AVERAGE",
		"LINE2:h2#0000FF:Inside ",
		"GPRINT:h2:MIN: Min\\: %2.lf",
		"GPRINT:h2:MAX: Max\\: %2.lf",
		"GPRINT:h2:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:h2:LAST: Current\\: %2.lf\\n",
		"LINE2:h3#00FF00:Outside",
		"GPRINT:h3:MIN: Min\\: %2.lf",
		"GPRINT:h3:MAX: Max\\: %2.lf",
		"GPRINT:h3:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:h3:LAST: Current\\: %2.lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Humidity graph: $ERROR\n"; }

#----- Solar Graph Inside
	RRDs::graph "$img/solarIn-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600", 
		"-a", "PNG",
		"-v Solar Rad",
		"DEF:r1=$rrd/solar1.rrd:r1:AVERAGE",
		"LINE2:r1#0000FF:Inside",
		"GPRINT:r1:MIN: Min\\: %2.lf",
		"GPRINT:r1:MAX: Max\\: %2.lf",
		"GPRINT:r1:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:r1:LAST: Current\\: %2.lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Solar graph: $ERROR\n"; }

#----- Solar Graph Outside
	RRDs::graph "$img/solar-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600", 
		"-a", "PNG",
		"-v Solar Rad",
		"DEF:r2=$rrd/solar1.rrd:r2:AVERAGE",
		"LINE2:r2#0000FF:Garden",
		"GPRINT:r2:MIN: Min\\: %2.lf",
		"GPRINT:r2:MAX: Max\\: %2.lf",
		"GPRINT:r2:AVERAGE: Avg\\: %4.1lf",
                "GPRINT:r2:LAST: Current\\: %2.lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Solar graph: $ERROR\n"; }


#----- Pressure Graph
	RRDs::graph "$img/pressure-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
	        "-u", "30.9",
	        "-l", "29",
    	        "-r",
		"-v Pressure (in Hg)",
		"DEF:p1=$rrd/pressure.rrd:p1:AVERAGE",
		"LINE2:p1#0000FF:Pressure",
		"GPRINT:p1:MIN: Min\\: %6.3lf",
		"GPRINT:p1:MAX: Max\\: %6.3lf",
		"GPRINT:p1:AVERAGE: Avg\\: %6.3lf",
                "GPRINT:p1:LAST: Current\\: %6.3lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Pressure graph: $ERROR\n"; }


#----- Lightning Graph
	RRDs::graph "$img/lightning-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
    	        "-r",
		"-v Strikes/min",
		"DEF:l1=$rrd/lightning.rrd:l1:AVERAGE",
		"CDEF:lmin=l1,60,*",
		"LINE2:lmin#0000FF:Lightning 1",
		"GPRINT:lmin:MIN: Min\\: %6.3lf",
		"GPRINT:lmin:MAX: Max\\: %6.3lf",
		"GPRINT:lmin:AVERAGE: Avg\\: %6.3lf",
                "GPRINT:lmin:LAST: Current\\: %6.3lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Lightning graph: $ERROR\n"; }

#----- Rain Graph - total
	RRDs::graph "$img/raint-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
                "-l", "0", 
		"-a", "PNG",
    	        "-r",
		"-v Rain (mm)",
		"DEF:total=$rrd/rain.rrd:total:AVERAGE",
		"CDEF:rinch=total,425,-,0.254,*",
		"LINE2:rinch#0000FF:Rain",
                "GPRINT:rinch:MIN: Min\\: %6.3lf",
                "GPRINT:rinch:MAX: Max\\: %6.3lf\\n";


	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Rain graph: $ERROR\n"; }

#----- Rain Graph - rate
	RRDs::graph "$img/rainr-$_[0].png",
		"--lazy",
		"-s -$_[0]",
		"-h", "80", "-w", "600",
		"-a", "PNG",
    	        "-r",
		"-v Rain (mm/hr)",
		"DEF:rate=$rrd/rain.rrd:rate:AVERAGE",
		"CDEF:rinch=rate,914.4,*",
		"LINE2:rinch#0000FF:Rain",
                "GPRINT:rinch:LAST: Current\\: %6.3lf\\n";

	if ($ERROR = RRDs::error) 
	{ print "$0: unable to generate Rain graph: $ERROR\n"; }

}
