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