source: src/webapp/webservice/index.php@ 22f7269

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

Merge branch 'master' of ​https://svn.falaba.de/gnuboat

  • Property mode set to 100644
File size: 5.2 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
[acea570]19xdebug_start_trace();
20
[4ab1c0a]21require_once(__DIR__ ."/inc/Config.php");
22$config = new Config();
23$dbFile = $config->databaseFile();
24
[5519ced]25define("REQUEST_DATA", "data");
26define("REQUEST_SHUTDOWN", "shutdown");
27
28
[4ab1c0a]29// configure roundings per value (key) and precision (value)
30$configRound = array
31(
32 "latitude" => 5,
33 "longitude" => 5,
34 "speedthroughwater" => 1,
35 "speedoverground" => 1,
36 "waterdepth" => 1,
37 "watertemperature" => 1,
38);
39
[33c6557]40function addRrdData($interval, $period, &$result, &$lastValid){
[73c34f3]41 global $dbFile;
42 global $debug;
43 global $configRound;
[33c6557]44 $lastValid = false;
[4ab1c0a]45
[33c6557]46 $endTime = floor(time() / $interval) * $interval;
47 $startTime = $endTime - $period;
48 $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
49 // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
[5519ced]50 if($data === false){
51 echo json_encode($result);
[73c34f3]52 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
[5519ced]53 if($debug > 0)
54 echo $message;
55 else
56 error_log($message);
[33c6557]57 return;
[5519ced]58 }
[3121b3b]59
[5519ced]60 if($debug > 2){
61 echo "<pre>";
62 var_dump($data);
63 echo "</pre>";
64 }
[3121b3b]65
[5519ced]66 $data = $data["data"];
67
68 // transform them into json format
69 $i = 0;
70 $lastTime = -1;
71 $keys = array_keys($data);
[acea570]72 $keyTimeframes = $keys[0];
73 $itemCount = count($keys);
74 if($itemCount < 0 ){
[5519ced]75 error_log("No keys in fetched rrd data from ". $dbFile);
76 echo json_encode("");
77 exit;
78 }
[acea570]79 $timeframesCount = count($data[$keyTimeframes]);
[73c34f3]80 if($debug > 0){
[acea570]81 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
[73c34f3]82 }
[33c6557]83 foreach($data[$keys[0]] as $timeframe => $value){
[5519ced]84 $allValid = true;
[33c6557]85 $gpsFix = false;
[5519ced]86 $tmp = array();
87 foreach($keys as $key){
88 if($key == "gpsfix"){
[33c6557]89 switch($data[$key][$timeframe]){
90 case 2:
[5519ced]91 $tmp[$key] = "2D";
[33c6557]92 $gpsFix = true;
[5519ced]93 break;
[33c6557]94 case 3:
[5519ced]95 $tmp[$key] = "3D";
[33c6557]96 $gpsFix = true;
[5519ced]97 break;
98 default:
99 $tmp[$key] = "-";
100 }
101 }
102 else{
[33c6557]103 $tmp[$key] = $data[$key][$timeframe];
[5519ced]104 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
105 $allValid = false;
[5149714]106 if($debug > 1){
107 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
108 }
[5519ced]109 }
110 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
111 $tmp[$key] = round($tmp[$key], $configRound[$key]);
[3121b3b]112 }
113 }
[5519ced]114 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
[5149714]115 if($allValid){
[33c6557]116 $lastValid = $tmp;
117 if($gpsFix){
118 if(!array_key_exists($timeframe, $result["track"])){
119 $result["track"][$timeframe] = $tmp;
120 }
121 }
[5519ced]122 }
[3121b3b]123 else{
[73c34f3]124 if($debug > 1){
[5519ced]125 echo "Failed <pre>";
126 var_dump($tmp);
127 echo "</pre>\n";
[4ab1c0a]128 }
[f8dfbe7]129 }
[4ab1c0a]130 }
[73c34f3]131 ksort($result["track"]);
132}
133
134$debug = 0;
135if(array_key_exists("debug", $_GET))
136 $debug = $_GET["debug"];
137
138$request = REQUEST_DATA;
139if(array_key_exists("request", $_GET)){
140 $request = $_GET["request"];
141}
142
143if($debug == 0)
144 header('Content-Type: application/json');
145
146$result = array();
147if($request == REQUEST_SHUTDOWN){
[9be4f3f]148 $handle = @fopen($config->shutdownRequestFile(), "w");
[73c34f3]149 if($handle === false){
[9be4f3f]150 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
[73c34f3]151 if($debug > 0)
152 echo $message;
153 error_log($message);
154 $result["shutdownstart"] = -1;
155 }
156 else{
157 $shutdownTime = new DateTime('NOW');
[9160ba0]158 $shutdownTime->modify("+15 seconds");
[73c34f3]159 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
160 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
161 }
162}
163elseif($request == REQUEST_DATA){
164 $result["lastvalid"] = array();
165 $result["track"] = array();
166
167 if(!file_exists($dbFile)){
168 echo json_encode("");
169 if($debug > 0)
170 echo "Database file ". $dbFile ." does not exist!";
171 error_log("Database file ". $dbFile ." does not exist");
172 exit;
173 }
[33c6557]174 // Fetch entries of the last 30s with maximum (available) resolution of 2s
175 addRrdData("2", "240", $result, $lastValid);
176 if($lastValid){
177 $result["lastvalid"] = $lastValid;
[4ab1c0a]178 }
[33c6557]179 // Fetch entries of the last 1h with maximum (available) resolution of 10s
180 addRrdData("10", "36000", $result, $lastTime);
181 // Fetch entries of the last 24h with maximum (available) resolution of 60s
182 addRrdData("60", "86400", $result, $lastTime);
[3121b3b]183}
184
[4ab1c0a]185if($debug > 0){
[33c6557]186 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
[4ab1c0a]187}
188else{
[33c6557]189 echo json_encode($result) ."\n";
[4ab1c0a]190}
[d7e6e9c]191
[acea570]192xdebug_stop_trace();
193
[d7e6e9c]194?>
Note: See TracBrowser for help on using the repository browser.