source: src/webapp/webservice/index.php@ 33c6557

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 33c6557 was 33c6557, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago

Fixed webservice rrd processing

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[d7e6e9c]1<?php
2/*
[33c6557]3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
[d7e6e9c]4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
[4ab1c0a]19require_once(__DIR__ ."/inc/Config.php");
20$config = new Config();
21$dbFile = $config->databaseFile();
22
[5519ced]23define("REQUEST_DATA", "data");
24define("REQUEST_SHUTDOWN", "shutdown");
25
26
[4ab1c0a]27// configure roundings per value (key) and precision (value)
28$configRound = array
29(
30 "latitude" => 5,
31 "longitude" => 5,
32 "speedthroughwater" => 1,
33 "speedoverground" => 1,
34 "waterdepth" => 1,
35 "watertemperature" => 1,
36);
37
[33c6557]38function addRrdData($interval, $period, &$result, &$lastValid){
[73c34f3]39 global $dbFile;
40 global $debug;
41 global $configRound;
[33c6557]42 $lastValid = false;
[4ab1c0a]43
[33c6557]44 $endTime = floor(time() / $interval) * $interval;
45 $startTime = $endTime - $period;
46 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
47 // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
[5519ced]48 if($data === false){
49 echo json_encode($result);
[73c34f3]50 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
[5519ced]51 if($debug > 0)
52 echo $message;
53 else
54 error_log($message);
[33c6557]55 return;
[5519ced]56 }
[3121b3b]57
[5519ced]58 if($debug > 2){
59 echo "<pre>";
60 var_dump($data);
61 echo "</pre>";
62 }
[3121b3b]63
[5519ced]64 $data = $data["data"];
65
66 // transform them into json format
67 $i = 0;
68 $lastTime = -1;
69 $keys = array_keys($data);
70 if(count($keys) < 0 ){
71 error_log("No keys in fetched rrd data from ". $dbFile);
72 echo json_encode("");
73 exit;
74 }
[73c34f3]75 if($debug > 0){
76 echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
77 }
[33c6557]78 foreach($data[$keys[0]] as $timeframe => $value){
[5519ced]79 $allValid = true;
[33c6557]80 $gpsFix = false;
[5519ced]81 $tmp = array();
82 foreach($keys as $key){
83 if($key == "gpsfix"){
[33c6557]84 switch($data[$key][$timeframe]){
85 case 2:
[5519ced]86 $tmp[$key] = "2D";
[33c6557]87 $gpsFix = true;
[5519ced]88 break;
[33c6557]89 case 3:
[5519ced]90 $tmp[$key] = "3D";
[33c6557]91 $gpsFix = true;
[5519ced]92 break;
93 default:
94 $tmp[$key] = "-";
95 }
96 }
97 else{
[33c6557]98 $tmp[$key] = $data[$key][$timeframe];
[5519ced]99 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
100 $allValid = false;
[5149714]101 if($debug > 1){
102 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
103 }
[5519ced]104 }
105 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
106 $tmp[$key] = round($tmp[$key], $configRound[$key]);
[3121b3b]107 }
108 }
[5519ced]109 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
[5149714]110 if($allValid){
[33c6557]111 $lastValid = $tmp;
112 if($gpsFix){
113 if(!array_key_exists($timeframe, $result["track"])){
114 $result["track"][$timeframe] = $tmp;
115 }
116 }
[5519ced]117 }
[3121b3b]118 else{
[73c34f3]119 if($debug > 1){
[5519ced]120 echo "Failed <pre>";
121 var_dump($tmp);
122 echo "</pre>\n";
[4ab1c0a]123 }
[f8dfbe7]124 }
[4ab1c0a]125 }
[73c34f3]126 ksort($result["track"]);
127}
128
129$debug = 0;
130if(array_key_exists("debug", $_GET))
131 $debug = $_GET["debug"];
132
133$request = REQUEST_DATA;
134if(array_key_exists("request", $_GET)){
135 $request = $_GET["request"];
136}
137
138if($debug == 0)
139 header('Content-Type: application/json');
140
141$result = array();
142if($request == REQUEST_SHUTDOWN){
[9be4f3f]143 $handle = @fopen($config->shutdownRequestFile(), "w");
[73c34f3]144 if($handle === false){
[9be4f3f]145 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
[73c34f3]146 if($debug > 0)
147 echo $message;
148 error_log($message);
149 $result["shutdownstart"] = -1;
150 }
151 else{
152 $shutdownTime = new DateTime('NOW');
[9160ba0]153 $shutdownTime->modify("+15 seconds");
[73c34f3]154 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
155 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
156 }
157}
158elseif($request == REQUEST_DATA){
159 $result["lastvalid"] = array();
160 $result["track"] = array();
161
162 if(!file_exists($dbFile)){
163 echo json_encode("");
164 if($debug > 0)
165 echo "Database file ". $dbFile ." does not exist!";
166 error_log("Database file ". $dbFile ." does not exist");
167 exit;
168 }
[33c6557]169 // Fetch entries of the last 30s with maximum (available) resolution of 2s
170 addRrdData("2", "240", $result, $lastValid);
171 if($lastValid){
172 $result["lastvalid"] = $lastValid;
[4ab1c0a]173 }
[33c6557]174 // Fetch entries of the last 1h with maximum (available) resolution of 10s
175 addRrdData("10", "36000", $result, $lastTime);
176 // Fetch entries of the last 24h with maximum (available) resolution of 60s
177 addRrdData("60", "86400", $result, $lastTime);
[3121b3b]178}
179
[4ab1c0a]180if($debug > 0){
[33c6557]181 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
[4ab1c0a]182}
183else{
[33c6557]184 echo json_encode($result) ."\n";
[4ab1c0a]185}
[d7e6e9c]186
187?>
Note: See TracBrowser for help on using the repository browser.