9b6ca84e3209b5de7b07c08e83b292488f16dc2c
[weathermon.git] / web / image.php
1 <?php // content="text/plain; charset=utf-8"
2
3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
4
5 include ('config_local.php');
6
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');
12
13 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
14
15   die('Ð\9dе Ð¼Ð¾Ð³Ñƒ Ð¿Ð¾Ð´ÐºÐ»ÑŽÑ‡Ð¸Ñ‚ÑŒÑ\81Ñ\8f Ðº Ð‘Д');
16
17 }  
18
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";
25
26 $cachefilename = NULL;
27
28 $db -> exec('SET CHARACTER SET utf8');
29   
30 $type = $_REQUEST['type'];
31 $sensor=$_REQUEST['sensor'];
32 $param=$_REQUEST['param'];
33
34 if ($type and $param) {
35   
36   $sensor = intval($sensor);
37   $param = intval($param);
38
39   $q = $db -> prepare(
40     'select s_description from sensors where id='.$sensor
41   );
42   $q -> execute();
43
44   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
45     $sensor_name = $row['s_description'];
46   }                                                                
47
48   $q = $db -> prepare(
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'
50   );
51   $q -> execute();
52
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'];
59   }                                                                
60
61   if (!empty($_COOKIE['unit_'.$unit_group])) {
62     $to_unit=intval($_COOKIE['unit_'.$unit_group]);
63   } else {
64     $to_unit=$from_unit;
65   }  
66
67   $q = $db -> prepare(
68     'select u.name_short,u.prec from units u where u.id='.$to_unit
69   );
70   $q -> execute();
71
72   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
73     $param_unit = $row['name_short'];
74     $precision = $row['prec'];
75   }                                                                
76   
77   $xdata = array();
78   $ydata = array();
79
80   if ($type == 'last24') { 
81
82     $q = $db -> prepare(
83       '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'
84     );
85
86     $bottomheight = 130;
87     $topheight = 40;
88     $sizex = 1000;
89     $sizey = 800;
90     $scale = True;
91     
92   } elseif ($type == 'last24small') {
93
94     $q = $db -> prepare(
95       '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'
96     );
97
98     $bottomheight = 20;
99     $topheight = 20;
100     $sizex = 400;
101     $sizey = 300;
102     $scale = False;
103     
104   } elseif ($type == 'range') {
105
106     $curr = intval(date('YmdHis'));
107
108     $from = intval($_REQUEST['fromdate']);
109     $to = intval($_REQUEST['todate']);
110
111     if ($curr>$to) {
112     
113         $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$from.'-'.$to.'.'.$img_format;
114     
115     }
116
117     $q = $db -> prepare(
118       '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'
119     );
120   
121     $bottomheight = 60;
122     $topheight = 40;
123     $sizex = 1000;
124     $sizey = 800;
125     $scale = True;
126   
127   }
128
129   $g = new Graph($sizex,$sizey);
130   
131   if ($cachefilename) {
132       if ($g->cache->IsValid($cachefilename)) {
133   
134           $g->cache->StreamImgFile($g->img,$cachefilename);
135           return;
136   
137       } else {
138       
139           $timeout = 8640000;
140           $g->SetupCache($cachefilename,$timeout);
141       
142       }
143   }
144
145   $q -> execute();
146     
147   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
148     
149     $xdata[] = $row['x'];
150     $ydata[] = $row['y'];
151     
152   }                                                                
153
154
155   for ($i = 0; $i < count($xdata); ++$i) {
156
157     $total_weight=0;
158     $sum=0;
159     $maxdelta = 1800;
160     
161     for ($j = $i; $j < count($xdata); ++$j) {
162     
163       $delta = abs($xdata[$i]-$xdata[$j]);
164       if ($delta > $maxdelta) { break; }
165     
166       $weight = 1-$delta/$maxdelta;
167       $total_weight += $weight;
168       $sum += $weight*$ydata[$j];
169       
170     }
171
172     for ($j = $i-1; $j >=0 ; --$j) {
173     
174       $delta = abs($xdata[$i]-$xdata[$j]);
175       if ($delta > $maxdelta) { break; }
176     
177       $weight = 1-$delta/$maxdelta;
178       $total_weight += $weight;
179       $sum += $weight*$ydata[$j];
180       
181     }
182     
183     $new_val = $sum/$total_weight;
184     $f_ydata[$i] = $new_val; 
185     
186   }
187
188   // Create the graph
189   $g->graph_theme = null;
190
191   $g->img->SetAntiAliasing();
192
193   // We need a datlin scale since we provide both
194   // x and y coordinates for the data points, but x is unix timestamp.
195   $g->SetScale('datlin');
196   if ($scale) {
197     $g->xaxis->SetLabelAngle(90);
198   } else {
199     $g->xaxis->HideLabels(True);
200   }
201   $g->xaxis->SetPos("min");
202   $g->yaxis->SetLabelFormat("%0.".$precision."f");
203 #  $g->xaxis->scale->SetTimeAlign( HOURADJ_1 );
204
205
206   if ($type!="last24small") {
207
208     // We use a scatterplot to illustrate the original
209     // contro points.
210     $splot = new ScatterPlot($ydata,$xdata);
211     $g->Add($splot);
212
213     // 
214     $splot->mark->SetFillColor($dot_color);
215     $splot->mark->SetColor($dot_color);
216     $splot->mark->SetType(MARK_FILLEDCIRCLE);
217     $splot->mark->SetSize(2);
218
219   }
220
221   $fplot = new LinePlot($f_ydata,$xdata);
222   $g->Add($fplot);
223   $fplot->SetColor($line_color);
224   $fplot->SetWeight(2);
225
226   $g->SetMargin(50,30,$topheight,$bottomheight);
227   if ($scale) {
228     $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
229     $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
230   }  
231   $g->SetMarginColor('lightgray');
232
233   $g->xgrid->Show();
234   $g->xgrid->SetLineStyle('dotted');
235   $g->ygrid->Show();
236   $g->ygrid->SetLineStyle('dotted');
237
238   // Add the plots to the graph and stroke
239   $g->Stroke();
240   
241 } else {
242
243   header("Content-Type: text/html; charset=UTF-8");
244   die('СенÑ\81ор Ð½Ðµ Ð²Ñ‹Ð±Ñ€Ð°Ð½!');
245
246 }  
247
248 ?>