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

debian/bullseye debian/jessie
Last change on this file since a68d628 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
Line 
1<?php
2/*
3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
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
19require_once(__DIR__ ."/inc/Config.php");
20$config = new Config();
21$dbFile = $config->databaseFile();
22
23define("REQUEST_DATA", "data");
24define("REQUEST_SHUTDOWN", "shutdown");
25
26
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
38function addRrdData($interval, $period, &$result, &$lastValid){
39 global $dbFile;
40 global $debug;
41 global $configRound;
42 $lastValid = false;
43
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"));
48 if($data === false){
49 echo json_encode($result);
50 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
51 if($debug > 0)
52 echo $message;
53 else
54 error_log($message);
55 return;
56 }
57
58 if($debug > 2){
59 echo "<pre>";
60 var_dump($data);
61 echo "</pre>";
62 }
63
64 $data = $data["data"];
65
66 // transform them into json format
67 $i = 0;
68 $lastValid = -1;
69 $keys = array_keys($data);
70 $keyTimeframes = $keys[0];
71 $itemCount = count($keys);
72 if($itemCount < 0 ){
73 error_log("No keys in fetched rrd data from ". $dbFile);
74 echo json_encode("");
75 exit;
76 }
77 $timeframesCount = count($data[$keyTimeframes]);
78 if($debug > 0){
79 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
80 }
81 foreach($data[$keys[0]] as $timeframe => $value){
82 $allValid = true;
83 $gpsFix = false;
84 $tmp = array();
85 foreach($keys as $key){
86 if($key == "gpsfix"){
87 switch($data[$key][$timeframe]){
88 case 2:
89 $tmp[$key] = "2D";
90 $gpsFix = true;
91 break;
92 case 3:
93 $tmp[$key] = "3D";
94 $gpsFix = true;
95 break;
96 default:
97 $tmp[$key] = "-";
98 }
99 }
100 else{
101 $tmp[$key] = $data[$key][$timeframe];
102 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
103 $allValid = false;
104 if($debug > 1){
105 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
106 }
107 }
108 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
109 $tmp[$key] = round($tmp[$key], $configRound[$key]);
110 }
111 }
112 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
113 if($allValid){
114 $lastValid = $tmp;
115 if($gpsFix){
116 if(!array_key_exists($timeframe, $result["track"])){
117 $result["track"][$timeframe] = $tmp;
118 }
119 }
120 }
121 else{
122 if($debug > 1){
123 echo "Failed <pre>";
124 var_dump($tmp);
125 echo "</pre>\n";
126 }
127 }
128 }
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){
146 $handle = @fopen($config->shutdownRequestFile(), "w");
147 if($handle === false){
148 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
149 if($debug > 0)
150 echo $message;
151 error_log($message);
152 $result["shutdownstart"] = -1;
153 }
154 else{
155 $shutdownTime = new DateTime('NOW');
156 $shutdownTime->modify("+15 seconds");
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 }
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;
176 }
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);
181}
182
183if($debug > 0){
184 echo json_encode($result, JSON_PRETTY_PRINT) ."\n";
185}
186else{
187 echo json_encode($result) ."\n";
188}
189
190?>
Note: See TracBrowser for help on using the repository browser.