#!/usr/bin/perl

use DBI;
use GD;
use GD::Graph::lines;
use Image::Magick;


open DATA, "wget -qO- http://192.168.1.98/pagedata|";
@ant1=<DATA>;
close DATA;
#print @ant1;

#@ant1[0]=~s/(Влажность  )([+-]?\d+\.?\d*)//gis;$vlag=$2;
@ant1[0]=~s/(\|Температура )([+-]?\d+\.?\d*)ºC//gis;$temp3=$2;
@ant1[0]=~s/(Hg\|Температура )([+-]?\d+\.?\d*)//gis;$temp2=$2;
@ant1[0]=~s/(ºF\|Температура )([+-]?\d+\.?\d*)//gis;$temp1=$2;
#@ant1[0]=~s/(Давление )([+-]?\d+\.?\d*)//gis;$davl=$2;


#print "$temp $temp2 $temp3 $vlag % $davl\n";
#exit;

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

if (($temp2 > -50)&&($temp2 < 80)){ $sth= $dbh->prepare("insert into sensor5 values (0,NOW(),\"$temp2\",\"$zamer\")");}
else { $sth= $dbh->prepare("insert into sensor5 values (0,NOW(),30,\"$zamer\")");}
$sth->execute();


if (($temp3 > -50)&&($temp3 < 80)){ $sth= $dbh->prepare("insert into sensor6 values (0,NOW(),\"$temp3\",\"$zamer\")");}
else {$sth= $dbh->prepare("insert into sensor6 values (0,NOW(),0,\"$zamer\")");}

$sth->execute();


#----------------------------
my @data;
my $i;
my $j=1440;
my $c;$jnt=0;

$maxt_p=-100;$mint_p=100;

my $sth= $dbh->prepare("select * from  sensor5 order by id DESC limit 1440");
$sth->execute();

while (@ar=$sth->fetchrow_array){
    if ($j==1440){
	if (int(@ar[3]/60)!=int(@ar[3]%60)){
	    $data[0][$j]=(int(@ar[3]/60)+1).":00";
	    $j--;
    	    for ($k=0;$k<60-int(@ar[3]%60);$k++){$data[0][$j]=(int(@ar[3]/60)+1).":00";$j--;}
	}
    $jnt=$j;
    }
    $data[1][$j]=@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];}
    $data[0][$j]=int(@ar[3]/60).":00";
if ($j>0){$j--;}
}

#print $mint_p."\n".$maxt_p."\n";

$sth= $dbh->prepare("select * from  sensor6 order by id DESC limit 1440");
$sth->execute();
$zag=0;$maxt_p2=-100;$mint_p2=100;

$j=$jnt;
while (@ar2=$sth->fetchrow_array){
    $data[2][$j]=@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 ($j>0){$j--;}
}

$maxt=int($maxt_p)+1;$mint=int($mint_p)-1;

#print $mint_p2."\n".$maxt_p2."\n";


if ($maxt<$maxt_p2){$maxt=int($maxt_p2)+1;}
if ($mint>$mint_p2){$mint=int($mint_p2)-1;}

$sth= $dbh->prepare("select * from  sensor1 order by id DESC limit 480");
$sth->execute();
$zag=0;$maxt_p3=-100;$mint_p3=100;

$j=$jnt;
while (@ar2=$sth->fetchrow_array){
    $data[3][$j]=@ar2[2];
    if ($zag==0) {$zag=1;$term_zag3=@ar2[2];$term_zag3=~ s/(.?\d+\.\d\d)(.*)/$1/i;}
    if (@ar2[2]>$maxt_p3){$maxt_p3=@ar2[2];}
    if (@ar2[2]<$mint_p3){$mint_p3=@ar2[2];}
if ($j>2){$j=$j-3;}
 }

#---------------
if ($maxt<$maxt_p3){$maxt=int($maxt_p3)+1;}
if ($mint>$mint_p3){$mint=int($mint_p3)-1;}


$y_tick_number=int($maxt-$mint);

$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;
$mint_p3=~ s/(.?\d+\.?\d?\d)(.*)/$1/i;$maxt_p3=~ s/(.?\d+\.?\d?\d)(.*)/$1/i;

my $mygraph = GD::Graph::lines->new(900, 600);
$mygraph->set(
    x_label     => "Время                        &#169; Ser ",
    y_label     => "Температура С&#176;",
    title       => $time_zam."  Температура: ".$term_zag3."&#176; / ".$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=>60,
#    zero_axis_only => 1,
    # Показывать значения над столбиками
    show_values => 0,
    # Делаем линии сплошной (solid), пунктирной (dashed), и штрих-точкой (dotted-dashed)
    line_types  => [1,1],
    # Задаем толщину линий
    line_width  => 3,
    # Задаем цвета
    dclrs       => ['red','#990099','#1D52B7'],
    transparent => 1,
    legendclr     => '#000000',   # Text for the legend
    textclr       => '#555555',   # All text, apart from the following 2
) or warn $mygraph->error;

$mygraph->set_legend_font('/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf', 10);
$mygraph->set_title_font('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 18);
$mygraph->set_legend('Температура масла    C&#176; (датчик №3)   '."Максимальная: ".$maxt_p."C&#176;  Минимальная: ".$mint_p."C&#176;",
"Температура воздуха C&#176; (датчик №2)  "." Максимальная: ".$maxt_p2."C&#176;   Минимальная: ".$mint_p2."C&#176;",
"Температура воздуха C&#176; (датчик №1)  "." Максимальная: ".$maxt_p3."C&#176;   Минимальная: ".$mint_p3."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_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/ser/l3_termo.png");
print GRAPH $myimage->png;
close GRAPH;




