1) Изменен Arduino-скетч для работы на Arduino Yun и более эффективной обработки...
[weathermon.git] / web / image.php
1 <?php // content="text/plain; charset=utf-8"
2
3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
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('Не могу подключиться к БД');
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 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   }                                                                
75   
76   $xdata = array();
77   $ydata = array();
78
79   if ($type == 'last24') { 
80
81     $q = $db -> prepare(
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'
83     );
84
85     $height = 130;
86     
87   } elseif ($type == 'range') {
88
89     $curr = intval(date('YmdHis'));
90
91     $from = intval($_REQUEST['fromdate']);
92     $to = intval($_REQUEST['todate']);
93
94     if ($curr>$to) {
95     
96         $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$from.'-'.$to.'.'.$img_format;
97     
98     }
99
100     $q = $db -> prepare(
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'
102     );
103   
104     $height = 60;
105   
106   }
107
108   $g = new Graph(640,480);
109   
110   if ($cachefilename) {
111       if ($g->cache->IsValid($cachefilename)) {
112   
113           $g->cache->StreamImgFile($g->img,$cachefilename);
114           return;
115   
116       } else {
117       
118           $timeout = 8640000;
119           $g->SetupCache($cachefilename,$timeout);
120       
121       }
122   }
123
124   $q -> execute();
125     
126   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
127     
128     $xdata[] = $row['x'];
129     $ydata[] = $row['y'];
130     
131   }                                                                
132
133
134   for ($i = 0; $i < count($xdata); ++$i) {
135
136     $total_weight=0;
137     $sum=0;
138     $maxdelta = 900;
139     
140     for ($j = $i; $j < count($xdata); ++$j) {
141     
142       $delta = abs($xdata[$i]-$xdata[$j]);
143       if ($delta > $maxdelta) { break; }
144     
145       $weight = 1-$delta/$maxdelta;
146       $total_weight += $weight;
147       $sum += $weight*$ydata[$j];
148       
149     }
150
151     for ($j = $i-1; $j >=0 ; --$j) {
152     
153       $delta = abs($xdata[$i]-$xdata[$j]);
154       if ($delta > $maxdelta) { break; }
155     
156       $weight = 1-$delta/$maxdelta;
157       $total_weight += $weight;
158       $sum += $weight*$ydata[$j];
159       
160     }
161     
162     $new_val = $sum/$total_weight;
163     $f_ydata[$i] = $new_val; 
164     
165   }
166
167   // Create the graph
168   $g->graph_theme = null;
169
170   //$g->img->SetAntiAliasing();
171
172   // We need a linlin scale since we provide both
173   // x and y coordinates for the data points.
174   $g->SetScale('datlin');
175   $g->xaxis->SetLabelAngle(90);
176   $g->xaxis->SetPos("min");
177
178   // We use a scatterplot to illustrate the original
179   // contro points.
180   $splot = new ScatterPlot($ydata,$xdata);
181   $g->Add($splot);
182
183   // 
184   $splot->mark->SetFillColor($dot_color);
185   $splot->mark->SetColor($dot_color);
186   $splot->mark->SetType(MARK_FILLEDCIRCLE);
187   $splot->mark->SetSize(2);
188
189   $fplot = new LinePlot($f_ydata,$xdata);
190   $g->Add($fplot);
191   $fplot->SetColor($line_color);
192   $fplot->SetWeight(2);
193
194   $g->SetMargin(50,30,40,$height);
195   $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
196   $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
197   $g->SetMarginColor('silver');
198
199   // Add the plots to the graph and stroke
200   $g->Stroke();
201   
202 } else {
203
204   header("Content-Type: text/html; charset=UTF-8");
205   die('Сенсор не выбран!');
206
207 }  
208
209 ?>