1 <?php // content="text/plain; charset=utf-8"
 
   3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
 
   5 include ('config_local.php');
 
   7 require_once ('jpgraph/jpgraph.php');
 
   8 require_once ('jpgraph/jpgraph_line.php');
 
   9 require_once ('jpgraph/jpgraph_date.php');
 
  10 require_once ('jpgraph/jpgraph_scatter.php');
 
  11 require_once ('jpgraph/jpgraph_regstat.php');
 
  13 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
 
  15   die('Не могу подключиться к БД');
 
  19 $supported = imagetypes();
 
  20 if( $supported & IMG_PNG )    $img_format="png";
 
  21 elseif( $supported & IMG_GIF ) $img_format="gif";
 
  22 elseif( $supported & IMG_JPG ) $img_format="jpeg";
 
  23 elseif( $supported & IMG_WBMP ) $img_format="wbmp";
 
  24 elseif( $supported & IMG_XPM ) $img_format="xpm";
 
  26 $cachefilename = NULL;
 
  28 $db -> exec('SET CHARACTER SET utf8');
 
  30 $type = $_REQUEST['type'];
 
  31 $sensor=$_REQUEST['sensor'];
 
  32 $param=$_REQUEST['param'];
 
  34 if ($type and $param) {
 
  36   $sensor = intval($sensor);
 
  37   $param = intval($param);
 
  40     'select s_description from sensors where id='.$sensor
 
  44   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  45     $sensor_name = $row['s_description'];
 
  49     'select st.st_dot_color,st.st_line_color,st.st_description,u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id'
 
  53   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  54     $param_name = $row['st_description'];
 
  55     $from_unit = $row['id'];
 
  56     $unit_group = $row['unit_group'];
 
  57     $dot_color = $row['st_dot_color'];
 
  58     $line_color = $row['st_line_color'];
 
  61   if (!empty($_COOKIE['unit_'.$unit_group])) {
 
  62     $to_unit=intval($_COOKIE['unit_'.$unit_group]);
 
  68     'select u.name_short from units u where u.id='.$to_unit
 
  72   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  73     $param_unit = $row['name_short'];
 
  79   if ($type == 'last24') { 
 
  82       'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>adddate(now(), -1) and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
 
  87   } elseif ($type == 'range') {
 
  89     $curr = intval(date('YmdHis'));
 
  91     $from = intval($_REQUEST['fromdate']);
 
  92     $to = intval($_REQUEST['todate']);
 
  96         $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$from.'-'.$to.'.'.$img_format;
 
 101       'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>=str_to_date("'.$from.'","%Y%m%d%H%i%s") and timestamp<=str_to_date("'.$to.'","%Y%m%d%H%i%s") and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
 
 108   $g = new Graph(640,480);
 
 110   if ($cachefilename) {
 
 111       if ($g->cache->IsValid($cachefilename)) {
 
 113           $g->cache->StreamImgFile($g->img,$cachefilename);
 
 119           $g->SetupCache($cachefilename,$timeout);
 
 126   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
 128     $xdata[] = $row['x'];
 
 129     $ydata[] = $row['y'];
 
 134   for ($i = 0; $i < count($xdata); ++$i) {
 
 140     for ($j = $i; $j < count($xdata); ++$j) {
 
 142       $delta = abs($xdata[$i]-$xdata[$j]);
 
 143       if ($delta > $maxdelta) { break; }
 
 145       $weight = 1-$delta/$maxdelta;
 
 146       $total_weight += $weight;
 
 147       $sum += $weight*$ydata[$j];
 
 151     for ($j = $i-1; $j >=0 ; --$j) {
 
 153       $delta = abs($xdata[$i]-$xdata[$j]);
 
 154       if ($delta > $maxdelta) { break; }
 
 156       $weight = 1-$delta/$maxdelta;
 
 157       $total_weight += $weight;
 
 158       $sum += $weight*$ydata[$j];
 
 162     $new_val = $sum/$total_weight;
 
 163     $f_ydata[$i] = $new_val; 
 
 168   $g->graph_theme = null;
 
 170   $g->img->SetAntiAliasing();
 
 172   // We need a datlin scale since we provide both
 
 173   // x and y coordinates for the data points, but x is unix timestamp.
 
 174   $g->SetScale('datlin');
 
 175   $g->xaxis->SetLabelAngle(90);
 
 176   $g->xaxis->SetPos("min");
 
 177   $g->xaxis->scale->SetTimeAlign( HOURADJ_1 );
 
 179   // We use a scatterplot to illustrate the original
 
 181   $splot = new ScatterPlot($ydata,$xdata);
 
 185   $splot->mark->SetFillColor($dot_color);
 
 186   $splot->mark->SetColor($dot_color);
 
 187   $splot->mark->SetType(MARK_FILLEDCIRCLE);
 
 188   $splot->mark->SetSize(2);
 
 190   $fplot = new LinePlot($f_ydata,$xdata);
 
 192   $fplot->SetColor($line_color);
 
 193   $fplot->SetWeight(2);
 
 195   $g->SetMargin(50,30,40,$height);
 
 196   $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
 
 197   $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
 
 198   $g->SetMarginColor('silver');
 
 201   $g->xgrid->SetLineStyle('dotted');
 
 203   $g->ygrid->SetLineStyle('dotted');
 
 205   // Add the plots to the graph and stroke
 
 210   header("Content-Type: text/html; charset=UTF-8");
 
 211   die('Сенсор не выбран!');