92a8e8e6a2dd384896fa8aa01311dcbee353680a
[weathermon.git] / web / index.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 http-equiv="Refresh" content="60"> 
6   <meta name="GENERATOR" content="Mozilla/4.72 (X11; U; Linux 2.2.12-20smp i686) [Netscape]">    
7   <title>WeatherMon (Последние 24 часа)</title>                      
8   <link rel="icon" href="favicon.png" />
9   <style type="text/css">
10      a:visited { text-decoration: none; color:darkblue; }
11      a:link { text-decoration: none; color:blue; }
12      a:hover { text-decoration: underline; color:navy; }
13      .header { font-size: 16; float: left; margin: 10px;}
14      .timestamp { font-size: 16; margin: 10px;}
15      .value { font-size: 60; margin: 10px; }
16      .block { float: left; margin: 20px; }
17      .container { clear: both; }
18      .footer { clear: both; margin: 20px; font-size: 20; }
19   </style>
20 </head>
21 <body>         
22 <div class="container">
23 <?php
24
25   $client_ip = $_SERVER["REMOTE_ADDR"];
26
27   if ((strpos($client_ip, "192.168.") === 0) || (strpos($client_ip, "10.8.") === 0)
28      || (strpos($client_ip, "2a02:578:5002:8174:") === 0)
29      || (strpos($client_ip, "2a02:578:5002:196::2") === 0)) {
30    
31     $local_net = True;
32             
33   } else {
34   
35     $local_net = False;
36   
37   }
38
39 include('config_local.php');
40 include('units.php');
41
42 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
43   die($err);
44   }  
45
46 $db -> exec('SET CHARACTER SET utf8');
47
48 if (!$local_net) {
49
50   $filter = ' and s.is_public=1'; 
51
52 } else {
53
54   $filter = '';
55
56 }
57
58 $q = $db -> prepare(
59   'select 
60     distinct v.sensor_id,s.s_description,p.id as param_id,p.st_description 
61   from 
62     sensor_values v,st_parameters p,sensors s 
63   where 
64     v.timestamp>adddate(now(), -1) 
65     and v.sensor_id=s.id
66     and v.parameter_id=p.id  
67     and s.st_id=p.st_id
68     and p.id>=0'.$filter.'
69   order by s_description,st_description'
70   );
71 $q -> execute();
72
73 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
74
75   echo '<div class="block">';
76   echo '<div class="header">'.$row['s_description'].'/'.$row['st_description'].'</div>';
77
78 $ql =  $db -> prepare(
79   '
80     SELECT unix_timestamp(timestamp) timestamp,DATE_FORMAT(timestamp,"%H:%i") printable,value
81     FROM
82       meteo.sensor_values 
83     WHERE 
84       sensor_id='.$row['sensor_id'].' and parameter_id='.$row['param_id'].' and timestamp>addtime(now(), -3600)
85     ORDER BY 
86       timestamp desc
87   '
88    );
89
90 $ql -> execute();
91
92 $printable_ts = "?";
93 $val = "?";
94
95 if ($rowl = $ql -> fetch(PDO::FETCH_ASSOC)) {
96
97   $timestamp = $rowl['timestamp'];
98   $printable_ts = $rowl['printable'];
99   $value = $rowl['value'];
100
101   $units = get_unit($db,$row['param_id']);
102   $from_unit = $units['from'];
103   $to_unit = $units['to'];
104   $param_unit = $units['name'];
105
106   $val = convert_unit($db,$value,$from_unit,$to_unit);
107                               
108 }
109                                   
110 if ($printable_ts != '?') { 
111   echo '<div class="timestamp">'.$printable_ts.'</div>';
112   echo '<div class="value">'.$val.' '.$param_unit.'</div>';
113 }
114 ?>
115 <div class="graph"><a href="image.php?sensor=<?php echo $row['sensor_id']; ?>&param=<?php echo $row['param_id']?>&type=last24"><img src="image.php?sensor=<?php echo $row['sensor_id']; ?>&param=<?php echo $row['param_id']?>&type=last24small"></a></div>
116 <?php  
117
118 echo "</div>";
119
120 }                                                            
121   
122 ?>
123 </div>
124 <div class="footer">
125 <a href="archive.php">Архивные данные</a>
126 <a href="graphs.php">Подробные графики</a>
127 <a href="setup.php">Настройки</a>
128 </div>
129 </body>
130