3 $query=$_REQUEST['query'];
5 $client_ip = $_SERVER["REMOTE_ADDR"];
7 include('config_local.php');
9 function startsWith($haystack, $needle)
11 $length = strlen($needle);
12 return (substr($haystack, 0, $length) === $needle);
15 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
19 $db -> exec('SET CHARACTER SET utf8');
21 $auth_token = $_COOKIE["auth-token"];
27 SELECT COUNT(*) AS auth FROM tokens WHERE str=:s and expires>now()
30 $q = $db -> prepare( $sql );
31 $q -> bindParam(':s',$auth_token,PDO::PARAM_INT);
36 $row = $q -> fetch(PDO::FETCH_ASSOC);
41 if ($auth || (strpos($client_ip, "192.168.") === 0) || (strpos($client_ip, "10.8.") === 0)) {
44 $filter = ' and s.is_public=1';
47 $hash = md5($_SERVER['QUERY_STRING'].":".$filter);
49 $redis->pconnect('127.0.0.1', 6379);
50 $results = $redis->get('meteo-'.$hash);
53 $results = unserialize($results);
54 print(json_encode($results));
59 function getYears($db) {
62 SELECT DISTINCT DATE_FORMAT(day,'%Y') y FROM sensors_ranges ORDER BY y DESC
65 $q = $db -> prepare( $sql );
70 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
71 array_push($res, $row['y']);
78 function getMonths($db,$year) {
83 SELECT DISTINCT DATE_FORMAT(day,'%m') m FROM sensors_ranges WHERE day>=STR_TO_DATE('".strval($y)."-01-01','%Y-%m-%d') and day<STR_TO_DATE('".strval($y+1)."-01-01','%Y-%m-%d') ORDER BY m DESC
86 $q = $db -> prepare( $sql );
91 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
92 array_push($res, $row['m']);
99 function getDays($db,$year,$month) {
105 SELECT DISTINCT DATE_FORMAT(day,'%d') d FROM sensors_ranges WHERE day>=STR_TO_DATE('".strval($y)."-".strval($m)."-01','%Y-%m-%d') and DATE_ADD(STR_TO_DATE('".strval($y)."-".strval($m)."-01','%Y-%m-%d'), INTERVAL 1 MONTH) ORDER BY d DESC
108 $q = $db -> prepare( $sql );
113 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
114 array_push($res, $row['d']);
121 function getSensors($db,$year,$month,$day) {
131 CONCAT(s_id,'.',t.st_abbr,'.',p.st_name) id
133 sensors_ranges r, sensors s, sensor_types t, st_parameters p
135 r.sensor=s.id and r.parameter=p.id and s.st_id=t.id and p.st_id=t.id ".$filter."
136 and r.day=STR_TO_DATE('".strval($y)."-".strval($m)."-".strval($d)."','%Y-%m-%d')
139 $q = $db -> prepare( $sql );
144 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
145 array_push($res, $row['id']);
152 function getCurrent($db,$id,$type,$param) {
157 SELECT s.id s_id,p.id p_id
158 FROM sensors s,sensor_types t, st_parameters p
159 WHERE s.st_id=t.id and p.st_id=t.id and s_id=:id and t.st_abbr=:type and p.st_name=:param ".$filter."
162 $q = $db -> prepare( $sql );
163 $q -> bindParam(':id',$id,PDO::PARAM_STR);
164 $q -> bindParam(':type',$type,PDO::PARAM_STR);
165 $q -> bindParam(':param',$param,PDO::PARAM_STR);
167 $sensor = $q -> fetch(PDO::FETCH_ASSOC);
171 u.id stored_unit,du.id display_unit
173 sensors s,sensor_types t,st_parameters p,units u,units du
174 WHERE s.st_id=t.id and p.st_id=t.id and p.st_unit=u.id and u.unit_group=du.unit_group and du.is_default=1 and s.id=:id and p.id=:param
177 $q = $db -> prepare( $sql );
178 $q -> bindParam(':id',$sensor['s_id'],PDO::PARAM_INT);
179 $q -> bindParam(':param',$sensor['p_id'],PDO::PARAM_INT);
181 $units = $q -> fetch(PDO::FETCH_ASSOC);
185 CONCAT(subset.t,'5:00') t,UnitConv(avg(subset.y),:stored,:display) y
188 substr(date_format(timestamp,'%Y-%m-%dT%H:%i'),1,15) t,value y
192 sensor_id = :id and parameter_id=:param
193 and timestamp>adddate(now(),-1)
199 $q = $db -> prepare( $sql );
200 $q -> bindParam(':id',$sensor['s_id'],PDO::PARAM_INT);
201 $q -> bindParam(':param',$sensor['p_id'],PDO::PARAM_INT);
202 $q -> bindParam(':stored',$units['stored_unit'],PDO::PARAM_INT);
203 $q -> bindParam(':display',$units['display_unit'],PDO::PARAM_INT);
206 return $q -> fetchAll(PDO::FETCH_ASSOC);
210 function getArchive($db,$year,$month,$day,$id,$type,$param) {
218 $date = strval($y).'-'.strval($m).'-'.strval($d);
221 SELECT s.id s_id,p.id p_id
222 FROM sensors s,sensor_types t, st_parameters p
223 WHERE s.st_id=t.id and p.st_id=t.id and s_id=:id and t.st_abbr=:type and p.st_name=:param ".$filter."
226 $q = $db -> prepare( $sql );
227 $q -> bindParam(':id',$id,PDO::PARAM_STR);
228 $q -> bindParam(':type',$type,PDO::PARAM_STR);
229 $q -> bindParam(':param',$param,PDO::PARAM_STR);
231 $sensor = $q -> fetch(PDO::FETCH_ASSOC);
235 u.id stored_unit,du.id display_unit
237 sensors s,sensor_types t,st_parameters p,units u,units du
238 WHERE s.st_id=t.id and p.st_id=t.id and p.st_unit=u.id and u.unit_group=du.unit_group and du.is_default=1 and s.id=:id and p.id=:param
241 $q = $db -> prepare( $sql );
242 $q -> bindParam(':id',$sensor['s_id'],PDO::PARAM_INT);
243 $q -> bindParam(':param',$sensor['p_id'],PDO::PARAM_INT);
245 $units = $q -> fetch(PDO::FETCH_ASSOC);
249 CONCAT(subset.t,'5:00') t,UnitConv(avg(subset.y),:stored,:display) y
252 substr(date_format(timestamp,'%Y-%m-%dT%H:%i'),1,15) t,value y
256 sensor_id = :id and parameter_id=:param
257 and timestamp>=STR_TO_DATE(:d,'%Y-%m-%d')
258 and timestamp<DATE_ADD(STR_TO_DATE(:d,'%Y-%m-%d'), interval 1 day)
264 $q = $db -> prepare( $sql );
265 $q -> bindParam(':id',$sensor['s_id'],PDO::PARAM_INT);
266 $q -> bindParam(':param',$sensor['p_id'],PDO::PARAM_INT);
267 $q -> bindParam(':d',$date,PDO::PARAM_STR);
268 $q -> bindParam(':stored',$units['stored_unit'],PDO::PARAM_INT);
269 $q -> bindParam(':display',$units['display_unit'],PDO::PARAM_INT);
272 return $q -> fetchAll(PDO::FETCH_ASSOC);
276 function getProps($db, $localNet) {
282 CONCAT(s_id,'.',t.st_abbr,'.',p.st_name) sensor_id,
283 p.st_description name,
287 p.st_line_color color
289 sensors s, sensor_types t, st_parameters p,units u,units du,places pl
291 s.st_id=t.id and p.st_id=t.id and p.st_unit=u.id and u.unit_group=du.unit_group and du.is_default=1 and pl.idplaces=s.place_id ".$filter."
294 $q = $db -> prepare( $sql );
303 "axes" => [ "color" => "black", "size" => 16, "style" => "normal" ],
304 "legend" => [ "color" => "black", "size" => 16, "style" => "normal"
307 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
308 $reply["names"][$row["sensor_id"]] = $row["name"];
309 $reply["colors"][$row["sensor_id"]] = $row["color"];
310 $reply["units"][$row["sensor_id"]] = $row["unit"];
311 $reply["places"][$row["sensor_id"]] = $row["place_name"];
312 $reply["scale"][$row["sensor_id"]] = [ 0 => 1.0, 1 => $row["prec"] ];
319 function getState($db, $localNet) {
328 v.sensor as sensor_int_id,
329 pl.place_name s_description,
331 p.st_name as param_name,
337 sensors_ranges v,st_parameters p,sensors s,places pl,sensor_types st,units u,units du
339 v.timestamp>addtime(now(), -43200)
346 and s.place_id=pl.idplaces
348 and u.unit_group=du.unit_group
352 s_description,sensor_id,param_id
355 $q = $db -> prepare( $sql );
360 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
362 $sensor_id = $row['sensor_id'];
363 $st_id = $row['st_abbr'];
364 $sensor_int_id = $row['sensor_int_id'];
365 $param_id = $row['param_name'];
366 $param_int_id = $row['param_id'];
367 $unit_id = $row['unit_id'];
368 $stored_unit_id = $row['stored_unit_id'];
369 $place_description = $row['s_description'];
372 SELECT UnitConv(value,".$stored_unit_id.",".$unit_id.") as val,timestamp
376 sensor_id = ".$sensor_int_id." and parameter_id=".$param_int_id."
382 $qv = $db -> prepare( $sql_last_val );
385 $v_row = $qv -> fetch(PDO::FETCH_ASSOC);
387 $value = $v_row["val"];
388 $timestamp = $v_row["timestamp"];
390 if (! array_key_exists($place_description,$reply)) {
391 $reply[$place_description] = [];
394 if (! array_key_exists($sensor_id,$reply[$place_description])) {
395 $reply[$place_description][$sensor_id] = [];
398 if (! array_key_exists($st_id,$reply[$place_description][$sensor_id])) {
399 $reply[$place_description][$sensor_id][$st_id] = [];
402 $reply[$place_description][$sensor_id][$st_id][$param_id] = $value;
403 $reply[$place_description][$sensor_id]['timestamp'] = $timestamp;
413 if ($query == 'props') {
415 $reply = getProps($db, $local_net);
418 } elseif ($query == 'state') {
420 $reply = getState($db, $local_net);
422 } elseif (startsWith($query,'get/')) {
424 $sensor = explode('/',substr($query,strlen('get/')));
425 $reply = getCurrent($db,$sensor[0],$sensor[1],$sensor[2]);
427 } elseif ($query == 'years') {
429 $reply = getYears($db);
432 } elseif (startsWith($query,'months/')) {
434 $date = explode('/',substr($query,strlen('months/')));
435 $reply = getMonths($db,$date[0]);
438 } elseif (startsWith($query,'days/')) {
440 $date = explode('/',substr($query,strlen('days/')));
441 $reply = getDays($db,$date[0],$date[1]);
444 } elseif (startsWith($query,'sensors/')) {
446 $date = explode('/',substr($query,strlen('sensors/')));
447 $reply = getSensors($db,$date[0],$date[1],$date[2]);
450 } elseif (startsWith($query,'get-archive/')) {
452 $path = explode('/',substr($query,strlen('get-archive/')));
453 $reply = getArchive($db,$path[0],$path[1],$path[2],$path[3],$path[4],$path[5]);
460 $redis->set('meteo-'.$hash, serialize($reply));
461 $redis->expire('meteo-'.$hash, $expire);
463 print(json_encode($reply));