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 CONCAT(s_id,'.',t.st_abbr,'.*') id
141 sensors_ranges r, sensors s, sensor_types t
143 r.sensor=s.id and s.st_id=t.id and t.st_common_description is not null ".$filter."
144 and r.day=STR_TO_DATE('".strval($y)."-".strval($m)."-".strval($d)."','%Y-%m-%d')
147 $q = $db -> prepare( $sql );
152 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
153 array_push($res, $row['id']);
160 function getCurrentValues($db,$s_id,$p_id) {
166 u.id stored_unit,du.id display_unit
168 sensors s,sensor_types t,st_parameters p,units u,units du
169 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
172 $q = $db -> prepare( $sql );
173 $q -> bindParam(':id',$s_id,PDO::PARAM_INT);
174 $q -> bindParam(':param',$p_id,PDO::PARAM_INT);
176 $units = $q -> fetch(PDO::FETCH_ASSOC);
180 CONCAT(subset.t,'5:00') t,UnitConv(avg(subset.y),:stored,:display) y
183 substr(date_format(timestamp,'%Y-%m-%dT%H:%i'),1,15) t,value y
187 sensor_id = :id and parameter_id=:param
188 and timestamp>adddate(now(),-1)
194 $q = $db -> prepare( $sql );
195 $q -> bindParam(':id',$s_id,PDO::PARAM_INT);
196 $q -> bindParam(':param',$p_id,PDO::PARAM_INT);
197 $q -> bindParam(':stored',$units['stored_unit'],PDO::PARAM_INT);
198 $q -> bindParam(':display',$units['display_unit'],PDO::PARAM_INT);
201 return $q -> fetchAll(PDO::FETCH_ASSOC);
205 function getCurrent($db,$id,$type,$param) {
212 SELECT s.id s_id,p.id p_id, p.st_name p_name
213 FROM sensors s,sensor_types t, st_parameters p
214 WHERE s.st_id=t.id and p.st_id=t.id and s_id=:id and t.st_abbr=:type ".$filter."
217 $q = $db -> prepare( $sql );
218 $q -> bindParam(':id',$id,PDO::PARAM_STR);
219 $q -> bindParam(':type',$type,PDO::PARAM_STR);
221 $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
225 foreach ($sensors as $sensor) {
227 $results[$sensor['p_name']] = getCurrentValues($db,$sensor['s_id'],$sensor['p_id']);
236 SELECT s.id s_id,p.id p_id
237 FROM sensors s,sensor_types t, st_parameters p
238 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."
241 $q = $db -> prepare( $sql );
242 $q -> bindParam(':id',$id,PDO::PARAM_STR);
243 $q -> bindParam(':type',$type,PDO::PARAM_STR);
244 $q -> bindParam(':param',$param,PDO::PARAM_STR);
246 $sensor = $q -> fetch(PDO::FETCH_ASSOC);
248 return getCurrentValues($db,$sensor['s_id'],$sensor['p_id']);
254 function getArchiveValues($db,$year,$month,$day,$s_id,$p_id) {
260 u.id stored_unit,du.id display_unit
262 sensors s,sensor_types t,st_parameters p,units u,units du
263 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
266 $q = $db -> prepare( $sql );
267 $q -> bindParam(':id',$s_id,PDO::PARAM_INT);
268 $q -> bindParam(':param',$p_id,PDO::PARAM_INT);
270 $units = $q -> fetch(PDO::FETCH_ASSOC);
274 CONCAT(subset.t,'5:00') t,UnitConv(avg(subset.y),:stored,:display) y
277 substr(date_format(timestamp,'%Y-%m-%dT%H:%i'),1,15) t,value y
281 sensor_id = :id and parameter_id=:param
282 and timestamp>=STR_TO_DATE(:d,'%Y-%m-%d')
283 and timestamp<DATE_ADD(STR_TO_DATE(:d,'%Y-%m-%d'), interval 1 day)
289 $date = $year.'-'.$month.'-'.$day;
291 $q = $db -> prepare( $sql );
292 $q -> bindParam(':id',$s_id,PDO::PARAM_INT);
293 $q -> bindParam(':param',$p_id,PDO::PARAM_INT);
294 $q -> bindParam(':d',$date,PDO::PARAM_STR);
295 $q -> bindParam(':stored',$units['stored_unit'],PDO::PARAM_INT);
296 $q -> bindParam(':display',$units['display_unit'],PDO::PARAM_INT);
299 return $q -> fetchAll(PDO::FETCH_ASSOC);
303 function getArchive($db,$year,$month,$day,$id,$type,$param) {
311 $date = strval($y).'-'.strval($m).'-'.strval($d);
316 SELECT s.id s_id,p.id p_id, p.st_name p_name
317 FROM sensors s,sensor_types t, st_parameters p
318 WHERE s.st_id=t.id and p.st_id=t.id and s_id=:id and t.st_abbr=:type ".$filter."
321 $q = $db -> prepare( $sql );
322 $q -> bindParam(':id',$id,PDO::PARAM_STR);
323 $q -> bindParam(':type',$type,PDO::PARAM_STR);
325 $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
329 foreach ($sensors as $sensor) {
331 $result[$sensor['p_name']] = getArchiveValues($db,$year,$month,$day,$sensor['s_id'],$sensor['p_id']);
340 SELECT s.id s_id,p.id p_id
341 FROM sensors s,sensor_types t, st_parameters p
342 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."
345 $q = $db -> prepare( $sql );
346 $q -> bindParam(':id',$id,PDO::PARAM_STR);
347 $q -> bindParam(':type',$type,PDO::PARAM_STR);
348 $q -> bindParam(':param',$param,PDO::PARAM_STR);
350 $sensor = $q -> fetch(PDO::FETCH_ASSOC);
352 return getArchiveValues($db,$year,$month,$day,$sensor['s_id'],$sensor['p_id']);
358 function getProps($db, $localNet) {
364 CONCAT(s_id,'.',t.st_abbr,'.',p.st_name) sensor_id,
365 p.st_description name,
369 p.st_line_color color
371 sensors s, sensor_types t, st_parameters p,units u,units du,places pl
373 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."
376 $q = $db -> prepare( $sql );
385 "axes" => [ "color" => "black", "size" => 16, "style" => "normal" ],
386 "legend" => [ "color" => "black", "size" => 16, "style" => "normal"
389 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
390 $reply["names"][$row["sensor_id"]] = $row["name"];
391 $reply["colors"][$row["sensor_id"]] = $row["color"];
392 $reply["units"][$row["sensor_id"]] = $row["unit"];
393 $reply["places"][$row["sensor_id"]] = $row["place_name"];
394 $reply["scale"][$row["sensor_id"]] = [ 0 => 1.0, 1 => $row["prec"] ];
399 CONCAT(s_id,'.',t.st_abbr,'.*') sensor_id,
400 t.st_common_description name,
403 sensors s, sensor_types t, places pl
405 pl.idplaces=s.place_id and s.st_id=t.id and t.st_common_description is not null ".$filter."
408 $q = $db -> prepare( $sql );
410 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
411 $reply["names"][$row["sensor_id"]] = $row["name"];
412 $reply["places"][$row["sensor_id"]] = $row["place_name"];
419 function getState($db, $localNet) {
428 v.sensor as sensor_int_id,
429 pl.place_name s_description,
431 p.st_name as param_name,
437 sensors_ranges v,st_parameters p,sensors s,places pl,sensor_types st,units u,units du
439 v.timestamp>addtime(now(), -43200)
446 and s.place_id=pl.idplaces
448 and u.unit_group=du.unit_group
452 s_description,sensor_id,param_id
455 $q = $db -> prepare( $sql );
460 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
462 $sensor_id = $row['sensor_id'];
463 $st_id = $row['st_abbr'];
464 $sensor_int_id = $row['sensor_int_id'];
465 $param_id = $row['param_name'];
466 $param_int_id = $row['param_id'];
467 $unit_id = $row['unit_id'];
468 $stored_unit_id = $row['stored_unit_id'];
469 $place_description = $row['s_description'];
472 SELECT UnitConv(value,".$stored_unit_id.",".$unit_id.") as val,timestamp
476 sensor_id = ".$sensor_int_id." and parameter_id=".$param_int_id."
482 $qv = $db -> prepare( $sql_last_val );
485 $v_row = $qv -> fetch(PDO::FETCH_ASSOC);
487 $value = $v_row["val"];
488 $timestamp = $v_row["timestamp"];
490 if (! array_key_exists($place_description,$reply)) {
491 $reply[$place_description] = [];
494 if (! array_key_exists($sensor_id,$reply[$place_description])) {
495 $reply[$place_description][$sensor_id] = [];
498 if (! array_key_exists($st_id,$reply[$place_description][$sensor_id])) {
499 $reply[$place_description][$sensor_id][$st_id] = [];
502 $reply[$place_description][$sensor_id][$st_id][$param_id] = $value;
503 $reply[$place_description][$sensor_id]['timestamp'] = $timestamp;
511 function getPanel($db) {
516 v.sensor as sensor_int_id,
518 p.st_name as param_name,
523 sensors_ranges v,st_parameters p,sensors s,places pl,sensor_types st,units u,units du
525 v.timestamp>addtime(now(), -43200)
532 and s.place_id=pl.idplaces
534 and u.unit_group=du.unit_group
536 and (s.s_id,p.st_name) in (select sensor_id,param_name from panel_sensors)
541 $q = $db -> prepare( $sql );
546 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
548 $sensor_id = $row['sensor_id'];
549 $sensor_int_id = $row['sensor_int_id'];
550 $param_id = $row['param_name'];
551 $param_int_id = $row['param_id'];
552 $unit_id = $row['unit_id'];
553 $stored_unit_id = $row['stored_unit_id'];
556 SELECT UnitConv(value,".$stored_unit_id.",".$unit_id.") as val,timestamp
560 sensor_id = ".$sensor_int_id." and parameter_id=".$param_int_id."
561 and timestamp > NOW() - interval 30 minute
567 $qv = $db -> prepare( $sql_last_val );
570 $v_row = $qv -> fetch(PDO::FETCH_ASSOC);
572 $value = $v_row["val"];
574 if ($value && $value != "null") {
575 $reply[strtoupper($param_id)] = $value;
586 if ($query == 'props') {
588 $reply = getProps($db, $local_net);
591 } elseif ($query == 'state') {
593 $reply = getState($db, $local_net);
595 } elseif ($query == 'panel') {
597 $reply = getPanel($db);
599 } elseif (startsWith($query,'get/')) {
601 $sensor = explode('/',substr($query,strlen('get/')));
602 $reply = getCurrent($db,$sensor[0],$sensor[1],$sensor[2]);
604 } elseif ($query == 'years') {
606 $reply = getYears($db);
609 } elseif (startsWith($query,'months/')) {
611 $date = explode('/',substr($query,strlen('months/')));
612 $reply = getMonths($db,$date[0]);
615 } elseif (startsWith($query,'days/')) {
617 $date = explode('/',substr($query,strlen('days/')));
618 $reply = getDays($db,$date[0],$date[1]);
621 } elseif (startsWith($query,'sensors/')) {
623 $date = explode('/',substr($query,strlen('sensors/')));
624 $reply = getSensors($db,$date[0],$date[1],$date[2]);
627 } elseif (startsWith($query,'get-archive/')) {
629 $path = explode('/',substr($query,strlen('get-archive/')));
630 $reply = getArchive($db,$path[0],$path[1],$path[2],$path[3],$path[4],$path[5]);
637 $redis->set('meteo-'.$hash, serialize($reply));
638 $redis->expire('meteo-'.$hash, $expire);
640 print(json_encode($reply));