#!/usr/bin/perl 

use CGI ':standard';
use DBI;
use GD;

use GD::Graph::lines;
use Image::Magick;
#use strict;

# Заносим данные.
my @data;

my $i;
my $j=0;
my $c;

for ($c=0;$c<=23;$c++){

    for ($i=0;$i<100;$i+=5){
	if ($i==0){$data[0][$j]=$c.":00";}
	if ($i>0){$data[0][$j]=$c.".".$i;}
    $j++
    }
    if ($c==23){$data[0][$j]=24;}
}

($sec,$min,$hour,$mday,$monn,$yearn,$wday,$yday,$isdst) = localtime(time());
my $dbh= DBI->connect("DBI:mysql:termo","sersql","sersql");

$maxt_p=-100;
$mint_p=100;

my $sth= $dbh->prepare("select * from  sensor1 order by id DESC limit 480");
$sth->execute();
$j=$hour*20+int($min/3);
$zag=0;$old_id=480;

while (@ar=$sth->fetchrow_array){
    if ($old_id>=@ar[3]){$old_id=@ar[3];}
    else {last;}
    $data[1][@ar[3]]=@ar[2];
    if ($zag==0) {$time_zam=@ar[1];$zag=1;$term_zag=@ar[2];$term_zag=~s/(.?\d+\.\d\d)(.*)/$1/i;}
    if (@ar[2]>$maxt_p){$maxt_p=@ar[2];}
    if (@ar[2]<$mint_p){$mint_p=@ar[2];}
    
}


$sth= $dbh->prepare("select * from  sensor4 order by id DESC limit 480");
$sth->execute();
$zag=0;$maxt_p2=-100;$mint_p2=100;$old_id=480;
while (@ar2=$sth->fetchrow_array){
    if ($old_id>=@ar2[3]){$old_id=@ar2[3];}
    else {last;}
    $data[2][@ar2[3]]=@ar2[2];
    if ($zag==0) {$zag=1;$term_zag2=@ar2[2];$term_zag2=~ s/(.?\d+\.\d\d)(.*)/$1/i;}
    if (@ar2[2]>$maxt_p2){$maxt_p2=@ar2[2];}
    if (@ar2[2]<$mint_p2){$mint_p2=@ar2[2];}
 }

if ($maxt_p>$maxt_p2){$maxt=int($maxt_p)+1;}
else {$maxt=int($maxt_p2)+1;}
if ($maxt<1){$maxt=1;}

if ($mint_p<$mint_p2){$mint=int($mint_p)-1;}
else {$mint=int($mint_p2)-1;}
if ($mint>-1){$mint=-1;}

if ((($mint+$maxt)/2)==int(($mint+$maxt)/2)){$maxt++;}

$y_tick_number=(abs($mint)+$maxt);
$mint_p=~ s/(.?\d+\.\d\d)(.*)/$1/i;$maxt_p=~ s/(.?\d+\.\d\d)(.*)/$1/i;
$mint_p2=~ s/(.?\d+\.\d\d)(.*)/$1/i;$maxt_p2=~ s/(.?\d+\.\d\d)(.*)/$1/i;

