Перехват "лишнего" вывода от вызываемых подпроцессов, чтоб не засорять лог.
[weathermon.git] / web / archive.php
1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
5   <meta name="GENERATOR" content="Mozilla/4.72 (X11; U; Linux 2.2.12-20smp i686) [Netscape]">    
6   <title>WeatherMon (архив)</title>                      
7   <link rel="icon" href="favicon.png" />
8   <style type="text/css">
9     a.year   {
10         padding:20px;
11         margin: 10px;
12         border:2px solid darkgrey;
13         border-radius: 10px;
14         display:inline-block;
15         width:200px;
16         float:left;
17         font-size:100%;
18         text-align:center;
19     }                                          
20     a.month   {
21         font-size:100%;
22         text-align:center;
23     }                                          
24     a:visited { text-decoration: none; color:darkblue; }
25     a:link { text-decoration: none; color:blue; }
26     a:hover { text-decoration: underline; color:navy; }  
27   </style>
28 </head>
29 <body text="black" bgcolor="silver" link="blue" vlink="#000080" alink="#FF0000">         
30 <?php
31
32 include('config_local.php');
33 include('calendar.php');
34 include('units.php');
35
36   $client_ip = $_SERVER["REMOTE_ADDR"];
37
38   if ((strpos($client_ip, "192.168.") === 0) || (strpos($client_ip, "10.8.") === 0)
39      || (strpos($client_ip, "2a02:578:5002:8174:") === 0)
40      || (strpos($client_ip, "2a02:578:5002:196::2") === 0)) {
41
42     $local_net = True;
43
44   } else {
45
46     $local_net = False;
47
48   }
49
50
51 $year = $_REQUEST['year']; 
52 $month = $_REQUEST['month'];
53 $day = $_REQUEST['day'];
54
55 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
56   die($err);
57   }  
58
59 $db -> exec('SET CHARACTER SET utf8');
60
61 if (! $year) {
62         
63   $q = $db -> prepare(
64     'select 
65        date_format(day,\'%Y\') as year
66      from 
67        calendar
68      group by year  
69      order by year');
70   $q -> execute();
71 ?>
72 <h1>Архив метеоданных</h1>
73 <?php
74   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
75 ?> 
76   <a class="year" href="?year=<?php echo $row['year']; ?>">Данные за <?php echo $row['year']; ?> год</a>
77 <?php
78   }
79       
80 } else {
81
82   if (! $month) {
83       
84     $next_year = $year+1;
85     $year      = sprintf('%04d',$year);
86     $next_year = sprintf('%04d',$next_year);
87     $q = $db -> prepare(
88           'select 
89              date_format(day,\'%m\') as month,
90              date_format(day,\'%d\') as day
91            from 
92              calendar
93            where 
94              day>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
95              day<str_to_date(\''.$next_year.'-01-01\',\'%Y-%m-%d\')
96            group by month,day
97            order by month,day'
98           );
99     $q -> execute();
100 ?>
101 <h1 align="center"><?php echo $year; ?> год</h1>
102 <?php
103     $data = $q -> fetchAll(PDO::FETCH_ASSOC);
104     $months = [];
105     foreach ($data as $row) {
106       $days [$year.$row['month'].$row['day']] = '&month='.$row['month'].'&day='.$row['day'];
107       $months[$row['month']]=1;
108     }
109     $m = 1;
110     echo '<table align="center">';
111     for ($i=1; $i<=3; $i++) {
112       echo '<tr>';
113       for ($j=1; $j<=4; $j++) {
114         echo '<td valign="top">';
115         $month=sprintf('%02d',$m);
116         $month_name = strftime('%B',mktime(0,0,0,$month,1,$year));
117         if (!empty($months[$month])) {
118           echo '<h3 align="center"><a class="month" href="?year='.$year.'&month='.$month.'">'.$month_name.'</a></h3>';
119         } else {
120           echo '<h3 align="center">'.$month_name.'</h3>';
121         }
122         calendar($year,$month,$days,'?year='.$year.'%s',0);
123         echo '</td>';
124         $m++;
125         }
126       echo '</tr>';
127     }
128     echo '</table>';
129     
130     $next_year = $year+1;
131
132     if (!$local_net) {
133
134       $filter = ' and s.is_public=1';
135   
136     } else {
137   
138       $filter = '';
139     
140     }  
141     
142     
143
144     $q = $db -> prepare (
145       'select 
146          s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
147          count(distinct day) cnt,
148          round(min(v.min),1) min_value,
149          round(max(v.max),1) max_value,
150          u.name_short unit
151        from
152          sensors_ranges v,sensors s,st_parameters p,units u
153        where 
154          v.sensor=s.id and
155          v.parameter=p.id and
156          p.st_unit=u.id and
157          v.day>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
158          v.day<str_to_date(\''.$next_year.'-01-01\',\'%Y-%m-%d\''.$filter.')
159       group by s.id,p.id,s.s_description,p.st_description
160       order by s.id,p.id'
161       );
162         
163     $q -> execute();
164     $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
165       
166     foreach ($sensors as $sensor) {
167
168       $units = get_unit($db,$sensor['param']);
169       $from_unit = $units['from'];
170       $to_unit = $units['to'];
171       $param_unit = $units['name'];
172
173       $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
174       $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
175      
176       echo '<h3 align="center">'.$sensor['param_name'].'('.$sensor['sensor_name'].')</h3>';
177       echo '<table align="center">';
178       echo '<tr><td>';
179       echo 'Минимальное значение за год <b>'.$min_val.' '.$param_unit.'</b><br>';
180       echo 'Максимальное значение за год <b>'.$max_val.' '.$param_unit.'</b><br>';
181       echo '</td></tr>';
182       echo '</table>';
183
184       if ($sensor['cnt']>1) {
185         
186         echo '<center><img src="image_minmax.php?sensor='.$sensor['sensor'].'&param='.$sensor['param'].
187           '&type=year&year='.$year.'"></center>';
188         
189       }
190       
191     }
192
193
194   } else {
195       
196     if (!$day) {
197       
198       $next_year = $year;
199       $next_month = $month+1;
200
201       if ($next_month == 13) {
202         $next_month = 1;
203         $next_year = $next_year+1;
204       }
205
206       $next_month = sprintf('%02d',$next_month); 
207       $next_year  = sprintf('%04d',$next_year);
208       $month      = sprintf('%02d',$month);
209       $year      = sprintf('%04d',$year);
210
211       if (!$local_net) {
212
213         $filter = ' and s.is_public=1';
214   
215       } else {
216   
217         $filter = '';
218     
219       }  
220     
221
222       $q = $db -> prepare( 
223           'select  
224              date_format(day,\'%d\') as day
225            from 
226              sensors_ranges r,sensors s 
227            where 
228              s.id=r.sensor and
229              day>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
230              day<str_to_date(\''.$next_year.'-'.$next_month.'-01\',\'%Y-%m-%d\')'.$filter.'
231            group by day
232            order by day'
233           );
234       $q -> execute();
235       
236       $data = $q -> fetchAll(PDO::FETCH_ASSOC);
237
238       $month_name = strftime('%B %Y',mktime(0,0,0,$month,1,$year));
239
240 ?>
241 <h1 align="center"><?php echo 'Данные за '.$month_name; ?></h1>
242 <?php          
243       foreach ($data as $row) {
244         $days [$year.$month.$row['day']] = $row['day'];
245       }
246
247       calendar($year,$month,$days,'?year='.$year.'&month='.$month.'&day=%s',3);
248              
249       if (!$local_net) {
250       
251         $filter = ' and s.is_public=1';
252                 
253       } else {
254                         
255         $filter = '';
256                                     
257       }
258                                                        
259               
260       $q = $db -> prepare (
261           'select 
262              s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
263              count(distinct day) cnt,
264              round(min(v.min),1) min_value,
265              round(max(v.max),1) max_value,
266              u.name_short unit
267            from
268              sensors_ranges v,sensors s,st_parameters p,units u
269            where 
270              v.sensor=s.id and
271              v.parameter=p.id and
272              p.st_unit=u.id and
273              v.day>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
274              v.day<str_to_date(\''.$next_year.'-'.$next_month.'-01\',\'%Y-%m-%d\')'.$filter.'
275            group by s.id,p.id,s.s_description,p.st_description
276            order by s.id,p.id'
277         );
278         
279       $q -> execute();
280       $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
281       
282       foreach ($sensors as $sensor) {
283
284         $units = get_unit($db,$sensor['param']);
285         $from_unit = $units['from'];
286         $to_unit = $units['to'];
287         $param_unit = $units['name'];
288
289         $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
290         $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
291       
292         echo '<h3 align="center">'.$sensor['param_name'].'('.$sensor['sensor_name'].')</h3>';
293         echo '<table align="center">';
294         echo '<tr><td>';
295         echo 'Минимальное значение за месяц <b>'.$min_val.' '.$param_unit.'</b><br>';
296         echo 'Максимальное значение за месяц <b>'.$max_val.' '.$param_unit.'</b><br>';
297         echo '</td></tr>';
298         echo '</table>';
299
300         if ($sensor['cnt']>1) {
301         
302           echo '<center><img src="image_minmax.php?sensor='.$sensor['sensor'].'&param='.$sensor['param'].
303             '&type=month&year='.$year.'&month='.$month.'"></center>';
304         
305         }
306       
307       }
308
309     } else {
310
311       $month      = sprintf('%02d',$month);
312       $year      = sprintf('%04d',$year);
313       $day       = sprintf('%02d',$day);
314
315       if (!$local_net) {
316
317         $filter = ' and s.is_public=1';
318   
319       } else {
320   
321         $filter = '';
322     
323       }
324
325
326       $q = $db -> prepare(
327             'select 
328                 distinct v.sensor as sensor_id,s.s_description,p.id as param_id,p.st_description 
329              from 
330                 sensors_ranges v,st_parameters p,sensors s 
331              where 
332                 v.day=str_to_date(\''.$year.'-'.$month.'-'.$day.'\',\'%Y-%m-%d\')
333                 and v.sensor=s.id 
334                 and v.parameter=p.id'.$filter
335             );
336       $q -> execute();
337
338       while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
339
340         echo '<h3 align="center">'.$row['s_description'].'/'.$row['st_description'].'</h3>';
341 ?>
342 <center><img src="image.php?sensor=<?php echo $row['sensor_id']; ?>&param=<?php echo $row['param_id']?>&type=range&fromdate=<?php echo $year.$month.$day.'000000'; ?>&todate=<?php echo $year.$month.$day.'235959'?>"></center>
343 <?php  
344
345       }                                                            
346     }
347   }
348 }
349 ?>
350 </body>
351