$line) if($line_num<1) // выборка последних N строк $t=substr($line,13,3); echo $t; } if($sqlite==1) { $q='SELECT val, max(dateof) FROM "vals" WHERE sensor_id='.$sens_id.' limit 1 '; $results = $db->query($q); $row = $results->fetchArray(); echo $row[val]; } } // прием данных с сенсора if($act=='save' && $val!='' && $sens_id!="" && $_GET["p"]==$pass) { if($text_file==1) { $fp = fopen($themp_log.$sens_id, "a"); fwrite($fp, date('ymdHis')." ".$val."\n"); fclose($fp); echo date('ymdHis')."Saved $val \n"; } if($sqlite==1) { $q="SELECT strftime('%s','now')+(3*60*60)-strftime('%s',max(dateof)) as lastsecs FROM vals WHERE sensor_id=$sens_id"; $results = $db->query($q); $row = $results->fetchArray(); echo "\n".$row[lastsecs]."=".(60*$minmins)."="; if($row[lastsecs]>60*$minmins || $row[lastsecs]=='' || $sens_id>9) { $q='INSERT INTO vals (sensor_id,val,dateof,from_ip) VALUES ('.$sens_id.','.$val.', "'.date('Y-m-d H:i:s').'","'.$_SERVER['REMOTE_ADDR'].'") '; $results = $db->query($q); echo date('ymdHis')."saved $val \n"; } else echo "not saved - too small exchange time\n"; } } // вывод изображения if($_GET["act"]=='' ) // example // http://site.name/gpio/themp.php?sensrs[0]=1&sensrs[1]=7&sensrs[2]=8&sensrs[3]=10&days=3 { if(isset($_GET['days'])) $days=$_GET["days"]; else $days=3; header ("Content-type: image/png"); $largeurImage = 600; $hauteurImage = 300; $im = ImageCreate ($largeurImage, $hauteurImage) or die ("Ошибка при создании изображения"); $blanc = ImageColorAllocate ($im, 255, 255, 255); $noir = ImageColorAllocate ($im, 0, 0, 0); $ser = ImageColorAllocate ($im, 150, 150, 150); $bleu = ImageColorAllocate ($im, 0, 0, 255); $red = ImageColorAllocate ($im, 255, 0, 0); $plus = ImageColorAllocate ($im, 255, 230, 230); $minus = ImageColorAllocate ($im, 230, 230, 255); $plus2 = ImageColorAllocate ($im, 255, 190, 190); $minus2 = ImageColorAllocate ($im, 190, 190, 255); $lins = array (ImageColorAllocate ($im, 150, 0, 0), ImageColorAllocate ($im, 0, 150, 0), ImageColorAllocate ($im, 0, 0, 150), ImageColorAllocate ($im, 150, 150, 0), ImageColorAllocate ($im, 150, 0, 150), ImageColorAllocate ($im, 0, 150, 150) ); if($text_file==1) { $themps = array(); $dates = array(); $lines = file($themp_log.$sens_id); // sort DESC $lines = array_reverse($lines); foreach ($lines as $line_num => $line) if($line_num<45) // выборка последних N строк { $themps[]=substr($line,9,3); $dates[]=substr($line,0,8); } $themps = array_reverse($themps); $dates = array_reverse($dates); $hgrads = 70; // масштаб $hmas = round($hauteurImage/$hgrads); $wmas = round(($largeurImage-20)/count($themps)); ImageFilledRectangle ($im, 10, 10, $largeurImage-10, $hauteurImage/2-10, $plus); ImageFilledRectangle ($im, 10, $hauteurImage/2-10, $largeurImage-10, $hauteurImage-10, $minus); // расуем график $i=0; $t=0; $h=0; ImageString ($im, 0, 10+($i)*$wmas , $hauteurImage-10, substr($dates[$i],0,6), $noir); foreach ($themps as &$themp) { if($i>0) if((abs(substr($dates[$i],6,2)-$h)<3)||substr($dates[$i],6,2)=="00") { if(round (( $hauteurImage/2-10-$t*$hmas + $hauteurImage/2-10-$themp*$hmas ) /2) > $hauteurImage/2-11 ) $rectcol = $minus2; else $rectcol = $plus2; ImageFilledRectangle ($im, 10+($i-1)*$wmas, round (( $hauteurImage/2-10-$t*$hmas + $hauteurImage/2-10-$themp*$hmas ) /2), 10+($i)*$wmas, $hauteurImage/2-10 , $rectcol); ImageLine ($im, 10+($i-1)*$wmas, $hauteurImage/2-10-$t*$hmas, 10+($i)*$wmas, $hauteurImage/2-10-$themp*$hmas, $bleu); } ImageString ($im, 0, 10+($i)*$wmas , $hauteurImage-20, substr($dates[$i],6,2), $noir); if($h>substr($dates[$i],6,2)) ImageString ($im, 0, 10+($i)*$wmas , $hauteurImage-10, substr($dates[$i],0,6), $noir); $t = $themp; $h=substr($dates[$i],6,2); $i++; } // ось абсцисс ImageLine ($im, 10, $hauteurImage/2-11, $largeurImage-10, $hauteurImage/2-11, $noir); for($i=-6;$i<7;$i++) { imageline($im, 10, $hauteurImage/2-10-$i*$hmas*5 , $largeurImage-10, $hauteurImage/2-10-$i*$hmas*5 , $ser); ImageString ($im, 0,11 , $hauteurImage/2-20-$i*$hmas*5, $i*5, $red); } // ось ординат ImageLine ($im, 10, 10, 10, $hauteurImage-10, $noir); ImageString ($im, 5, $largeurImage-40 ,20, round($t), $red); } if($sqlite==1) { // $sensrs=$_GET["sensrs"]; $st_q=''; $i=0; foreach ( $_GET["sensrs"] as $v) { if($i>0) $st_q.=','; $st_q.=$v; $i++; } if($i==0) $st_q='1'; $q=" SELECT sensor_id, (abs(max(val))+abs(min(val))) as zY, max(val) as maxY , min(val) as minY, strftime('%s',max(dateof)) as maxX, strftime('%s',min(dateof)) as minX, strftime('%s',max(dateof))-strftime('%s',min(dateof)) as zX, strftime('%s',dateof) -( strftime('%s','now')-(24*60*60*$days)) as actual FROM vals WHERE actual>0 and sensor_id in ($st_q)"; // echo "$q
"; $results = $db->query($q); $row = $results->fetchArray(); //group by sensor_id "; // https://bravikov.wordpress.com/2013/08/26/%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%B8-%D0%B4%D0%B0%D1%82%D1%8B-%D0%B8-%D0%B2%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%B5%D0%BC-sqlite/ // разница дат julianday(dateof)-julianday(dateof), // разница секунд strftime('%s',max(dateof))-strftime('%s',min(dateof)) $hgrads = round($row[zY]+1); // масштаб $max_val=round($row[maxY]+1); $min_val=round($row[minY]-1); $start_time=$row[minX]; $hmas = round(($hauteurImage-20)/$hgrads+0,5); // pixels in gradus Y if($row[maxY]>=0 && $row[minY]>=0) { $min_val=-1; $hgrads=$max_val+1; $hmas = round(($hauteurImage-20)/$hgrads-0,5); } if($row[maxY]<=0 && $row[minY]<=0) { $max_val=1; $hgrads=abs($min_val)+1; $hmas = round(($hauteurImage-20)/$hgrads-0,5); } if($row[maxY]>=0 && $row[minY]<=0) $hzero = 10+$hmas*$row[maxY]; // 0 on Y // $hmas = round(($hauteurImage-20)/$hgrads); // $y=$max_val-1; for($i=1;$i<$hgrads;$i++) { $y--; if($y==0) $hzero=10+$hmas*$i; } $wmas = ($largeurImage-20)/$row[zX]; // pixels in second X ImageFilledRectangle ($im, 10, 10, $largeurImage-10, $hzero, $plus); ImageFilledRectangle ($im, 10, $hzero, $largeurImage-10, $hauteurImage-10, $minus); // ось абсцисс ImageLine ($im, 10, $hzero-1, $largeurImage-10, $hzero-1, $noir); $y=$max_val-1; $peer_y=ceil($hgrads/30); $peer_y_cnt=1; for($i=1;$i<$hgrads;$i++) { $y--; if($peer_y_cnt==$peer_y) { imageline($im, 10, 10+$hmas*$i , $largeurImage-10, 10+$hmas*$i , $ser); ImageString ($im, 1,1 , $hmas*$i+5, $y, $noir); } if($y==0) imageline($im, 10, 10+$hmas*$i-1 , $largeurImage-10, 10+$hmas*$i-1 , $noir); if($peer_y_cnt<$peer_y) $peer_y_cnt++; else $peer_y_cnt=1; } // ось ординат ImageLine ($im, 10, 10, 10, $hauteurImage-10, $noir); $cn2=0; foreach ( $_GET["sensrs"] as $v) { // среднее по часам SELECT date(dateof) as Dt, strftime('%H', dateof) as Hr, avg(val) as val FROM "vals" WHERE sensor_id=1 group by Dt, Hr // graph $q="SELECT sensor_id, dateof,strftime('%s',dateof) as secs,val , dateof,strftime('%s',dateof) -( strftime('%s','now')-(24*60*60*$days)) as actual, dateof,strftime('%s',dateof) as secs, dateof,strftime('%H',dateof) as hr, dateof,strftime('%d',dateof) as dt FROM vals WHERE actual>0 and sensor_id=$v ORDER BY dateof "; $start_sec=0; $hr_show=$days; $x=0; $y=0; $dt=0; $hr=0; $results = $db->query($q); while ($row = $results->fetchArray()) { if($start_sec==0) $start_sec=$start_time;// $row[secs]; if($x<>0 && $y<>0 && $row[secs]-$t<60*60*2) ImageLine ($im, $x,$y,10+round(($row[secs]-$start_sec)*$wmas),$hzero-$row[val]*$hmas, $lins[$cn2]); $x=10+round(($row[secs]-$start_sec)*$wmas); $y=$hzero-$row[val]*$hmas; $t=$row[val]; $t=$row[secs]; if($cn2<1) { if($dt!=$row[dt]) { ImageString ($im, 0, $x , $hzero-8, $row[dt], $noir); $dt=$row[dt]; } if($hr!=$row[hr]){ $hr_show++; if($hr_show==3) { ImageString ($im, 0, $x , $hzero, $row[hr], $noir); $hr_show=1; } $hr=$row[hr]; } } } ImageString ($im, 1, $x-2 ,$y-5, round($t), $lins[$cn2]); ImageString ($im, 1, $cn2*50 ,0, "sens-$v", $lins[$cn2]); $cn2++; } } ImagePng ($im); } ?>