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'
91 } elseif ($type == 'last24small') {
94 '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'
103 } elseif ($type == 'range') {
105 $curr = intval(date('YmdHis'));
107 $from = intval($_REQUEST['fromdate']);
108 $to = intval($_REQUEST['todate']);
112 $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$from.'-'.$to.'.'.$img_format;
117 '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'
128 $g = new Graph($sizex,$sizey);
130 if ($cachefilename) {
131 if ($g->cache->IsValid($cachefilename)) {
133 $g->cache->StreamImgFile($g->img,$cachefilename);
139 $g->SetupCache($cachefilename,$timeout);
146 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
148 $xdata[] = $row['x'];
149 $ydata[] = $row['y'];
154 for ($i = 0; $i < count($xdata); ++$i) {
160 for ($j = $i; $j < count($xdata); ++$j) {
162 $delta = abs($xdata[$i]-$xdata[$j]);
163 if ($delta > $maxdelta) { break; }
165 $weight = 1-$delta/$maxdelta;
166 $total_weight += $weight;
167 $sum += $weight*$ydata[$j];
171 for ($j = $i-1; $j >=0 ; --$j) {
173 $delta = abs($xdata[$i]-$xdata[$j]);
174 if ($delta > $maxdelta) { break; }
176 $weight = 1-$delta/$maxdelta;
177 $total_weight += $weight;
178 $sum += $weight*$ydata[$j];
182 $new_val = $sum/$total_weight;
183 $f_ydata[$i] = $new_val;
188 $g->graph_theme = null;
190 $g->img->SetAntiAliasing();
192 // We need a datlin scale since we provide both
193 // x and y coordinates for the data points, but x is unix timestamp.
194 $g->SetScale('datlin');
196 $g->xaxis->SetLabelAngle(90);
198 $g->xaxis->HideLabels(True);
200 $g->xaxis->SetPos("min");
201 # $g->xaxis->scale->SetTimeAlign( HOURADJ_1 );
204 if ($type!="last24small") {
206 // We use a scatterplot to illustrate the original
208 $splot = new ScatterPlot($ydata,$xdata);
212 $splot->mark->SetFillColor($dot_color);
213 $splot->mark->SetColor($dot_color);
214 $splot->mark->SetType(MARK_FILLEDCIRCLE);
215 $splot->mark->SetSize(2);
219 $fplot = new LinePlot($f_ydata,$xdata);
221 $fplot->SetColor($line_color);
222 $fplot->SetWeight(2);
224 $g->SetMargin(50,30,$topheight,$bottomheight);
226 $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
227 $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
229 $g->SetMarginColor('lightgray');
232 $g->xgrid->SetLineStyle('dotted');
234 $g->ygrid->SetLineStyle('dotted');
236 // Add the plots to the graph and stroke
241 header("Content-Type: text/html; charset=UTF-8");
242 die('Сенсор не выбран!');