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

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since b3b939f was 9be4f3f, checked in by Joachim Langenbach <joachim.langenbach@…>, 10 years ago
  • Updated default paths for database files
  • Added config option to webservice to configure shutdown file
  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[d7e6e9c]1<?php
2/*
3 Copyright (C) 2016 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
[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
[73c34f3]38function addRrdData($interval, $period, &$result, &$lastTime){
39 global $dbFile;
40 global $debug;
41 global $configRound;
[4ab1c0a]42
[73c34f3]43 $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
[5519ced]44 if($data === false){
45 echo json_encode($result);
[73c34f3]46 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
[5519ced]47 if($debug > 0)
48 echo $message;
49 else
50 error_log($message);
51 }
[3121b3b]52
[5519ced]53 if($debug > 2){
54 echo "<pre>";
55 var_dump($data);
56 echo "</pre>";
57 }
[3121b3b]58
[5519ced]59 $data = $data["data"];
60
61 // transform them into json format
62 $i = 0;
63 $lastTime = -1;
64 $keys = array_keys($data);
65 if(count($keys) < 0 ){
66 error_log("No keys in fetched rrd data from ". $dbFile);
67 echo json_encode("");
68 exit;
69 }
[73c34f3]70 if($debug > 0){
71 echo "<p>Variables: ". count($keys) ."; Timeframes: ". count($data[$keys[0]]) ."</p>\n";
72 }
[5519ced]73 for($i = 0; $i < count($data[$keys[0]]); $i++){
74 $time = array_keys($data[$keys[0]])[$i];
75 $allValid = true;
76 $tmp = array();
77 foreach($keys as $key){
78 if($key == "gpsfix"){
79 switch($data[$key][$time]){
80 case 1:
81 $tmp[$key] = "2D";
82 break;
83 case 2:
84 $tmp[$key] = "3D";
85 break;
86 default:
87 $tmp[$key] = "-";
88 }
89 }
90 else{
91 $tmp[$key] = $data[$key][$time];
92 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
93 $allValid = false;
[5149714]94 if($debug > 1){
95 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
96 }
[5519ced]97 }
98 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
99 $tmp[$key] = round($tmp[$key], $configRound[$key]);
[3121b3b]100 }
101 }
[5519ced]102 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
[5149714]103 if($allValid){
104 if(!array_key_exists($time, $result["track"])){
[73c34f3]105 $result["track"][$time] = $tmp;
[5149714]106 }
[5519ced]107 $lastTime = $time;
108 }
[3121b3b]109 else{
[73c34f3]110 if($debug > 1){
[5519ced]111 echo "Failed <pre>";
112 var_dump($tmp);
113 echo "</pre>\n";
[4ab1c0a]114 }
[f8dfbe7]115 }
[4ab1c0a]116 }
[e82ec64]117
[73c34f3]118 ksort($result["track"]);
119}
120
121$debug = 0;
122if(array_key_exists("debug", $_GET))
123 $debug = $_GET["debug"];
124
125$request = REQUEST_DATA;
126if(array_key_exists("request", $_GET)){
127 $request = $_GET["request"];
128}
129
130if($debug == 0)
131 header('Content-Type: application/json');
132
133$result = array();
134if($request == REQUEST_SHUTDOWN){
[9be4f3f]135 $handle = @fopen($config->shutdownRequestFile(), "w");
[73c34f3]136 if($handle === false){
[9be4f3f]137 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
[73c34f3]138 if($debug > 0)
139 echo $message;
140 error_log($message);
141 $result["shutdownstart"] = -1;
142 }
143 else{
144 $shutdownTime = new DateTime('NOW');
[9160ba0]145 $shutdownTime->modify("+15 seconds");
[73c34f3]146 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
147 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
148 }
149}
150elseif($request == REQUEST_DATA){
151 $result["lastvalid"] = array();
152 $result["track"] = array();
153
154 if(!file_exists($dbFile)){
155 echo json_encode("");
156 if($debug > 0)
157 echo "Database file ". $dbFile ." does not exist!";
158 error_log("Database file ". $dbFile ." does not exist");
159 exit;
160 }
161 // Fetch entries of the last 30 seconds
162 addRrdData("5s", "30s", $result, $lastTime);
[5519ced]163 if($lastTime > -1){
164 $result["lastvalid"] = $result["track"][$lastTime];
[4ab1c0a]165 }
[73c34f3]166 addRrdData("15s", "5minutes", $result, $lastTime);
167 addRrdData("30s", "12h", $result, $lastTime);
[3121b3b]168}
169
[4ab1c0a]170if($debug > 0){
171 echo json_encode($result, JSON_PRETTY_PRINT);
172}
173else{
174 echo json_encode($result);
175}
[d7e6e9c]176
177?>
Note: See TracBrowser for help on using the repository browser.