source: src/webapp/webservice/index.php@ 06c0abb

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

Small fixes in gnuboat daemon and webservice

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