my $mygraph = GD::Graph::lines->new(900, 300);
$mygraph->set(
    x_label     => "Время                        &#169; Ser kandalaksha.info",
    y_label     => "Температура воздуха  С&#176;",
    title       => $time_zam."  Температура воздуха: ".$term_zag ."&#176;"." / ".$term_zag2 ."&#176;",
    bar_width   => 1,
    # Sepearte the bars with 4 pixels
    bar_spacing => 1,
    # Показывать сетку
    long_ticks  => 1,
    y_max_value => $maxt,
    y_min_value => $mint,
   y_tick_number => $y_tick_number,
    box_axis => 0,
    x_label_skip=>20,
#    zero_axis_only => 1,
    # Показывать значения над столбиками
    show_values => 0,
    # Делаем линии сплошной (solid), пунктирной (dashed), и штрих-точкой (dotted-dashed)
    line_types  => [1,1],
    # Задаем толщину линий
    line_width  => 3,
    # Задаем цвета
    dclrs       => ['red','#990099'],
    transparent => 1,
    legendclr     => '#000000',   # Text for the legend


    # bgclr         => 'white',   # background colour
    #fgclr         => 'dblue',   # Axes and grid
#    boxclr        => 'red',     # Fill colour for box axes, default: not used
#    accentclr     => 'red',    # bar, area and pie outlines.

#    labelclr      => 'red',   # labels on axes
#    axislabelclr  => 'red',   # values on axes
#    legendclr     => 'red',   # Text for the legend
    textclr       => '#555555',   # All text, apart from the following 2
#
#    valuesclr     => 'red',   # values printed above the points
# data set colours
#    dclrs => [qw(lred lgreen lblue lyellow lpurple cyan lorange)],




#     x_label_position => 1,
) or warn $mygraph->error;


#$mygraph->set_legend_font(GD::gdMediumBoldFont,25);
$mygraph->set_legend_font('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 9);
$mygraph->set_title_font('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 18);
$mygraph->set_legend('Температура воздуха C&#176;                  '."Максимальная: ".$maxt_p."C&#176;   Минимальная: ".$mint_p."C&#176;","Температура воздуха C&#176; (датчик №2)  "." Максимальная: ".$maxt_p2."C&#176;   Минимальная: ".$mint_p2."C&#176;");
$mygraph->set_y_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
$mygraph->set_x_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf',9 );
$mygraph->set_y_axis_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
#$mygraph->set_y_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
$mygraph->set_x_axis_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 8);
$mygraph->set_values_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 10);

my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
open(GRAPH,">/var/www/termo/termo.png");
print GRAPH $myimage->png;
close GRAPH;

$im = GD::Image->new("/usr/lib/cgi-bin/88x31.png");
$red = $im->colorAllocate(255,0,0);
$white = $im->colorClosest(0,255,0);
$im->transparent($white);
$im->stringFT($red, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 12, 0,10, 20,"$term_zag2 C&#176;");
open(GRAPH,">/usr/lib/cgi-bin/88x31_0.png");
print GRAPH $im->png;
close GRAPH;


# Баннер GIF 100
my $sth= $dbh->prepare("select * from  sensor2 order by id DESC limit 1");
$sth->execute();
@ar=$sth->fetchrow_array;
$davl=@ar[2];
$davl=~ s/(.\d+\.\d\d)(.*)/$1/i;

$im = GD::Image->new("/usr/lib/cgi-bin/100x100.png");
#$im = newFromPng GD::Image(PNG);
$red = $im->colorAllocate(255,0,0);
$red2 = $im->colorAllocate(204,51,0);
$white = $im->colorClosest(0,255,0);
$im->transparent($white);
$im->stringFT($red, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 12, 0,12, 40, "$term_zag2 C&#176;");
$im->stringFT($red2, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 9, 0,8, 60, "$davl (mm Hg)");
open(GRAPH,">/var/www/termo/termo100.png");
print GRAPH $im->png;
close GRAPH;


#Аниммашку gif
$image = new Image::Magick;

  $image->Read('/usr/lib/cgi-bin/88x31_1.png');
  $image->Read('/usr/lib/cgi-bin/88x31_2.png');
  $image->Read('/usr/lib/cgi-bin/88x31_d.png');
  $image->Read('/usr/lib/cgi-bin/88x31_3.png');
  $image->Read('/usr/lib/cgi-bin/88x31_0.png');
  $image->Read('/usr/lib/cgi-bin/88x31_4.png');
  $image->Read('/usr/lib/cgi-bin/88x31_v.png');
  $image->Write(filename=>"/var/www/termo/termo88a.gif",delay=>"200");

