source: src/webapp/webservice/index.php@ 5031511

debian/bullseye debian/jessie
Last change on this file since 5031511 was 6cce084, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago

Removed xtrace commands from webservice

  • Property mode set to 100644
File size: 5.1 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;
[06c0abb]45 $startTime = $endTime - $period;
[33c6557]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;
[06c0abb]68 $lastValid = -1;
[5519ced]69 $keys = array_keys($data);
[acea570]70 $keyTimeframes = $keys[0];
71 $itemCount = count($keys);
72 if($itemCount < 0 ){
[5519ced]73 error_log("No keys in fetched rrd data from ". $dbFile);
74 echo json_encode("");
75 exit;
76 }
[acea570]77 $timeframesCount = count($data[$keyTimeframes]);
[73c34f3]78 if($debug > 0){
[acea570]79 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
[73c34f3]80 }
[33c6557]81 foreach($data[$keys[0]] as $timeframe => $value){
[5519ced]82 $allValid = true;
[33c6557]83 $gpsFix = false;
[5519ced]84 $tmp = array();
85 foreach($keys as $key){
86 if($key == "gpsfix"){
[33c6557]87 switch($data[$key][$timeframe]){
88 case 2:
[5519ced]89 $tmp[$key] = "2D";
[33c6557]90 $gpsFix = true;
[5519ced]91 break;
[33c6557]92 case 3:
[5519ced]93 $tmp[$key] = "3D";
[33c6557]94 $gpsFix = true;
[5519ced]95 break;
96 default:
97 $tmp[$key] = "-";
98 }
99 }
100 else{
[33c6557]101 $tmp[$key] = $data[$key][$timeframe];
[5519ced]102 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
103 $allValid = false;
[5149714]104 if($debug > 1){
105 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
106 }
[5519ced]107 }
108 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
109 $tmp[$key] = round($tmp[$key], $configRound[$key]);
[3121b3b]110 }
111 }
[5519ced]112 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
[5149714]113 if($allValid){
[33c6557]114 $lastValid = $tmp;
115 if($gpsFix){
116 if(!array_key_exists($timeframe, $result["track"])){
117 $result["track"][$timeframe] = $tmp;
118 }
119 }
[5519ced]120 }
[3121b3b]121 else{
[73c34f3]122 if($debug > 1){
[5519ced]123 echo "Failed <pre>";
124 var_dump($tmp);
125 echo "</pre>\n";
[4ab1c0a]126 }
[f8dfbe7]127 }
[4ab1c0a]128 }
[73c34f3]129 ksort($result["track"]);
130}
131
132$debug = 0;
133if(array_key_exists("debug", $_GET))
134 $debug = $_GET["debug"];
135
136$request = REQUEST_DATA;
137if(array_key_exists("request", $_GET)){
138 $request = $_GET["request"];
139}
140
141if($debug == 0)
142 header('Content-Type: application/json');
143
144$result = array();
145if($request == REQUEST_SHUTDOWN){
[9be4f3f]146 $handle = @fopen($config->shutdownRequestFile(), "w");
[73c34f3]147 if($handle === false){
[9be4f3f]148 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
[73c34f3]149 if($debug > 0)
150 echo $message;
151 error_log($message);
152 $result["shutdownstart"] = -1;
153 }
154 else{
155 $shutdownTime = new DateTime('NOW');
[9160ba0]156 $shutdownTime->modify("+15 seconds");
[73c34f3]157 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
158 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
159 }
160}
161elseif($request == REQUEST_DATA){
162 $result["lastvalid"] = array();
163 $result["track"] = array();
164
165 if(!file_exists($dbFile)){
166 echo json_encode("");
167 if($debug > 0)
168 echo "Database file ". $dbFile ." does not exist!";
169 error_log("Database file ". $dbFile ." does not exist");
170 exit;
171 }
[33c6557]172 // Fetch entries of the last 30s with maximum (available) resolution of 2s
173 addRrdData("2", "240", $result, $lastValid);
174 if($lastValid){
175 $result["lastvalid"] = $lastValid;
[4ab1c0a]176 }
[33c6557]177 // Fetch entries of the last 1h with maximum (available) resolution of 10s
178 addRrdData("10", "36000", $result, $lastTime);
179 // Fetch entries of the last 24h with maximum (available) resolution of 60s
180 addRrdData("60", "86400", $result, $lastTime);
[3121b3b]181}
182
[4ab1c0a]183if($debug > 0){
[33c6557]184 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
[4ab1c0a]185}
186else{
[33c6557]187 echo json_encode($result) ."\n";
[4ab1c0a]188}
[d7e6e9c]189
190?>
Note: See TracBrowser for help on using the repository browser.