| [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] | 19 | require_once(__DIR__ ."/inc/Config.php");
|
|---|
| 20 | $config = new Config();
|
|---|
| 21 | $dbFile = $config->databaseFile();
|
|---|
| 22 |
|
|---|
| 23 | // configure roundings per value (key) and precision (value)
|
|---|
| 24 | $configRound = array
|
|---|
| 25 | (
|
|---|
| 26 | "latitude" => 5,
|
|---|
| 27 | "longitude" => 5,
|
|---|
| 28 | "speedthroughwater" => 1,
|
|---|
| 29 | "speedoverground" => 1,
|
|---|
| 30 | "waterdepth" => 1,
|
|---|
| 31 | "watertemperature" => 1,
|
|---|
| 32 | );
|
|---|
| 33 |
|
|---|
| 34 | $debug = 0;
|
|---|
| 35 | if(array_key_exists("debug", $_GET))
|
|---|
| 36 | $debug = $_GET["debug"];
|
|---|
| 37 |
|
|---|
| 38 | if($debug == 0)
|
|---|
| 39 | header('Content-Type: application/json');
|
|---|
| 40 |
|
|---|
| 41 | $result["lastvalid"] = array();
|
|---|
| 42 | $result["track"] = array();
|
|---|
| 43 |
|
|---|
| 44 | // Fetch entries of the last 12hours
|
|---|
| 45 | $data = rrd_fetch($dbFile, array( "AVERAGE", "--start", "-12h"));
|
|---|
| 46 | if($data === false){
|
|---|
| 47 | echo json_encode($result);
|
|---|
| 48 | $message = "Could not open rrd database ". $dbFile;
|
|---|
| 49 | if($debug > 0)
|
|---|
| 50 | echo $message;
|
|---|
| 51 | else
|
|---|
| 52 | error_log($message);
|
|---|
| 53 | exit;
|
|---|
| 54 | }
|
|---|
| [3121b3b] | 55 |
|
|---|
| [4ab1c0a] | 56 | if($debug > 2){
|
|---|
| 57 | echo "<pre>";
|
|---|
| 58 | var_dump($data);
|
|---|
| 59 | echo "</pre>";
|
|---|
| 60 | }
|
|---|
| [3121b3b] | 61 |
|
|---|
| [4ab1c0a] | 62 | $data = $data["data"];
|
|---|
| [3121b3b] | 63 |
|
|---|
| [4ab1c0a] | 64 | // transform them into json format
|
|---|
| 65 | $i = 0;
|
|---|
| 66 | $lastTime = -1;
|
|---|
| 67 | $keys = array_keys($data);
|
|---|
| 68 | if(count($keys) < 0 ){
|
|---|
| 69 | error_log("No keys in fetched rrd data from ". $dbFile);
|
|---|
| 70 | echo json_encode("");
|
|---|
| 71 | exit;
|
|---|
| 72 | }
|
|---|
| 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]){
|
|---|
| [3121b3b] | 80 | case 1:
|
|---|
| [4ab1c0a] | 81 | $tmp[$key] = "2D";
|
|---|
| [3121b3b] | 82 | break;
|
|---|
| 83 | case 2:
|
|---|
| [4ab1c0a] | 84 | $tmp[$key] = "3D";
|
|---|
| [3121b3b] | 85 | break;
|
|---|
| 86 | default:
|
|---|
| [4ab1c0a] | 87 | $tmp[$key] = "-";
|
|---|
| [3121b3b] | 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | else{
|
|---|
| [4ab1c0a] | 91 | $tmp[$key] = $data[$key][$time];
|
|---|
| 92 | if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
|
|---|
| 93 | $allValid = false;
|
|---|
| 94 | }
|
|---|
| 95 | elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
|
|---|
| 96 | $tmp[$key] = round($tmp[$key], $configRound[$key]);
|
|---|
| [f8dfbe7] | 97 | }
|
|---|
| [4ab1c0a] | 98 | }
|
|---|
| 99 | // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
|
|---|
| 100 | if($allValid && $tmp["gpsfix"] != "-"){
|
|---|
| 101 | $result["track"][$time] = $tmp;
|
|---|
| 102 | $lastTime = $time;
|
|---|
| 103 | }
|
|---|
| 104 | else{
|
|---|
| 105 | if($debug > 0){
|
|---|
| 106 | echo "Failed <pre>";
|
|---|
| 107 | var_dump($tmp);
|
|---|
| 108 | echo "</pre>\n";
|
|---|
| [f8dfbe7] | 109 | }
|
|---|
| [3121b3b] | 110 | }
|
|---|
| [4ab1c0a] | 111 | }
|
|---|
| 112 | if($lastTime > -1){
|
|---|
| 113 | $result["lastvalid"] = $result["track"][$lastTime];
|
|---|
| [3121b3b] | 114 | }
|
|---|
| 115 |
|
|---|
| [4ab1c0a] | 116 | if($debug > 0){
|
|---|
| 117 | echo json_encode($result, JSON_PRETTY_PRINT);
|
|---|
| 118 | }
|
|---|
| 119 | else{
|
|---|
| 120 | echo json_encode($result);
|
|---|
| 121 | }
|
|---|
| [d7e6e9c] | 122 |
|
|---|
| 123 | ?>
|
|---|